MOIRAI<T>
Models & Types · AiDotNet.Finance.Forecasting.Foundation
MOIRAI (Masked EncOder-based UnIveRsAl TIme Series Foundation Model) implementation.
Inheritance: NeuralNetworkBase<T> → FinancialModelBase<T> → ForecastingModelBase<T> → TimeSeriesFoundationModelBase<T> → MOIRAI<T>
For Beginners
MOIRAI is designed to be truly universal:
Multi-Scale Patching: Unlike single-patch models, MOIRAI uses multiple patch sizes simultaneously:
- Small patches (8 steps): Capture fine-grained, high-frequency patterns
- Medium patches (16, 32): Balance detail and context
- Large patches (64+): Capture long-term trends and seasonality
Masked Encoder Architecture: During training, random patches are masked and the model learns to predict them. This is similar to BERT’s masked language modeling but for time series.
Mixture of Distributions: For probabilistic forecasting, MOIRAI outputs a mixture of Gaussian distributions, allowing it to model complex, multi-modal forecast uncertainties.
Any-to-Any Forecasting: The same model can predict any horizon from any context length, making it flexible for different forecasting scenarios.
How It Works
MOIRAI is Salesforce’s universal time series foundation model that uses multi-scale patching and masked encoder training for any-to-any forecasting. It can handle different time series frequencies and domains without fine-tuning.
Reference: Woo et al., “Unified Training of Universal Time Series Forecasting Transformers”, 2024.
https://arxiv.org/abs/2402.02592
Example
// Create a MOIRAI universal time series foundation model
// Multi-scale patching and masked encoder for any-to-any forecasting across domains
var architecture = new NeuralNetworkArchitecture<double>(
inputType: InputType.OneDimensional,
taskType: NeuralNetworkTaskType.Regression,
inputHeight: 512, inputWidth: 1, inputDepth: 1, outputSize: 24);
// Training mode with multi-scale masked encoder
var model = new MOIRAI<double>(architecture);
// ONNX inference mode with pre-trained model
var onnxModel = new MOIRAI<double>(architecture, "moirai_base.onnx");
Constructors
| Constructor | Summary |
|---|---|
MOIRAI(NeuralNetworkArchitecture<T> architecture, MOIRAIOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) |
Initializes a new instance in native mode for training. |
MOIRAI(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, MOIRAIOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) |
Initializes a new instance using an ONNX pretrained 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 NumMixtures { get; } |
Gets the number of mixture components for distribution output. |
int PatchSize { get; } |
|
int PredictionHorizon { get; } |
|
int SequenceLength { get; } |
|
int Stride { get; } |
|
int[] PatchSizes { get; } |
Gets the array of patch sizes used for multi-scale patching. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, T> Evaluate(Tensor<T> predictions, Tensor<T> actuals) |
|
Dictionary<string, T> GetFinancialMetrics() |
|
ModelMetadata<T> GetModelMetadata() |
|
Tensor<T> ApplyInstanceNormalization(Tensor<T> input) |
|
Tensor<T> AutoregressiveForecast(Tensor<T> input, int steps) |