TimesNet<T>
Models & Types · AiDotNet.Finance.Forecasting.Transformers
TimesNet (Temporal 2D-Variation Modeling) neural network for time series analysis.
Inheritance: NeuralNetworkBase<T> → FinancialModelBase<T> → ForecastingModelBase<T> → TimesNet<T>
For Beginners
TimesNet thinks about time series data like a calendar:
- Instead of just seeing days in a row (1D), it arranges them into weeks and months (2D)
- This makes it easy to see patterns like “every Monday sales drop” or “end of month peaks”
- It automatically discovers what time scales matter most (daily, weekly, quarterly, etc.)
Key innovations:
- Period Discovery: Uses FFT to automatically find dominant periods
- 2D Transformation: Reshapes 1D time series into 2D based on periods
- Inception Module: Multi-scale convolutions capture patterns at different granularities
How It Works
TimesNet transforms 1D time series into 2D tensors based on automatically discovered periods, then applies 2D convolutions to capture both intra-period and inter-period variations. This approach is particularly effective for time series with multiple periodic patterns.
Reference: Wu et al., “TimesNet: Temporal 2D-Variation Modeling for General
Time Series Analysis”, ICLR 2023. https://arxiv.org/abs/2210.02186
Example
// Create a TimesNet for time series analysis with 2D temporal variation modeling
// Transforms 1D series into 2D based on discovered periods, then applies 2D convolutions
var architecture = new NeuralNetworkArchitecture<double>(
inputType: InputType.OneDimensional,
taskType: NeuralNetworkTaskType.Regression,
inputHeight: 96, inputWidth: 7, inputDepth: 1, outputSize: 24);
// Training mode with FFT-based period discovery and inception convolutions
var model = new TimesNet<double>(architecture);
// ONNX inference mode with pre-trained model
var onnxModel = new TimesNet<double>(architecture, "timesnet_electricity.onnx");
Constructors
| Constructor | Summary |
|---|---|
TimesNet(NeuralNetworkArchitecture<T> architecture, TimesNetOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) |
Creates a TimesNet network in native mode for training from scratch. |
TimesNet(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, TimesNetOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) |
Creates a TimesNet network using pretrained ONNX model. |
Properties
| Property | Summary |
|---|---|
bool IsChannelIndependent { get; } |
|
bool SupportsTraining { get; } |
|
bool UseNativeMode { 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() |
|
Dictionary<string, Tensor<T>> GetNamedLayerActivations(Tensor<T> input) |
Captures named activations along TimesNet’s actual forward path. |
ModelMetadata<T> GetModelMetadata() |
|
Tensor<T> ApplyInstanceNormalization(Tensor<T> input) |
|
Tensor<T> AutoregressiveForecast(Tensor<T> input, int steps) |
|
void SetTrainingMode(bool isTraining) |
Propagates training mode to every layer. |