PatchTST<T>

Models & Types · AiDotNet.Finance.Forecasting.Transformers

PatchTST (Patch Time Series Transformer) neural network for long-term time series forecasting.

Inheritance: NeuralNetworkBase<T> → FinancialModelBase<T> → ForecastingModelBase<T> → PatchTST<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

PatchTST treats a time series like a sentence: it breaks the data into “patches” (chunks of consecutive values) just as a sentence is split into words. A transformer then processes these patches to make predictions. This patching trick reduces computational cost dramatically while capturing long-range patterns. Each variable in a multivariate series is processed independently, which surprisingly improves accuracy.

How It Works

PatchTST is a state-of-the-art transformer model for long-term time series forecasting. It introduces patching (dividing time series into segments) and channel independence to achieve efficient and accurate forecasting.

Reference: Nie et al., “A Time Series is Worth 64 Words: Long-term Forecasting with Transformers”, ICLR 2023. https://arxiv.org/abs/2211.14730

Example

// Create a PatchTST for long-term forecasting with patching and channel independence
// Divides time series into patches (like words in a sentence) for efficient transformer processing
var architecture = new NeuralNetworkArchitecture<double>(
    inputType: InputType.OneDimensional,
    taskType: NeuralNetworkTaskType.Regression,
    inputHeight: 96, inputWidth: 7, inputDepth: 1, outputSize: 24);
            
// Training mode with patch embedding and channel-independent processing
var model = new PatchTST<double>(architecture);
            
// ONNX inference mode with pre-trained model
var onnxModel = new PatchTST<double>(architecture, "patchtst_etth1.onnx");

Constructors

Constructor Summary
PatchTST(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, PatchTSTOptions<T> options) Initializes the model from an already-resolved options instance.
PatchTST(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, PatchTSTOptions<T> options) Initializes the ONNX-backed model from an already-resolved options instance.

Properties

Property Summary
bool IsChannelIndependent { get; }
int PatchSize { get; }
int Stride { get; }

Methods

Method Summary
Dictionary<string, T> Evaluate(Tensor<T> inputs, Tensor<T> targets) Evaluates the model’s forecasting performance on test data.
Dictionary<string, T> GetFinancialMetrics() Gets financial-specific metrics from the model.
ModelMetadata<T> GetModelMetadata() Gets metadata about the model for serialization and inspection.
Tensor<T> ApplyInstanceNormalization(Tensor<T> input) Applies instance normalization (RevIN) during inference for distribution shift handling.
Tensor<T> AutoregressiveForecast(Tensor<T> input, int steps) Generates multi-step forecasts iteratively (autoregressive forecasting).