Moirai-1.0-R-Large: A Guide to Time Series Forecasting with Ease

May 28, 2024 | Educational

Have you ever wanted to predict future trends using past data? Whether it’s for finance, weather forecasting, or analyzing user behavior, time series forecasting is essential. Enter Moirai, the Masked Encoder-based Universal Time Series Forecasting Transformer, designed for effective time series prediction. In this blog, we will walk through how to utilize Moirai-1.0-R-Large, uncovering its architecture and functionality along the way. Let’s dive right in!

Understanding the Moirai Architecture

The Moirai architecture comprises several components designed to work harmoniously—think of it as a well-orchestrated symphony. Each instrument (or variate) contributes to the harmonious output (forecast). In our case, Moirai is fed data (the instruments) where multiple time series are tokenized into segments (or patches), forming the input for the transformer model.

Overall architecture of Moirai

In this visual, you will notice that the variates 0 and 1 represent the target variables we aim to predict, while variate 2 is a dynamic covariate, with known values extending into the forecast horizon.

Getting Started with Moirai

To start using Moirai for inference, follow these simple steps:

Step 1: Install the uni2ts Library

  • Clone the GitHub repository:
  • git clone https://github.com/SalesforceAIResearch/uni2ts.git
    cd uni2ts
       
  • Create a virtual environment:
  • virtualenv venv
    source venv/bin/activate
       
  • Build from source:
  • pip install -e .[notebook]
       
  • Create a .env file:
  • touch .env
       

Step 2: Load Your Data and Make Predictions

Here’s a snippet to kickstart your forecasting adventure:

import torch
import matplotlib.pyplot as plt
import pandas as pd
from gluonts.dataset.pandas import PandasDataset
from gluonts.dataset.split import split
from uni2ts.eval_util.plot import plot_single
from uni2ts.model.moirai import MoiraiForecast, MoiraiModule

SIZE = 'small'  #model size
PDT = 20  # prediction length
CTX = 200  # context length
PSZ = 'auto'  # patch size
BSZ = 32  # batch size
TEST = 100  # test set length

# Step 1: Read and preprocess data
url = 'https://gist.githubusercontent.com/rsnirwan/c8c8654a98350fadd229b00167174ec4/raw/42101c7786d4bc7695228a0f2c8cea41340e18/fts_wide.csv'
df = pd.read_csv(url, index_col=0, parse_dates=True)
ds = PandasDataset(dict(df))

# Step 2: Split and prepare rolling evaluation
train, test_template = split(ds, offset=-TEST)

# Step 3: Create model and predictor
model = MoiraiForecast(
    module=MoiraiModule.from_pretrained(f'Salesforce/moirai-1.0-R-{SIZE}'),
    prediction_length=PDT,
    context_length=CTX,
    patch_size=PSZ,
    num_samples=100,
    target_dim=1,
    feat_dynamic_real_dim=ds.num_feat_dynamic_real,
    past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real,
)

predictor = model.create_predictor(batch_size=BSZ)
forecasts = predictor.predict(test_data.input)

# Visualization
input_it = iter(test_data.input)
label_it = iter(test_data.label)
forecast_it = iter(forecasts)
inp = next(input_it)
label = next(label_it)
forecast = next(forecast_it)

plot_single(
    inp,
    label,
    forecast,
    context_length=200,
    name='pred',
    show_label=True,
)
plt.show()

This code snippet covers reading data, preprocessing it, and getting predictions using the Moirai model.

Troubleshooting Common Issues

If you run into any issues while implementing Moirai, here are some troubleshooting tips:

  • Package Not Found Error: Ensure you have activated your virtual environment before executing your Python script. Run source venv/bin/activate if you haven’t done so.
  • Model Weights Not Downloading: Check your internet connection. If it persists, verify that the URL used for model weights is correct.
  • Data Loading Issues: Make sure the dataset URL is accessible. If you encounter a 404 error, the URL may have changed or the dataset may have been removed.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

The Moirai Family of Models

Within the Moirai universe, you have options from small to large models, each tailored for specific performance needs:

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Conclusion

By following the steps outlined above, you can harness the power of Moirai-1.0-R-Large for your time series forecasting tasks. With its advanced architecture and flexibility, Moirai is equipped to tackle a wide array of scenarios. Happy forecasting!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox