GPT4TS<T>
Models & Types · AiDotNet.Finance.Forecasting.Foundation
GPT4TS — One Fits All: Power General Time Series Analysis by Pretrained LM.
Inheritance: NeuralNetworkBase<T> → FinancialModelBase<T> → ForecastingModelBase<T> → TimeSeriesFoundationModelBase<T> → GPT4TS<T>
For Beginners
GPT4TS takes a language model (GPT-2) that was trained to predict text and repurposes it for time series forecasting. The core idea is that patterns in sequences of numbers are similar to patterns in sequences of words. The language model stays frozen (unchanged) while small task-specific layers are added on top, making this approach surprisingly effective with minimal training.
How It Works
GPT4TS uses a frozen GPT-2 backbone with task-specific heads for time series forecasting, classification, and anomaly detection. It demonstrates that pretrained language models transfer effectively to time series tasks without fine-tuning the backbone.
Reference: Zhou et al., “One Fits All: Power General Time Series Analysis by Pretrained LM”, 2023.
Example
// Create a GPT4TS model using a frozen GPT-2 backbone for time series analysis
// Demonstrates that pretrained LLMs transfer effectively to time series tasks
var architecture = new NeuralNetworkArchitecture<double>(
inputType: InputType.OneDimensional,
taskType: NeuralNetworkTaskType.Regression,
inputHeight: 512, inputWidth: 1, inputDepth: 1, outputSize: 24);
// Training mode with frozen GPT-2 backbone and task-specific heads
var model = new GPT4TS<double>(architecture);
// ONNX inference mode with pre-trained model
var onnxModel = new GPT4TS<double>(architecture, "gpt4ts.onnx");
Constructors
| Constructor | Summary |
|---|---|
GPT4TS(NeuralNetworkArchitecture<T> architecture, GPT4TSOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) |
Creates a GPT4TS model in native mode for training or fine-tuning. |
GPT4TS(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, GPT4TSOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) |
Creates a GPT4TS 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() |
|
Dictionary<string, Tensor<T>> GetNamedLayerActivations(Tensor<T> input) |
|
ModelMetadata<T> GetModelMetadata() |
|
Tensor<T> ApplyInstanceNormalization(Tensor<T> input) |
|
Tensor<T> AutoregressiveForecast(Tensor<T> input, int steps) |