uni2ts

Unified Training of Universal Time Series Forecasting Transformers

https://github.com/salesforceairesearch/uni2ts

Science Score: 54.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
    Links to: arxiv.org
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (8.6%) to scientific vocabulary

Keywords

deep-learning forecasting machine-learning pre-trained-models pre-training representation-learning time-series time-series-forecasting transformers universal-forecasting
Last synced: 6 months ago · JSON representation ·

Repository

Unified Training of Universal Time Series Forecasting Transformers

Basic Info
  • Host: GitHub
  • Owner: SalesforceAIResearch
  • License: apache-2.0
  • Language: Jupyter Notebook
  • Default Branch: main
  • Homepage:
  • Size: 8.29 MB
Statistics
  • Stars: 1,227
  • Watchers: 10
  • Forks: 147
  • Open Issues: 36
  • Releases: 2
Topics
deep-learning forecasting machine-learning pre-trained-models pre-training representation-learning time-series time-series-forecasting transformers universal-forecasting
Created about 2 years ago · Last pushed 6 months ago
Metadata Files
Readme Contributing License Code of conduct Citation Codeowners Security

README.md

# Unified Training of Universal Time Series Transformers [![arXiv](https://img.shields.io/badge/Moirai-2402.02592-b31b1b.svg)](https://arxiv.org/abs/2402.02592) [![arXiv](https://img.shields.io/badge/MoiraiMoE-2410.10469-b31b1b.svg)](https://arxiv.org/abs/2410.10469) [![huggingface](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Models-FFD21E)](https://huggingface.co/collections/Salesforce/moirai-r-models-65c8d3a94c51428c300e0742) [![huggingface](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Leaderboard-FFD21E)](https://huggingface.co/spaces/Salesforce/GIFT-Eval) [![License: MIT](https://img.shields.io/badge/License-Apache--2.0-green.svg)](https://opensource.org/licenses/Apache-2.0)

Uni2TS is a PyTorch based library for research and applications related to Time Series Forecasting. It provides a unified framework for large-scale pre-training, fine-tuning, inference, and evaluation of Universal Time Series Transformers.

Related reading: Moirai Paper, Moirai Salesforce Blog, Moirai-MoE Paper, Moirai-MoE Salesforce Blog, Moirai-MoE AI Horizon Forecast Blog, Moirai-MoE Jiqizhixin Blog.

🎉 What's New

⚙️ Installation

  1. Clone repository: shell git clone https://github.com/SalesforceAIResearch/uni2ts.git cd uni2ts

2) Create virtual environment: shell virtualenv venv . venv/bin/activate

3) Build from source: shell pip install -e '.[notebook]'

4) Create a .env file: shell touch .env

We also support installation via PyPI. shell pip install uni2ts

🏃 Getting Started

Let's see a simple example on how to use Uni2TS to make zero-shot forecasts from a pre-trained model. We first load our data using pandas, in the form of a wide DataFrame. Uni2TS relies on GluonTS for inference as it provides many convenience functions for time series forecasting, such as splitting a dataset into a train/test split and performing rolling evaluations, as demonstrated below.

```python import torch import matplotlib.pyplot as plt import pandas as pd from gluonts.dataset.pandas import PandasDataset from gluonts.dataset.split import split from huggingfacehub import hfhub_download

from uni2ts.evalutil.plot import plotsingle from uni2ts.model.moirai import MoiraiForecast, MoiraiModule from uni2ts.model.moirai_moe import MoiraiMoEForecast, MoiraiMoEModule

MODEL = "moirai2" # model name: choose from {'moirai', 'moirai-moe', 'moirai2'} SIZE = "small" # model size: choose from {'small', 'base', 'large'} PDT = 20 # prediction length: any positive integer CTX = 200 # context length: any positive integer PSZ = "auto" # patch size: choose from {"auto", 8, 16, 32, 64, 128} BSZ = 32 # batch size: any positive integer TEST = 100 # test set length: any positive integer

Read data into pandas DataFrame

url = ( "https://gist.githubusercontent.com/rsnirwan/c8c8654a98350fadd229b00167174ec4" "/raw/a42101c7786d4bc7695228a0f2c8cea41340e18f/tswide.csv" ) df = pd.readcsv(url, indexcol=0, parsedates=True)

Convert into GluonTS dataset

ds = PandasDataset(dict(df))

Split into train/test set

train, test_template = split( ds, offset=-TEST ) # assign last TEST time steps as test set

Construct rolling window evaluation

testdata = testtemplate.generateinstances( predictionlength=PDT, # number of time steps for each prediction windows=TEST // PDT, # number of windows in rolling window evaluation distance=PDT, # number of time steps between each window - distance=PDT for non-overlapping windows )

Prepare pre-trained model by downloading model weights from huggingface hub

if MODEL == "moirai": model = MoiraiForecast( module=MoiraiModule.frompretrained(f"Salesforce/moirai-1.1-R-{SIZE}"), predictionlength=PDT, contextlength=CTX, patchsize=PSZ, numsamples=100, targetdim=1, featdynamicrealdim=ds.numfeatdynamicreal, pastfeatdynamicrealdim=ds.numpastfeatdynamicreal, ) elif MODEL == "moirai-moe": model = MoiraiMoEForecast( module=MoiraiMoEModule.frompretrained(f"Salesforce/moirai-moe-1.0-R-{SIZE}"), predictionlength=PDT, contextlength=CTX, patchsize=16, numsamples=100, targetdim=1, featdynamicrealdim=ds.numfeatdynamicreal, pastfeatdynamicrealdim=ds.numpastfeatdynamicreal, ) elif MODEL == "moirai2": model = Moirai2Forecast( module=Moirai2Module.frompretrained( f"Salesforce/moirai-2.0-R-small", ), predictionlength=100, contextlength=1680, targetdim=1, featdynamicrealdim=0, pastfeatdynamicreal_dim=0, )

predictor = model.createpredictor(batchsize=BSZ) forecasts = predictor.predict(test_data.input)

inputit = iter(testdata.input) labelit = iter(testdata.label) forecast_it = iter(forecasts)

inp = next(inputit) label = next(labelit) forecast = next(forecast_it)

plotsingle( inp, label, forecast, contextlength=200, name="pred", show_label=True, ) plt.show() ```

📔 Jupyter Notebook Examples

See the example folder for more examples on common tasks, e.g. visualizing forecasts, predicting from pandas DataFrame, etc.

💻 Command Line Interface

We provide several scripts which act as a command line interface to easily run fine-tuning, evaluation, and even pre-training jobs. Configurations are managed with the Hydra framework.

Fine-tuning

Firstly, let's see how to use Uni2TS to fine-tune a pre-trained model on your custom dataset. Uni2TS uses the Hugging Face datasets library to handle data loading, and we first need to convert your dataset into the Uni2TS format. If your dataset is a simple pandas DataFrame, we can easily process your dataset with the following script. We'll use the ETTh1 dataset from the popular Long Sequence Forecasting benchmark for this example. For more complex use cases, see this notebook for more in-depth examples on how to use your custom dataset with Uni2TS. For formal LSF finetuning experiments based on original configurations, see this folder for shell scripts and more detailed examples.

  1. To begin the process, add the path to the directory where you want to save the processed dataset into the .env file. shell echo "CUSTOM_DATA_PATH=PATH_TO_SAVE" >> .env

  2. Run the following script to process the dataset into the required format. For the dataset_type option, we support wide, long and wide_multivariate. shell python -m uni2ts.data.builder.simple ETTh1 dataset/ETT-small/ETTh1.csv --dataset_type wide

However, we may want validation set during fine-tuning to perform hyperparameter tuning or early stopping. To additionally split the dataset into a train and validation split we can use the mutually exclusive date_offset (datetime string) or offset (integer) options which determines the last time step of the train set. The validation set will be saved as DATASETNAMEeval. shell python -m uni2ts.data.builder.simple ETTh1 dataset/ETT-small/ETTh1.csv --date_offset '2017-10-23 23:00:00'

In some cases, we may want to normalize the data using the mean and std computed from the training dataset. This can be achieved by setting the --normalize argument.

shell python -m uni2ts.data.builder.simple ETTh1 dataset/ETT-small/ETTh1.csv --date_offset '2017-10-23 23:00:00' --normalize

  1. Finally, we can simply run the fine-tuning script with the appropriate training and validation data configuration files. Forecasting configurations such as patch size, context length and prediction length need to be specified by users. Since dataset_type is wide, data.mode is set to S for univariate setup. shell python -m cli.train \ -cp conf/finetune \ exp_name=example_lsf \ run_name=example_run \ model=moirai_1.0_R_small \ model.patch_size=32 \ model.context_length=1000 \ model.prediction_length=96 \ data=etth1 \ data.patch_size=32 \ data.context_length=1000 \ data.prediction_length=96 \ data.mode=S \ val_data=etth1 \ val_data.patch_size=32 \ val_data.context_length=1000 \ val_data.prediction_length=96 \ val_data.mode=S

Evaluation

The evaluation script can be used to calculate evaluation metrics such as MSE, MASE, CRPS, and so on (see the configuration file).

Given a test split (see previous section on processing datasets), we can run the following command to evaluate it: shell python -m cli.eval \ run_name=example_eval_1 \ model=moirai_1.0_R_small \ model.patch_size=32 \ model.context_length=1000 \ data=etth1_test

Alternatively, we provide access to popular datasets, and can be toggled via the data configurations. As an example, say we want to perform evaluation, again on the ETTh1 dataset from the popular Long Sequence Forecasting benchmark. We first need to download the pre-processed datasets and put them in the correct directory, by setting up the TSLib repository and following the instructions. Then, assign the dataset directory to the LSF_PATH environment variable: shell echo "LSF_PATH=PATH_TO_TSLIB/dataset" >> .env

Thereafter, simply run the following script with the predefined Hydra config file: shell python -m cli.eval \ run_name=example_eval_2 \ model=moirai_1.0_R_small \ model.patch_size=32 \ model.context_length=1000 \ data=lsf_test \ data.dataset_name=ETTh1 \ data.prediction_length=96

Pre-training

Now, let's see how you can pre-train your own model. We'll start with preparing the data for pre-training first, by downloading the Large-scale Open Time Series Archive (LOTSA data). Assuming you've already createed a .env file, run the following commands. shell huggingface-cli download Salesforce/lotsa_data --repo-type=dataset --local-dir PATH_TO_SAVE echo "LOTSA_V1_PATH=PATH_TO_SAVE" >> .env

Then, we can simply run the following script to start a pre-training job. See the relevant files on how to further customize the settings. shell python -m cli.train \ -cp conf/pretrain \ run_name=first_run \ model=moirai_small \ data=lotsa_v1_unweighted

👀 Citation

If you're using this repository in your research or applications, please cite using the following BibTeX:

```markdown @article{liu2024moiraimoe, title={Moirai-MoE: Empowering Time Series Foundation Models with Sparse Mixture of Experts}, author={Liu, Xu and Liu, Juncheng and Woo, Gerald and Aksu, Taha and Liang, Yuxuan and Zimmermann, Roger and Liu, Chenghao and Savarese, Silvio and Xiong, Caiming and Sahoo, Doyen}, journal={arXiv preprint arXiv:2410.10469}, year={2024} }

@article{aksu2024gifteval, title={GIFT-Eval: A Benchmark For General Time Series Forecasting Model Evaluation}, author={Aksu, Taha and Woo, Gerald and Liu, Juncheng and Liu, Xu and Liu, Chenghao and Savarese, Silvio and Xiong, Caiming and Sahoo, Doyen}, journal={arXiv preprint arXiv:2410.10393}, year={2024} }

@inproceedings{woo2024moirai, title={Unified Training of Universal Time Series Forecasting Transformers}, author={Woo, Gerald and Liu, Chenghao and Kumar, Akshat and Xiong, Caiming and Savarese, Silvio and Sahoo, Doyen}, booktitle={Forty-first International Conference on Machine Learning}, year={2024} } ```

Ethical Considerations

This release is for research purposes only in support of an academic paper. Our models, datasets, and code are not specifically designed or evaluated for all downstream purposes. We strongly recommend users evaluate and address potential concerns related to accuracy, safety, and fairness before deploying this model. We encourage users to consider the common limitations of AI, comply with applicable laws, and leverage best practices when selecting use cases, particularly for high-risk scenarios where errors or misuse could significantly impact people’s lives, rights, or safety. For further guidance on use cases, refer to our AUP and AI AUP.

Owner

  • Name: Salesforce AI Research
  • Login: SalesforceAIResearch
  • Kind: organization
  • Email: ospo@salesforce.com

Open Source projects released by Salesforce AI Research

Citation (CITATION.cff)

cff-version: 1.2.0
title: Unified Training of Universal Time Series Forecasting Transformers
message: If you find Moirai useful for your research, please consider citing the associated paper.
authors:
  - family-names: Woo
    given-names: Gerald
  - family-names: Liu
    given-names: Chenghao
  - family-names: Kumar
    given-names: Akshat
  - family-names: Xiong
    given-names: Caiming
  - family-names: Savarese
    given-names: Silvio
  - family-names: Sahoo
    given-names: Doyen
preferred-citation:
  type: conference-paper
  authors:
    - family-names: Woo
      given-names: Gerald
    - family-names: Liu
      given-names: Chenghao
    - family-names: Kumar
      given-names: Akshat
    - family-names: Xiong
      given-names: Caiming
    - family-names: Savarese
      given-names: Silvio
    - family-names: Sahoo
      given-names: Doyen
  title: Unified Training of Universal Time Series Forecasting Transformers
  year: 2024
  collection-title: Proceedings of the 41st International Conference on Machine Learning
  conference:
    name: International Conference on Machine Learning (ICML)
    city: Vienna
    country: Austria

GitHub Events

Total
  • Create event: 6
  • Release event: 1
  • Issues event: 78
  • Watch event: 385
  • Delete event: 5
  • Member event: 1
  • Issue comment event: 121
  • Push event: 28
  • Pull request review comment event: 1
  • Pull request review event: 14
  • Pull request event: 36
  • Fork event: 63
Last Year
  • Create event: 6
  • Release event: 1
  • Issues event: 78
  • Watch event: 385
  • Delete event: 5
  • Member event: 1
  • Issue comment event: 121
  • Push event: 28
  • Pull request review comment event: 1
  • Pull request review event: 14
  • Pull request event: 36
  • Fork event: 63

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 108
  • Total pull requests: 52
  • Average time to close issues: about 1 month
  • Average time to close pull requests: 7 days
  • Total issue authors: 77
  • Total pull request authors: 11
  • Average comments per issue: 2.77
  • Average comments per pull request: 0.73
  • Merged pull requests: 40
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 50
  • Pull requests: 27
  • Average time to close issues: about 1 month
  • Average time to close pull requests: 4 days
  • Issue authors: 39
  • Pull request authors: 9
  • Average comments per issue: 1.3
  • Average comments per pull request: 0.52
  • Merged pull requests: 17
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • liu-jc (4)
  • zqiao11 (4)
  • marcopeix (4)
  • HALF111 (3)
  • kaushikb258 (3)
  • bkoyuncu (3)
  • aleksmaksimovic (2)
  • yAoOw (2)
  • RongzhangCheng (2)
  • gwyanCN (2)
  • Hank0626 (2)
  • thisthq (2)
  • Mo-Abdelhameed (2)
  • fritol (2)
  • hahne0922 (2)
Pull Request Authors
  • gorold (32)
  • liu-jc (31)
  • zqiao11 (5)
  • liuxu77 (5)
  • Keytoyze (4)
  • bohdan-nd (4)
  • chenghaoliu89 (2)
  • HALF111 (2)
  • dmwyd (2)
  • sfdc-ospo-bot (2)
Top Labels
Issue Labels
bug (54) enhancement (2) question (1) documentation (1)
Pull Request Labels
cla:signed (17) cla:missing (6) enhancement (2) bug (2) documentation (2)

Dependencies

pyproject.toml pypi
  • datasets ~=2.17.1
  • einops ==0.7.*
  • gluonts ~=0.14.3
  • huggingface_hub *
  • hydra-core ==1.3
  • jax [cpu]
  • jaxtyping ~=0.2.24
  • lightning >=2.0
  • multiprocess *
  • numpy ~=1.26.0
  • orjson *
  • python-dotenv ==1.0.0
  • scipy ~=1.11.3
  • tensorboard *
  • torch >=2.0