LLMTime<T>

Models & Types · AiDotNet.Finance.Forecasting.Foundation

LLM-Time — Zero-Shot Time Series Forecasting via LLM Tokenization.

Inheritance: NeuralNetworkBase<T> → FinancialModelBase<T> → ForecastingModelBase<T> → TimeSeriesFoundationModelBase<T> → LLMTime<T>

For Beginners

LLM-Time takes an unconventional approach: it converts your numerical data into text (like “3.2, 3.5, 3.8, …”) and asks a large language model to predict the next numbers, just like predicting the next word in a sentence. Remarkably, this works without any training on time series data at all. The LLM already understands number patterns from its language training.

How It Works

LLM-Time converts numeric time series into text strings and uses pretrained LLMs (GPT-3, LLaMA) for zero-shot forecasting by treating the task as next-token prediction on numerical text. No fine-tuning is required—the LLM backbone is frozen.

Reference: Gruver et al., “Large Language Models Are Zero-Shot Time Series Forecasters”, NeurIPS 2023.

Example

// Create an LLMTime model for zero-shot forecasting via LLM tokenization
// Converts numeric time series to text strings and uses a frozen LLM for prediction
var architecture = new NeuralNetworkArchitecture<double>(
    inputType: InputType.OneDimensional,
    taskType: NeuralNetworkTaskType.Regression,
    inputHeight: 512, inputWidth: 1, inputDepth: 1, outputSize: 24);
            
// Training mode with text-based time series representation
var model = new LLMTime<double>(architecture);
            
// ONNX inference mode with pre-trained model
var onnxModel = new LLMTime<double>(architecture, "llmtime.onnx");

Constructors

Constructor Summary
LLMTime(NeuralNetworkArchitecture<T> architecture, LLMTimeOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) Creates an LLMTime model in native mode for training or fine-tuning.
LLMTime(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, LLMTimeOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) Creates an LLMTime 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)