TS2Vec<T>

Models & Types · AiDotNet.Finance.Forecasting.Foundation

TS2Vec — Contrastive Learning of Universal Time Series Representations.

Inheritance: NeuralNetworkBase<T> → FinancialModelBase<T> → ForecastingModelBase<T> → TimeSeriesFoundationModelBase<T> → TS2Vec<T>

For Beginners

TS2Vec creates a universal “fingerprint” for time series data at any time scale. It works by showing the model two different views of the same data (like seeing a city from two angles) and training it to recognize they represent the same thing. The resulting representations can be used for forecasting, classification, or anomaly detection without task-specific retraining.

How It Works

TS2Vec learns universal time series representations via hierarchical contrastive learning across augmented context views, producing contextual representations at arbitrary granularities. It uses a dilated CNN encoder with temporal and instance contrastive losses.

Reference: Yue et al., “TS2Vec: Towards Universal Representation of Time Series”, AAAI 2022.

Example

// Create a TS2Vec model for universal time series representation learning
// Hierarchical contrastive learning produces contextual representations at any granularity
var architecture = new NeuralNetworkArchitecture<double>(
    inputType: InputType.OneDimensional,
    taskType: NeuralNetworkTaskType.Regression,
    inputHeight: 512, inputWidth: 1, inputDepth: 1, outputSize: 24);
            
// Training mode with dilated CNN encoder and contrastive objectives
var model = new TS2Vec<double>(architecture);
            
// ONNX inference mode with pre-trained model
var onnxModel = new TS2Vec<double>(architecture, "ts2vec.onnx");

Constructors

Constructor Summary
TS2Vec(NeuralNetworkArchitecture<T> architecture, TS2VecOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) Creates a TS2Vec model in native mode for training or fine-tuning.
TS2Vec(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, TS2VecOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) Creates a TS2Vec model using a pretrained ONNX model.

Properties

Property Summary
FoundationModelSize ModelSize { get; }
bool IsChannelIndependent { get; }
bool SupportsTraining { get; }
bool UseNativeMode { get; }
int MaxContextLength { get; }
int MaxPredictionHorizon { get; }
int NumFeatures { get; }
int PatchSize { get; }
int PredictionHorizon { get; }
int SequenceLength { get; }
int Stride { get; }

Methods

Method Summary
Dictionary<string, T> Evaluate(Tensor<T> predictions, Tensor<T> actuals)
Dictionary<string, T> GetFinancialMetrics()
ModelMetadata<T> GetModelMetadata()
Tensor<T> ApplyInstanceNormalization(Tensor<T> input)
Tensor<T> AutoregressiveForecast(Tensor<T> input, int steps)