TimeLLM<T>

Models & Types · AiDotNet.Finance.Forecasting.Foundation

Time-LLM (Large Language Model Reprogramming for Time Series) implementation.

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

For Beginners

Time-LLM is a clever way to use powerful language models for time series:

The Key Insight: LLMs like GPT/LLaMA are amazing at pattern recognition in sequences. Time-LLM asks: “Can we make time series ‘speak’ the language of LLMs?”

How It Works:

  1. Patch Reprogramming: Convert time series patches into “prompt-like” tokens
  2. Text Prototypes: Learn embeddings that bridge numeric and text domains
  3. Frozen LLM: The LLM weights stay fixed (no fine-tuning needed)
  4. Output Projection: Map LLM output back to forecast values

Advantages:

  • Leverages powerful pretrained LLMs without expensive fine-tuning
  • Works with any LLM backbone (GPT-2, LLaMA, etc.)
  • Only trains small reprogramming layers
  • Zero-shot transfer to new domains

How It Works

Time-LLM repurposes frozen large language models for time series forecasting by learning a reprogramming layer that translates time series into text-like representations that the LLM can understand.

Reference: Jin et al., “Time-LLM: Time Series Forecasting by Reprogramming Large Language Models”, 2024. https://arxiv.org/abs/2310.01728

Example

// Create a Time-LLM model that repurposes frozen LLMs for time series forecasting
// Learns a reprogramming layer to translate time series into text-like representations
var architecture = new NeuralNetworkArchitecture<double>(
    inputType: InputType.OneDimensional,
    taskType: NeuralNetworkTaskType.Regression,
    inputHeight: 512, inputWidth: 1, inputDepth: 1, outputSize: 24);
            
// Training mode with patch reprogramming and frozen LLM backbone
var model = new TimeLLM<double>(architecture);
            
// ONNX inference mode with pre-trained model
var onnxModel = new TimeLLM<double>(architecture, "time_llm.onnx");

Constructors

Constructor Summary
TimeLLM(NeuralNetworkArchitecture<T> architecture, TimeLLMOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) Initializes a new instance in native mode for training.
TimeLLM(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, TimeLLMOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) Initializes a new instance using an ONNX pretrained model.

Properties

Property Summary
bool IsChannelIndependent { get; }
bool SupportsTraining { get; }
bool UseNativeMode { get; }
int NumFeatures { get; }
int NumPrototypes { get; } Gets the number of text prototypes.
int PatchSize { get; }
int PredictionHorizon { get; }
int SequenceLength { get; }
int Stride { get; }
string LLMBackbone { get; } Gets the LLM backbone type.

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)