Class AutoformerModel<T>
- Namespace
- AiDotNet.TimeSeries
- Assembly
- AiDotNet.dll
Implements the Autoformer model for long-term time series forecasting with decomposition.
public class AutoformerModel<T> : TimeSeriesModelBase<T>, ITimeSeriesModel<T>, IFullModel<T, Matrix<T>, Vector<T>>, IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Matrix<T>, Vector<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>, IGradientComputable<T, Matrix<T>, Vector<T>>, IJitCompilable<T>
Type Parameters
TThe numeric type used for calculations (e.g., float, double).
- Inheritance
-
AutoformerModel<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
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:
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
Auto-Correlation Mechanism: Replaces point-wise self-attention with period-based dependencies. Uses FFT to find correlations between sub-series efficiently (O(L log L)). Aggregates similar sub-sequences based on their correlation strength.
Progressive Decomposition Architecture: Each encoder/decoder layer further refines the decomposition. Seasonal and trend branches are processed separately and accumulated.
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)
Constructors
AutoformerModel(AutoformerOptions<T>?)
Initializes a new instance of the Autoformer model with the specified options.
public AutoformerModel(AutoformerOptions<T>? options = null)
Parameters
optionsAutoformerOptions<T>Configuration options for the model. Uses defaults if null.
Remarks
For Beginners: Create an Autoformer like this:
var model = new AutoformerModel<double>(new AutoformerOptions<double>
{
LookbackWindow = 96, // Look at past 96 time steps
ForecastHorizon = 24, // Predict next 24 time steps
MovingAverageKernel = 25 // Trend smoothing window
});
Methods
CreateInstance()
Creates a new instance of the derived model class.
protected override IFullModel<T, Matrix<T>, Vector<T>> CreateInstance()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new instance of the same model type.
Remarks
This abstract factory method must be implemented by derived classes to create a new instance of their specific type. It's used by Clone and DeepCopy to ensure that the correct derived type is instantiated.
For Beginners: This method creates a new, empty instance of the specific model type. It's used during cloning and deep copying to ensure that the copy is of the same specific type as the original.
For example, if the original model is an ARIMA model, this method would create a new ARIMA model. If it's a TBATS model, it would create a new TBATS model.
DeserializeCore(BinaryReader)
Deserializes model-specific data from the binary reader.
protected override void DeserializeCore(BinaryReader reader)
Parameters
readerBinaryReaderThe binary reader to read from.
Remarks
This abstract method must be implemented by each specific model type to load its unique parameters and state.
For Beginners: This method is responsible for loading the specific details that make each type of time series model unique. It reads exactly what was written by SerializeCore, in the same order, reconstructing the specialized parts of the model.
It's the counterpart to SerializeCore and should read data in exactly the same order and format that it was written.
This separation allows the base class to handle common deserialization tasks while each model type handles its specialized data.
GetModelMetadata()
Gets metadata about the time series model.
public override ModelMetadata<T> GetModelMetadata()
Returns
- ModelMetadata<T>
A ModelMetaData object containing information about the model.
Remarks
This method provides comprehensive metadata about the model, including its type, configuration options, training status, evaluation metrics, and information about which features/lags are most important.
For Beginners: This method provides important information about the model that can help you understand its characteristics and performance.
The metadata includes:
- The type of model (e.g., ARIMA, TBATS, Neural Network)
- Configuration details (e.g., lag order, seasonality period)
- Whether the model has been trained
- Performance metrics from the last evaluation
- Information about which features (time periods) are most influential
This information is useful for documentation, model comparison, and debugging. It's like a complete summary of everything important about the model.
PredictMultiple(Vector<T>)
Predicts multiple time steps ahead using the Autoformer architecture.
public Vector<T> PredictMultiple(Vector<T> input)
Parameters
inputVector<T>Input sequence of historical values.
Returns
- Vector<T>
Vector of predictions for the entire forecast horizon.
Remarks
This method returns predictions for all steps in the forecast horizon, as Autoformer is designed for multi-horizon forecasting.
PredictSingle(Vector<T>)
Predicts a single future value using the trained model.
public override T PredictSingle(Vector<T> input)
Parameters
inputVector<T>The input sequence.
Returns
- T
The predicted value.
SerializeCore(BinaryWriter)
Serializes model-specific data to the binary writer.
protected override void SerializeCore(BinaryWriter writer)
Parameters
writerBinaryWriterThe binary writer to write to.
Remarks
This abstract method must be implemented by each specific model type to save its unique parameters and state.
For Beginners: This method is responsible for saving the specific details that make each type of time series model unique. Different models have different internal structures and parameters that need to be saved separately from the common elements.
For example:
- An ARIMA model would save its AR, I, and MA coefficients
- A TBATS model would save its level, trend, and seasonal components
- A neural network model would save its weights and biases
This separation allows the base class to handle common serialization tasks while each model type handles its specialized data.
TrainCore(Matrix<T>, Vector<T>)
Trains the model using backpropagation through the Autoformer architecture. For multi-step forecasting, the input should contain lookback + forecastHorizon values. The first lookback values are used as input, and the last forecastHorizon values from the sequence are used as multi-step targets.
protected override void TrainCore(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>yVector<T>