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 pandas as pd
3from gluonts.dataset.pandas import PandasDataset
4from gluonts.dataset.split import split
5from huggingface_hub import hf_hub_download
6
7from uni2ts.eval_util.plot import plot_single
8from uni2ts.model.moirai import MoiraiForecast
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.load_from_checkpoint(
42 checkpoint_path=hf_hub_download(
43 repo_id=f"Salesforce/moirai-R-{SIZE}", filename="model.ckpt"
44 ),
45 prediction_length=PDT,
46 context_length=CTX,
47 patch_size=PSZ,
48 num_samples=100,
49 target_dim=1,
50 feat_dynamic_real_dim=ds.num_feat_dynamic_real,
51 past_feat_dynamic_real_dim=ds.num_past_feat_dynamic_real,
52 map_location="cuda:0" if torch.cuda.is_available() else "cpu",
53)
54
55predictor = model.create_predictor(batch_size=BSZ)
56forecasts = predictor.predict(test_data.input)
57
58input_it = iter(test_data.input)
59label_it = iter(test_data.label)
60forecast_it = iter(forecasts)
61
62inp = next(input_it)
63label = next(label_it)
64forecast = next(forecast_it)
65
66plot_single(
67 inp,
68 label,
69 forecast,
70 context_length=200,
71 name="pred",
72 show_label=True,
73)| # 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}