Class Signature
MoiraiTSModel class implements Salesforce’s Moirai, a universal time series forecasting model with support for multiple variants including standard Moirai, Moirai-MoE (Mixture of Experts), and Moirai2.
Initialization Parameters
Model configuration dictionary containing hyperparameters.
Hugging Face model repository ID. If not provided, defaults to Salesforce repositories based on model_type and model_size.
Type of Moirai model:
"moirai": Standard Moirai model"moirai-moe": Moirai with Mixture of Experts"moirai2": Moirai 2.0 with quantile predictions
Model size: “small”, “base”, or “large”.
Configuration Parameters
Forecast horizon length.
Context window length.
Patch size for tokenization.
Batch size for training and inference.
Number of samples for probabilistic forecasting (ignored for moirai2).
Quantile levels for moirai2 (e.g., [0.1, 0.5, 0.9]).
Dimension of target variables.
Dimension of dynamic real-valued features.
Dimension of past dynamic real-valued features.
Methods
finetune()
Dataset containing the input data and relevant functions like dataloaders.
Maximum number of training epochs.
Override default batch size.
Number of batches per epoch (optional).
Enable TF32 tensor cores for faster training.
The finetuned model is stored in
self.finetuned_model.evaluate()
Dataset to evaluate the model on.
Metrics to evaluate. Options: “MSE”, “MASE”.
Transforms to apply on the model output.
If True, use number of samples for distribution sampling.
If True, use the standard model; else use the finetuned model.
If True, only return metrics.
When
metric_only=True:Dictionary containing:mse: Mean Squared Errormae: Mean Absolute Errormase: Mean Absolute Scaled Errormape: Mean Absolute Percentage Errorrmse: Root Mean Squared Errornrmse: Normalized RMSEsmape: Symmetric Mean Absolute Percentage Errormsis: Mean Scaled Interval Scorend: Normalized Deviationmwsq: Mean Weighted Scaled Quantile Losscrps: Continuous Ranked Probability Score
metric_only=False:Tuple of (metrics, trues, preds, histories, quantile_preds):metrics: Dictionary of metricstrues: Ground truth values, shape (num_samples, num_ts, horizon_len)preds: Mean predictions, shape (num_samples, num_ts, horizon_len)histories: Historical values, shape (num_samples, num_ts, context_len)quantile_preds: Quantile predictions, shape (num_samples, num_ts, horizon_len, num_quantiles)
plot()
Dataset containing the input data and relevant functions.
If True, use the standard model; else use the finetuned model.
This method does not return a value. It displays visualizations.
Usage Example
Notes
- Moirai supports three variants: standard, MoE (Mixture of Experts), and Moirai2
- The model uses patch-based tokenization for efficient processing
- Moirai2 directly outputs quantile predictions, while earlier versions use sampling
- The model supports variable context and horizon lengths through autoregressive generation
- Finetuning uses frozen initial layers and only trains the last encoder layers and projection heads
- For long horizons exceeding max patches, autoregressive forecasting is automatically applied