How to Utilize Moirai for Time Series Forecasting

May 28, 2024 | Educational

Moirai-1.0-R-Small is an innovative transformer model specifically designed for time series forecasting. In this guide, we’ll walk you through the steps to get started with Moirai and troubleshoot any issues you might encounter along the way.

What is Moirai?

Moirai stands for the Masked Encoder-based Universal Time Series Forecasting Transformer. It’s a robust pre-trained model based on LOTSA data that efficiently predicts future values in time series data. To dig deeper into the architecture and outcomes of Moirai, you can refer to its research paper.

Moirai Architecture

Fig. 1: Overall architecture of Moirai. Here, variate 0 and 1 represent target variables while variate 2 acts as a dynamic covariate. The Transformer processes the patchified variates to generate forecast parameters.

Getting Started with Moirai

1. Installation

To perform inference with Moirai, follow these installation steps:

  1. Clone the repository:
  2. git clone https://github.com/SalesforceAIResearch/uni2ts.git
    cd uni2ts
  3. Create a virtual environment:
  4. virtualenv venv
    . venv/bin/activate
  5. Build from source:
  6. pip install -e .[notebook]
  7. Create a .env file:
  8. touch .env

2. Running a Sample Forecast

Now, let’s dive into the code to set up and run a forecasting model using Moirai. Imagine this process as preparing a gourmet meal where each ingredient needs to be measured and mixed accurately for a perfect dish!

  1. Import necessary libraries:
  2. 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
  3. Set parameters:
  4. SIZE = 'small'     
    PDT = 20             
    CTX = 200              
    PSZ = 'auto'         
    BSZ = 32               
    TEST = 100            
    
  5. Load and preprocess the data:
  6. url = 'https://gist.githubusercontent.com/rsnirwan/c8c8654a98350fadd229b00167174ec4/raw/a42101c7786d4bc7695228a0f2c8cea41340e18/fts_wide.csv'
    df = pd.read_csv(url, index_col=0, parse_dates=True)
    ds = PandasDataset(dict(df))
    train, test_template = split(ds, offset=-TEST)
  7. Prepare the model and predictions:
  8. test_data = test_template.generate_instances(prediction_length=PDT, windows=TEST // PDT, distance=PDT)
    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)
    
  9. Visualize the results:
  10. 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()

Troubleshooting

If you run into any issues while setting up or using Moirai, consider the following troubleshooting steps:

  • Ensure that you have the right version of Python installed as well as the necessary dependencies.
  • If the model fails to load, check your internet connection or if the Hugging Face model URL is accessible.
  • For data-related errors, double-check the dataset URL and format. Ensure that the data is correctly structured.
  • If you experience unexpected results in forecasts, consider adjusting the parameters such as context length or batch size.

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

Conclusion

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