AutoformerModel<T>

Models & Types · AiDotNet.TimeSeries

Implements the Autoformer model for long-term time series forecasting with decomposition.

Inheritance: TimeSeriesModelBase<T> → AutoformerModel<T>

Implements: IGeneratedParameterRegistrar<T>, ISupportsLossFunction<T>

For Beginners

Autoformer is like having two experts work together:

  • One expert focuses on the long-term direction (trend)
  • One expert focuses on repeating patterns (seasonality)

Instead of looking at individual data points, it looks at how patterns repeat over time. If today’s pattern looks like last week’s pattern, that’s useful information!

Example use cases:

  • Electricity demand forecasting (daily/weekly patterns)
  • Retail sales prediction (seasonal buying patterns)
  • Traffic flow prediction (rush hour patterns)

How It Works

The Long-Term Forecasting Challenge: Long-term time series forecasting requires models that can capture both fine-grained seasonal patterns and long-term trends. Traditional approaches struggle because:

  • RNNs have difficulty with long-range dependencies
  • Transformers treat time series like text, ignoring continuous nature
  • Neither explicitly models trend and seasonality separately

The Autoformer Solution (Wu et al., NeurIPS 2021): Autoformer introduces three key innovations:

  1. Series Decomposition Block:

Progressive separation of trend and seasonal components at each layer. Uses moving average to extract trend, remainder is seasonal. Formula: Trend = MovingAvg(X), Seasonal = X - Trend

  1. Auto-Correlation Mechanism:

Replaces point-wise self-attention with period-based dependencies. Finds correlations between sub-series and aggregates similar sub-sequences based on their correlation strength.

  1. Progressive Decomposition Architecture:

Each encoder/decoder layer further refines the decomposition. Seasonal and trend branches are processed separately and accumulated.

This class forecasts a univariate series (the reference’s features='S') with the network the official code builds (AutoformerNetwork, shared with the Finance Autoformer and checked against thuml/Autoformer), and trains it as the reference’s experiment does: the series standardized with training statistics, every window a sample, Adam on the MSE over shuffled batches (the last incomplete batch dropped), the learning rate halved after every epoch. The temporal embedding needs timestamps, which the time-series interface does not carry, so it is not used.

Example

using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.TimeSeries;
using AiDotNet.Tensors.LinearAlgebra;

double[] series =
{
    120, 135, 148, 160, 155, 170, 180, 195, 210, 198, 220, 235,
    140, 155, 165, 178, 172, 190, 200, 215, 230, 218, 245, 260
};
var x = new Matrix<double>(series.Length, 1);
for (int i = 0; i < series.Length; i++) x[i, 0] = i;

var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new AutoformerModel<double>())
    .ConfigureDataLoader(DataLoaders.FromMatrixVector(x, new Vector<double>(series)))
    .BuildAsync();

var forecast = result.Predict(new Matrix<double>(6, 1));
Console.WriteLine($"AutoformerModel: forecast {forecast.Length} steps.");

Constructors

Constructor Summary
AutoformerModel(AutoformerOptions<T> options) Initializes a new instance of the Autoformer model with the specified options.

Methods

Method Summary
ModelMetadata<T> GetModelMetadata()
T PredictSingle(Vector<T> input) Predicts the value after the given history (its last LookbackWindow values).
Vector<T> Predict(Matrix<T> input) Predicts the series: with as many rows as the training series, the one-step forecast of each observation from the values before it; with one column (time steps), the forecasts of the next input.Rows steps after the training series, continue…
Vector<T> PredictMultiple(Vector<T> input) Forecasts the ForecastHorizon values after the given history in one forward pass.
void SetLossFunction(ILossFunction<T> lossFunction)