Views
No views yet

1git clone https://github.com/SalesforceAIResearch/uni2ts.git
2cd uni2ts1virtualenv venv
2. venv/bin/activatepip install -e '.[notebook]'.env file:touch .env1import torch
2import matplotlib.pyplot as plt
3import pandas as pd
4from gluonts.dataset.pandas import PandasDataset
5from gluonts.dataset.split import split
6
7from uni2ts.eval_util.plot import plot_single
8from uni2ts.model.moirai import MoiraiForecast, MoiraiModule
9
10
11SIZE = "small" # model size: choose from {'small', 'base', 'large'}
12PDT = 20 # prediction length: any positive integer
13CTX = 200 # context length: any positive integer
14PSZ = "auto" # patch size: choose from {"auto", 8, 16, 32, 64, 128}
15BSZ = 32 # batch size: any positive integer
16TEST = 100 # test set length: any positive integer
17
18# Read data into pandas DataFrame
19url = (
20 "https://gist.githubusercontent.com/rsnirwan/c8c8654a98350fadd229b00167174ec4"
21 "/raw/a42101c7786d4bc7695228a0f2c8cea41340e18f/ts_wide.csv"
22)
23df = pd.read_csv(url, index_col=0, parse_dates=True)
24
25# Convert into GluonTS dataset
26ds = PandasDataset(dict(df))
27
28# Split into train/test set
29train, test_template = split(
30 ds, offset=-TEST
31) # assign last TEST time steps as test set
32
33# Construct rolling window evaluation
34test_data = test_template.generate_instances(
35 prediction_length=PDT, # number of time steps for each prediction
36 windows=TEST // PDT, # number of windows in rolling window evaluation
37 distance=PDT, # number of time steps between each window - distance=PDT for non-overlapping windows
38)
39
40# Prepare pre-trained model by downloading model weights from huggingface hub
41model = MoiraiForecast(
42 module=MoiraiModule.from_pretrained(f"Salesforce/moirai-1.0-R-{SIZE}"),
43 prediction_length=PDT,
44 context_length=CTX,
45 patch_size=PSZ,
46 num_samples=100,
47 target_dim=1,
48 feat_dynamic_real_dim=ds.num_feat_dynamic_real,
49 past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real,
50)
51
52predictor = model.create_predictor(batch_size=BSZ)
53forecasts = predictor.predict(test_data.input)
54
55input_it = iter(test_data.input)
56label_it = iter(test_data.label)
57forecast_it = iter(forecasts)
58
59inp = next(input_it)
60label = next(label_it)
61forecast = next(forecast_it)
62
63plot_single(
64 inp,
65 label,
66 forecast,
67 context_length=200,
68 name="pred",
69 show_label=True,
70)
71plt.show()| # Model | # Parameters |
|---|---|
| Moirai-1.0-R-Small | 14m |
| Moirai-1.0-R-Base | 91m |
| Moirai-1.0-R-Large | 311m |
1@article{woo2024unified,
2 title={Unified Training of Universal Time Series Forecasting Transformers},
3 author={Woo, Gerald and Liu, Chenghao and Kumar, Akshat and Xiong, Caiming and Savarese, Silvio and Sahoo, Doyen},
4 journal={arXiv preprint arXiv:2402.02592},
5 year={2024}
6}