TCN<T>
Models & Types · AiDotNet.Finance.Forecasting.Neural
TCN (Temporal Convolutional Network) model for time series forecasting.
Inheritance: NeuralNetworkBase<T> → FinancialModelBase<T> → ForecastingModelBase<T> → TCN<T>
For Beginners
TCN is a modern alternative to LSTM/GRU for sequence modeling.
Key Innovation - Dilated Convolutions: Imagine reading a book. Regular convolution reads 3 consecutive words at a time. Dilated convolution can skip words to cover more context:
- Dilation 1: Reads words 1, 2, 3
- Dilation 2: Reads words 1, 3, 5 (skipping every other word)
- Dilation 4: Reads words 1, 5, 9 (skipping 3 words)
By stacking layers with increasing dilation, TCN can “see” very far into the past efficiently. With 8 layers and kernel size 3, it can consider over 1000 past time steps!
Causal Convolutions: TCN only looks at past and present values, never future ones. This makes it suitable for real-time prediction where you can’t peek ahead.
Residual Connections: Each block adds its input to its output: output = block(input) + input This helps gradients flow during training and allows the network to learn when to ignore certain blocks.
How It Works
TCN uses dilated causal convolutions to model temporal sequences. It offers several advantages over recurrent networks:
- Parallel computation (faster training)
- Flexible receptive field through dilation
- No vanishing gradient problem
- Better handling of long sequences
Reference: Bai et al., “An Empirical Evaluation of Generic Convolutional and
Recurrent Networks for Sequence Modeling”, 2018. https://arxiv.org/abs/1803.01271
Example
var architecture = new NeuralNetworkArchitecture<double>(
inputType: InputType.OneDimensional,
taskType: NeuralNetworkTaskType.Regression,
inputHeight: 60, inputWidth: 1, inputDepth: 1, outputSize: 24);
var model = new TCN<double>(architecture);
var onnxModel = new TCN<double>(architecture, "tcn.onnx");
Constructors
| Constructor | Summary |
|---|---|
TCN() |
Creates a TCN in native mode for training from scratch. |
TCN(NeuralNetworkArchitecture<T> architecture, TCNOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) |
Creates a TCN model using the specified architecture. |
TCN(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, TCNOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) |
Creates a TCN 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() |
|
ModelMetadata<T> GetModelMetadata() |
|
Tensor<T> ApplyInstanceNormalization(Tensor<T> input) |
|
Tensor<T> AutoregressiveForecast(Tensor<T> input, int steps) |