TimesFM<T>

Models & Types · AiDotNet.Finance.Forecasting.Foundation

TimesFM (Time Series Foundation Model) for zero-shot time series forecasting.

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

For Beginners

TimesFM is a revolutionary approach to time series forecasting:

Foundation Model Concept: Just like GPT learns language patterns from vast text data, TimesFM learns time series patterns from millions of diverse series:

  • Weather data (temperature, precipitation, wind)
  • Financial data (stock prices, exchange rates)
  • Retail data (sales, inventory, demand)
  • Energy data (consumption, production, prices)

Zero-Shot Capability: The “zero-shot” term means no training required for new tasks:

  • Traditional models: Train specifically for your data
  • TimesFM: Works immediately on any time series
  • Just provide history → get forecasts

Patching Innovation: TimesFM groups consecutive time steps into “patches”:

  • Example: 512 time steps → 16 patches of 32 steps each
  • Each patch becomes one token for the transformer
  • Benefits: Longer context, faster processing, captures local patterns

Decoder-Only Architecture: Like GPT, TimesFM uses causal (one-directional) attention:

  • Each position only attends to earlier positions
  • Naturally suited for autoregressive forecasting
  • Generates predictions step by step

When TimesFM Excels:

  • Quick prototyping without model training
  • Cross-domain forecasting (same model for different data types)
  • Limited historical data (leverages pre-training knowledge)
  • Baseline comparisons for specialized models

How It Works

TimesFM is Google’s foundation model for time series forecasting. It uses a decoder-only transformer architecture pre-trained on a massive dataset spanning diverse domains, enabling zero-shot forecasting without task-specific training.

Reference: Das et al., “A decoder-only foundation model for time-series forecasting”, 2024. https://arxiv.org/abs/2310.10688

Example

// Create a TimesFM (Google) for zero-shot time series forecasting
// Decoder-only transformer pre-trained on diverse domains with patch-based tokenization
var architecture = new NeuralNetworkArchitecture<double>(
    inputType: InputType.OneDimensional,
    taskType: NeuralNetworkTaskType.Regression,
    inputHeight: 512, inputWidth: 1, inputDepth: 1, outputSize: 24);
            
// Training mode with patch-based decoder-only architecture
var model = new TimesFM<double>(architecture);
            
// ONNX inference mode with pre-trained model
var onnxModel = new TimesFM<double>(architecture, "timesfm.onnx");

Constructors

Constructor Summary
TimesFM(NeuralNetworkArchitecture<T> architecture, TimesFMOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) Creates a TimesFM model in native mode for training or fine-tuning.
TimesFM(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, TimesFMOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) Creates a TimesFM model using 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 NumHeads { get; } Gets the number of attention heads in the transformer.
int NumPatches { get; } Gets the number of patches derived from the context length and patch size.
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)