Mastering Time Series Forecasting with Moirai-1.0-R-Large

May 30, 2024 | Educational

Time series forecasting is one of the cornerstones of data analysis, and with the innovative Moirai-1.0-R-Large, you can take your forecasting to the next level. This powerful model, based on a Transformer architecture, is designed to handle complex time series data and make accurate predictions. In this article, we will guide you through setting up and using Moirai, ensuring a seamless experience.

What is Moirai?

Moirai, the Masked Encoder-based Universal Time Series Forecasting Transformer, is a large model that has been pre-trained on LOTSA data. For technical details, you can refer to the Moirai paper. The overall architecture visualizes how it processes a 3-variate time series, making it a cutting-edge tool for forecasters.

Moirai Architecture Visualization

Installation and Setup

Let’s dive into how to perform inference with Moirai:

  1. Clone the Repository: Start by cloning the Uni2TS GitHub repository.
  2. git clone https://github.com/SalesforceAIResearch/uni2ts.git
    cd uni2ts
  3. Create a Virtual Environment: It’s crucial to create a virtual environment to keep dependencies organized.
  4. virtualenv venv
    source venv/bin/activate
  5. Build from Source: Install the necessary packages.
  6. pip install -e .[notebook]
  7. Create a .env File: Initiate the environment file for configuration.
  8. touch .env

Quick Example to Get Started

Now, let’s set up a quick example using Moirai:

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: choose from small, base, large
PDT = 20  # prediction length
CTX = 200  # context length
PSZ = 'auto'  # patch size
BSZ = 32  # batch size
TEST = 100  # test set length

# Read data into pandas DataFrame
url = "https://gist.githubusercontent.com/rsnirwanc/8c8654a98350fadd229b00167174ec4/raw/a42101c7786d4bc7695228a0f2c8cea41340e18/fts_wide.csv"
df = pd.read_csv(url, index_col=0, parse_dates=True)

# Convert into GluonTS dataset
ds = PandasDataset(dict(df))

# Split into train-test set
train, test_template = split(ds, offset=-TEST)

# Rolling window evaluation
test_data = test_template.generate_instances(prediction_length=PDT, windows=TEST // PDT, distance=PDT)

# Prepare pre-trained model
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)

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()

Understanding the Code with an Analogy

Imagine you are a chef preparing a grand meal for a feast. Each step in your recipe corresponds to a line of code in the example above. Here’s how it breaks down:

  • Gather Ingredients: Just as a chef selects the freshest vegetables, herbs, and spices, the code first reads and prepares your time-series data.
  • Measure Quantities: Like measuring the right amount of each ingredient, the model sets parameters for context length, prediction length, and batch size to ensure accurate forecasting.
  • Mix and Cook: The core of the recipe involves mixing and cooking, akin to the model training and evaluating data. Here, you generate instances and prepare the model for prediction.
  • Presentation: Finally, just as a chef presents the dish elegantly on the table, the code concludes by plotting the results for visual representation.

Troubleshooting Tips

If you encounter issues while using Moirai, consider the following troubleshooting ideas:

  • Environment Issues: Ensure that your virtual environment is active. If dependencies are missing, check that you installed them properly.
  • Data Loading Errors: If your dataset doesn’t load, verify the URL or ensure that your data format is correct.
  • Model Prediction Failures: Double-check your parameter settings such as prediction length, context length, and patches, to ensure they align with your dataset.

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

Conclusion

By using Moirai-1.0-R-Large, you can significantly enhance your time series forecasting capabilities. Enjoy exploring the vast possibilities that predictive modeling has to offer!

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.

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

Tech News and Blog Highlights, Straight to Your Inbox