Skip to content

Commit 7ba945c

Browse files
committedMar 13, 2024
Upload code
1 parent 2420c10 commit 7ba945c

File tree

11 files changed

+964
-6
lines changed

11 files changed

+964
-6
lines changed
 

‎.github/workflows/ci.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
strategy:
8+
max-parallel: 4
9+
fail-fast: false
10+
matrix:
11+
python-version: ['3.11']
12+
platform: [ubuntu-latest]
13+
14+
runs-on: ${{ matrix.platform }}
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
- name: Set up Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v4
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Install dependencies
23+
run: pip install ".[test]"
24+
- name: Test with pytest
25+
run: pytest

‎.gitignore

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
#.idea/
161+
162+
# macOS stuff
163+
.DS_store

‎README.md

+81-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,87 @@
1-
## My Project
1+
# Chronos: Learning the Language of Time Series
22

3-
TODO: Fill this README out!
3+
Chronos is a family of **pretrained time series forecasting models** based on language model architectures. A time series is transformed into a sequence of tokens via scaling and quantization, and a language model is trained on these tokens using the cross-entropy loss. Once trained, probabilistic forecasts are obtained by sampling multiple future trajectories given the historical context. Chronos models have been trained on a large corpus of publicly available time series data, as well as synthetic data generated using Gaussian processes.
44

5-
Be sure to:
5+
For details on Chronos models, training data and procedures, and experimental results, please refer to the paper [Chronos: Learning the Language of Time Series](https://arxiv.org/abs/2403.07815).
66

7-
* Change the title in this README
8-
* Edit your repository description on GitHub
7+
<p align="center">
8+
<img src="figures/main-figure.png" width="100%">
9+
<br />
10+
<span>
11+
Fig. 1: High-level depiction of Chronos. (<b>Left</b>) The input time series is scaled and quantized to obtain a sequence of tokens. (<b>Center</b>) The tokens are fed into a language model which may either be an encoder-decoder or a decoder-only model. The model is trained using the cross-entropy loss. (<b>Right</b>) During inference, we autoregressively sample tokens from the model and map them back to numerical values. Multiple trajectories are sampled to obtain a predictive distribution.
12+
</span>
13+
</p>
14+
15+
---
16+
17+
## Architecture
18+
19+
The models in this repository are based on the [T5 architecture](https://arxiv.org/abs/1910.10683). The only difference is in the vocabulary size: Chronos-T5 models use 4096 different tokens, compared to 32128 of the original T5 models, resulting in fewer parameters.
20+
21+
| Model | Parameters | Based on |
22+
| ---------------------------------------------------------------------- | ---------- | ---------------------------------------------------------------------- |
23+
| [**chronos-t5-tiny**](https://huggingface.co/amazon/chronos-t5-tiny) | 8M | [t5-efficient-tiny](https://huggingface.co/google/t5-efficient-tiny) |
24+
| [**chronos-t5-mini**](https://huggingface.co/amazon/chronos-t5-mini) | 20M | [t5-efficient-mini](https://huggingface.co/google/t5-efficient-mini) |
25+
| [**chronos-t5-small**](https://huggingface.co/amazon/chronos-t5-small) | 46M | [t5-efficient-small](https://huggingface.co/google/t5-efficient-small) |
26+
| [**chronos-t5-base**](https://huggingface.co/amazon/chronos-t5-base) | 200M | [t5-efficient-base](https://huggingface.co/google/t5-efficient-base) |
27+
| [**chronos-t5-large**](https://huggingface.co/amazon/chronos-t5-large) | 710M | [t5-efficient-large](https://huggingface.co/google/t5-efficient-large) |
28+
29+
## Usage
30+
31+
To perform inference with Chronos models, install this package by running:
32+
33+
```
34+
pip install git+https://github.com/amazon-science/chronos-forecasting.git
35+
```
36+
37+
A minimal example showing how to perform inference using Chronos models:
38+
39+
```python
40+
import matplotlib.pyplot as plt
41+
import numpy as np
42+
import pandas as pd
43+
import torch
44+
from chronos import ChronosPipeline
45+
46+
pipeline = ChronosPipeline.from_pretrained(
47+
"amazon/chronos-t5-small",
48+
device_map="cuda",
49+
torch_dtype=torch.bfloat16,
50+
)
51+
52+
df = pd.read_csv("https://raw.githubusercontent.com/AileenNielsen/TimeSeriesAnalysisWithPython/master/data/AirPassengers.csv")
53+
54+
# context must be either a 1D tensor, a list of 1D tensors,
55+
# or a left-padded 2D tensor with batch as the first dimension
56+
context = torch.tensor(df["#Passengers"])
57+
prediction_length = 12
58+
forecast = pipeline.predict(context, prediction_length) # shape [num_series, num_samples, prediction_length]
59+
60+
# visualize the forecast
61+
forecast_index = range(len(df), len(df) + prediction_length)
62+
low, median, high = np.quantile(forecast[0].numpy(), [0.1, 0.5, 0.9], axis=0)
63+
64+
plt.figure(figsize=(8, 4))
65+
plt.plot(df["#Passengers"], color="royalblue", label="historical data")
66+
plt.plot(forecast_index, median, color="tomato", label="median forecast")
67+
plt.fill_between(forecast_index, low, high, color="tomato", alpha=0.3, label="80% prediction interval")
68+
plt.legend()
69+
plt.grid()
70+
plt.show()
71+
```
72+
73+
## Citation
74+
75+
If you find Chronos models useful for your research, please consider citing the associated [paper](https://arxiv.org/abs/2403.07815):
76+
77+
```
78+
@article{ansari2024chronos,
79+
author = {Ansari, Abdul Fatir and Stella, Lorenzo and Turkmen, Caner and Zhang, Xiyuan, and Mercado, Pedro and Shen, Huibin and Shchur, Oleksandr and Rangapuram, Syama Syndar and Pineda Arango, Sebastian and Kapoor, Shubham and Zschiegner, Jasper and Maddix, Danielle C. and Mahoney, Michael W. and Torkkola, Kari and Gordon Wilson, Andrew and Bohlke-Schneider, Michael and Wang, Yuyang},
80+
title = {Chronos: Learning the Language of Time Series},
81+
journal = {arXiv preprint arXiv:2403.07815},
82+
year = {2024}
83+
}
84+
```
985

1086
## Security
1187

@@ -14,4 +90,3 @@ See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more inform
1490
## License
1591

1692
This project is licensed under the Apache-2.0 License.
17-

‎figures/main-figure.png

227 KB
Loading

‎pyproject.toml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[project]
2+
name = "chronos"
3+
version = "1.0.0"
4+
requires-python = ">=3.8"
5+
license = {file = "LICENSE"}
6+
dependencies = [
7+
"torch~=2.1", # package was tested on 2.2
8+
"transformers~=4.31",
9+
"accelerate"
10+
]
11+
12+
[project.optional-dependencies]
13+
test = [
14+
"pytest~=8.0",
15+
"numpy~=1.21"
16+
]
17+
18+
[tool.mypy]
19+
ignore_missing_imports = true

‎src/chronos/__init__.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from .chronos import (
5+
ChronosConfig,
6+
ChronosModel,
7+
ChronosPipeline,
8+
ChronosTokenizer,
9+
MeanScaleUniformBins,
10+
)
11+
12+
__all__ = [
13+
"ChronosConfig",
14+
"ChronosModel",
15+
"ChronosPipeline",
16+
"ChronosTokenizer",
17+
"MeanScaleUniformBins",
18+
]

0 commit comments

Comments
 (0)
Failed to load comments.