timecopilot-timesfm 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- timecopilot_timesfm-0.1.0/.gitignore +8 -0
- timecopilot_timesfm-0.1.0/LICENSE +202 -0
- timecopilot_timesfm-0.1.0/PKG-INFO +345 -0
- timecopilot_timesfm-0.1.0/README.md +311 -0
- timecopilot_timesfm-0.1.0/pyproject.toml +71 -0
- timecopilot_timesfm-0.1.0/src/finetuning/__init__.py +0 -0
- timecopilot_timesfm-0.1.0/src/finetuning/finetuning_example.py +403 -0
- timecopilot_timesfm-0.1.0/src/finetuning/finetuning_torch.py +399 -0
- timecopilot_timesfm-0.1.0/src/timesfm/__init__.py +30 -0
- timecopilot_timesfm-0.1.0/src/timesfm/data_loader.py +255 -0
- timecopilot_timesfm-0.1.0/src/timesfm/patched_decoder.py +543 -0
- timecopilot_timesfm-0.1.0/src/timesfm/pytorch_patched_decoder.py +801 -0
- timecopilot_timesfm-0.1.0/src/timesfm/time_features.py +215 -0
- timecopilot_timesfm-0.1.0/src/timesfm/timesfm_base.py +736 -0
- timecopilot_timesfm-0.1.0/src/timesfm/timesfm_jax.py +353 -0
- timecopilot_timesfm-0.1.0/src/timesfm/timesfm_torch.py +168 -0
- timecopilot_timesfm-0.1.0/src/timesfm/xreg_lib.py +486 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: timecopilot-timesfm
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Open weights time-series foundation model from Google Research.
|
|
5
|
+
Project-URL: Homepage, https://github.com/google-research/timesfm
|
|
6
|
+
Project-URL: Repository, https://github.com/google-research/timesfm
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: forecast,time series,time series model,timesfm
|
|
9
|
+
Classifier: Environment :: Console
|
|
10
|
+
Classifier: Framework :: Flake8
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Topic :: Software Development :: Documentation
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Requires-Dist: absl-py>=1.4.0
|
|
17
|
+
Requires-Dist: einshape>=1.0.0
|
|
18
|
+
Requires-Dist: huggingface-hub[cli]>=0.23.0
|
|
19
|
+
Requires-Dist: numpy>=1.26.4
|
|
20
|
+
Requires-Dist: pandas>=2.0.0
|
|
21
|
+
Requires-Dist: safetensors<0.6,>=0.5.3
|
|
22
|
+
Requires-Dist: scikit-learn>=1.2.2
|
|
23
|
+
Requires-Dist: torch>=2.0.0
|
|
24
|
+
Requires-Dist: typer>=0.12.3
|
|
25
|
+
Requires-Dist: utilsforecast>=0.1.10
|
|
26
|
+
Requires-Dist: wandb>=0.17.5
|
|
27
|
+
Provides-Extra: pax
|
|
28
|
+
Requires-Dist: jax[cuda12]>=0.4.26; (python_version >= '3.10' and python_version < '3.12') and extra == 'pax'
|
|
29
|
+
Requires-Dist: jaxlib>=0.4.26; (python_version >= '3.10' and python_version < '3.12') and extra == 'pax'
|
|
30
|
+
Requires-Dist: lingvo>=0.12.7; (python_version >= '3.10' and python_version < '3.11') and extra == 'pax'
|
|
31
|
+
Requires-Dist: paxml>=1.4.0; (python_version >= '3.10' and python_version < '3.11') and extra == 'pax'
|
|
32
|
+
Provides-Extra: torch
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
|
|
35
|
+
# TimesFM
|
|
36
|
+
|
|
37
|
+
TimesFM (Time Series Foundation Model) is a pretrained time-series foundation model developed by Google
|
|
38
|
+
Research for time-series forecasting.
|
|
39
|
+
|
|
40
|
+
* Paper: [A decoder-only foundation model for time-series forecasting](https://arxiv.org/abs/2310.10688), to appear in ICML 2024.
|
|
41
|
+
* [Google Research blog](https://research.google/blog/a-decoder-only-foundation-model-for-time-series-forecasting/)
|
|
42
|
+
* [Hugging Face release](https://huggingface.co/collections/google/timesfm-release-66e4be5fdb56e960c1e482a6)
|
|
43
|
+
|
|
44
|
+
This repo contains the code to load public TimesFM checkpoints and run model
|
|
45
|
+
inference. Please visit our
|
|
46
|
+
[Hugging Face release](https://huggingface.co/collections/google/timesfm-release-66e4be5fdb56e960c1e482a6)
|
|
47
|
+
to download model checkpoints.
|
|
48
|
+
|
|
49
|
+
This is not an officially supported Google product.
|
|
50
|
+
|
|
51
|
+
We recommend at least 32GB RAM to load TimesFM dependencies.
|
|
52
|
+
|
|
53
|
+
## Update - Dec. 30, 2024
|
|
54
|
+
- We are launching a 500m checkpoint as a part of TimesFM-2.0 release. This new checkpoint can be upto 25% better than v1.0 on leading benchmarks and also has a 4 times longer max. context length.
|
|
55
|
+
- Launched [finetuning support](https://github.com/google-research/timesfm/blob/master/notebooks/finetuning.ipynb) that lets you finetune the weights of the pretrained TimesFM model on your own data.
|
|
56
|
+
- Launched [~zero-shot covariate support](https://github.com/google-research/timesfm/blob/master/notebooks/covariates.ipynb) with external regressors. More details [here](https://github.com/google-research/timesfm?tab=readme-ov-file#covariates-support).
|
|
57
|
+
|
|
58
|
+
## Update - Feb. 17, 2024
|
|
59
|
+
- We are providing the option for [finetuning using Pytorch](https://github.com/google-research/timesfm/blob/master/notebooks/finetuning_torch.ipynb), which mimics the previously added functionality from [finetuning support](https://github.com/google-research/timesfm/blob/master/notebooks/finetuning.ipynb).
|
|
60
|
+
- We are also providing the Multi-GPU finetuining with Pytorch. We currently support DDP multi-gpu finetuning, other variants of multi-gpu training (pipeline parallelism/model parallelism) might be added later. In order to use it, follow the steps in [finetuning example](https://github.com/google-research/timesfm/blob/master/finetuning/finetuning_example.py) .
|
|
61
|
+
|
|
62
|
+
## Checkpoint timesfm-1.0-200m (-pytorch)
|
|
63
|
+
|
|
64
|
+
timesfm-1.0-200m is our first open model checkpoint:
|
|
65
|
+
|
|
66
|
+
- It performs univariate time series forecasting for context lengths up to 512 timepoints and any horizon lengths, with an optional frequency indicator.
|
|
67
|
+
- It focuses on point forecasts, and does not support probabilistic forecasts. We experimentally offer quantile heads but they have not been calibrated after pretraining.
|
|
68
|
+
|
|
69
|
+
## Checkpoint timesfm-2.0-500m (-jax/-pytorch)
|
|
70
|
+
|
|
71
|
+
timesfm-2.0-500m is our second open model checkpoint:
|
|
72
|
+
|
|
73
|
+
- It performs univariate time series forecasting for context lengths up to 2048 timepoints and any horizon lengths, with an optional frequency indicator.
|
|
74
|
+
- It focuses on point forecasts. We experimentally offer 10 quantile heads but they have not been calibrated after pretraining.
|
|
75
|
+
- This new checkpoint can be upto 25% better than v1.0 on leading benchmarks and also has a 4 times longer max. context length.
|
|
76
|
+
|
|
77
|
+
## Benchmarking
|
|
78
|
+
|
|
79
|
+
TimesFM 2.0 has been added to [GIFT-Eval](https://huggingface.co/spaces/Salesforce/GIFT-Eval) which is one of the most comprehensive time-series bechmarks available. It takes the top spot in terms of aggregated MASE and CRPS, where it is 6\% better than the next best model in terms of aggregated MASE.
|
|
80
|
+
|
|
81
|
+
## Installation
|
|
82
|
+
|
|
83
|
+
### Local installation using poetry
|
|
84
|
+
|
|
85
|
+
We will be using `pyenv` and `poetry`. In order to set these things up please follow the instructions [here](https://substack.com/home/post/p-148747960?r=28a5lx&utm_campaign=post&utm_medium=web). Note that the PAX (or JAX) version needs to run on python 3.10.x and the PyTorch version can run on >=3.11.x. Therefore make sure you have two versions of python installed:
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
pyenv install 3.10
|
|
89
|
+
pyenv install 3.11
|
|
90
|
+
pyenv versions # to list the versions available (lets assume the versions are 3.10.15 and 3.11.10)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### For PAX version installation do the following.
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
pyenv local 3.10.15
|
|
97
|
+
poetry env use 3.10.15
|
|
98
|
+
poetry lock
|
|
99
|
+
poetry install -E pax
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
After than you can run the timesfm under `poetry shell` or do `poetry run python3 ...`.
|
|
103
|
+
|
|
104
|
+
### For PyTorch version installation do the following.
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
pyenv local 3.11.10
|
|
108
|
+
poetry env use 3.11.10
|
|
109
|
+
poetry lock
|
|
110
|
+
poetry install -E torch
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
After than you can run the timesfm under `poetry shell` or do `poetry run python3 ...`.
|
|
114
|
+
|
|
115
|
+
**Additional Note**:
|
|
116
|
+
|
|
117
|
+
If you plan to use the **`forecast_with_covariates`** function (which requires external regressors),
|
|
118
|
+
you need to install **JAX** and **jaxlib**. If you installed the base version of TimesFM (`torch`), you must manually install the dependencies for **`forecast_with_covariates`** support:
|
|
119
|
+
```
|
|
120
|
+
pip install jax jaxlib
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**Why is this needed?**
|
|
124
|
+
The `forecast_with_covariates` method relies on the `xreg_lib` module, which depends on JAX and jaxlib. If these packages are not installed,
|
|
125
|
+
calling `forecast_with_covariates` will raise an error. However, due to a lazy import mechanism, `xreg_lib` (and hence JAX/jaxlib) is not needed for standard `forecast` calls.
|
|
126
|
+
|
|
127
|
+
### Notes
|
|
128
|
+
|
|
129
|
+
1. Running the provided benchmarks would require additional dependencies. Please see the `experiments` folder.
|
|
130
|
+
|
|
131
|
+
2. The dependency `lingvo` does not support ARM architectures, and the code is not working for machines with Apple silicon. We are aware of this issue and are working on a solution. Stay tuned.
|
|
132
|
+
|
|
133
|
+
### Install from PyPI (and publish)
|
|
134
|
+
|
|
135
|
+
On python 3.11 you can install the torch version using:
|
|
136
|
+
|
|
137
|
+
```pip install timesfm[torch]```
|
|
138
|
+
|
|
139
|
+
On python 3.10 you can install the pax version using:
|
|
140
|
+
|
|
141
|
+
```pip install timesfm[pax]```
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
## Usage
|
|
145
|
+
|
|
146
|
+
### Initialize the model and load a checkpoint.
|
|
147
|
+
Then the base class can be loaded as,
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
import timesfm
|
|
151
|
+
|
|
152
|
+
# Loading the timesfm-2.0 checkpoint:
|
|
153
|
+
# For PAX
|
|
154
|
+
tfm = timesfm.TimesFm(
|
|
155
|
+
hparams=timesfm.TimesFmHparams(
|
|
156
|
+
backend="gpu",
|
|
157
|
+
per_core_batch_size=32,
|
|
158
|
+
horizon_len=128,
|
|
159
|
+
num_layers=50,
|
|
160
|
+
context_len=2048,
|
|
161
|
+
|
|
162
|
+
use_positional_embedding=False,
|
|
163
|
+
),
|
|
164
|
+
checkpoint=timesfm.TimesFmCheckpoint(
|
|
165
|
+
huggingface_repo_id="google/timesfm-2.0-500m-jax"),
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
# For Torch
|
|
169
|
+
tfm = timesfm.TimesFm(
|
|
170
|
+
hparams=timesfm.TimesFmHparams(
|
|
171
|
+
backend="gpu",
|
|
172
|
+
per_core_batch_size=32,
|
|
173
|
+
horizon_len=128,
|
|
174
|
+
num_layers=50,
|
|
175
|
+
use_positional_embedding=False,
|
|
176
|
+
context_len=2048,
|
|
177
|
+
),
|
|
178
|
+
checkpoint=timesfm.TimesFmCheckpoint(
|
|
179
|
+
huggingface_repo_id="google/timesfm-2.0-500m-pytorch"),
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
# Loading the timesfm-1.0 checkpoint:
|
|
183
|
+
# For PAX
|
|
184
|
+
tfm = timesfm.TimesFm(
|
|
185
|
+
hparams=timesfm.TimesFmHparams(
|
|
186
|
+
backend="gpu",
|
|
187
|
+
per_core_batch_size=32,
|
|
188
|
+
horizon_len=128,
|
|
189
|
+
),
|
|
190
|
+
checkpoint=timesfm.TimesFmCheckpoint(
|
|
191
|
+
huggingface_repo_id="google/timesfm-1.0-200m"),
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
# For Torch
|
|
195
|
+
tfm = timesfm.TimesFm(
|
|
196
|
+
hparams=timesfm.TimesFmHparams(
|
|
197
|
+
backend="gpu",
|
|
198
|
+
per_core_batch_size=32,
|
|
199
|
+
horizon_len=128,
|
|
200
|
+
),
|
|
201
|
+
checkpoint=timesfm.TimesFmCheckpoint(
|
|
202
|
+
huggingface_repo_id="google/timesfm-1.0-200m-pytorch"),
|
|
203
|
+
)
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Note some of the parameters are fixed to load the 200m and 500m models
|
|
207
|
+
|
|
208
|
+
1. The `context_len` in `hparams` here can be set as the max context length **of the model** (a maximum of 2048 for 2.0 models and 512 for 1.0 models). **It needs to be a multiplier of `input_patch_len`, i.e. a multiplier of 32.** You can provide a shorter series to the `tfm.forecast()` function and the model will handle it. The input time series can have **any context length**. Padding / truncation will be handled by the inference code if needed.
|
|
209
|
+
|
|
210
|
+
2. The horizon length can be set to anything. We recommend setting it to the largest horizon length you would need in the forecasting tasks for your application. We generally recommend horizon length <= context length but it is not a requirement in the function call.
|
|
211
|
+
|
|
212
|
+
3. `backend` is one of "cpu", "gpu", case sensitive.
|
|
213
|
+
|
|
214
|
+
### Perform inference
|
|
215
|
+
|
|
216
|
+
We provide APIs to forecast from either array inputs or `pandas` dataframe. Both forecast methods expect (1) the input time series contexts, (2) along with their frequencies. Please look at the documentation of the functions `tfm.forecast()` and `tfm.forecast_on_df()` for detailed instructions.
|
|
217
|
+
|
|
218
|
+
In particular regarding the frequency, TimesFM expects a categorical indicator valued in {0, 1, 2}:
|
|
219
|
+
|
|
220
|
+
- **0** (default): high frequency, long horizon time series. We recommend using this for time series up to daily granularity.
|
|
221
|
+
- **1**: medium frequency time series. We recommend using this for weekly and monthly data.
|
|
222
|
+
- **2**: low frequency, short horizon time series. We recommend using this for anything beyond monthly, e.g. quarterly or yearly.
|
|
223
|
+
|
|
224
|
+
This categorical value should be directly provided with the array inputs. For dataframe inputs, we convert the conventional letter coding of frequencies to our expected categories, that
|
|
225
|
+
|
|
226
|
+
- **0**: T, MIN, H, D, B, U
|
|
227
|
+
- **1**: W, M
|
|
228
|
+
- **2**: Q, Y
|
|
229
|
+
|
|
230
|
+
Notice you do **NOT** have to strictly follow our recommendation here. Although this is our setup during model training and we expect it to offer the best forecast result, you can also view the frequency input as a free parameter and modify it per your specific use case.
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
Examples:
|
|
234
|
+
|
|
235
|
+
Array inputs, with the frequencies set to low, medium and high respectively.
|
|
236
|
+
|
|
237
|
+
```python
|
|
238
|
+
import numpy as np
|
|
239
|
+
forecast_input = [
|
|
240
|
+
np.sin(np.linspace(0, 20, 100)),
|
|
241
|
+
np.sin(np.linspace(0, 20, 200)),
|
|
242
|
+
np.sin(np.linspace(0, 20, 400)),
|
|
243
|
+
]
|
|
244
|
+
frequency_input = [0, 1, 2]
|
|
245
|
+
|
|
246
|
+
point_forecast, experimental_quantile_forecast = tfm.forecast(
|
|
247
|
+
forecast_input,
|
|
248
|
+
freq=frequency_input,
|
|
249
|
+
)
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
`pandas` dataframe, with the frequency set to "M" monthly.
|
|
253
|
+
|
|
254
|
+
```python
|
|
255
|
+
import pandas as pd
|
|
256
|
+
|
|
257
|
+
# e.g. input_df is
|
|
258
|
+
# unique_id ds y
|
|
259
|
+
# 0 T1 1975-12-31 697458.0
|
|
260
|
+
# 1 T1 1976-01-31 1187650.0
|
|
261
|
+
# 2 T1 1976-02-29 1069690.0
|
|
262
|
+
# 3 T1 1976-03-31 1078430.0
|
|
263
|
+
# 4 T1 1976-04-30 1059910.0
|
|
264
|
+
# ... ... ... ...
|
|
265
|
+
# 8175 T99 1986-01-31 602.0
|
|
266
|
+
# 8176 T99 1986-02-28 684.0
|
|
267
|
+
# 8177 T99 1986-03-31 818.0
|
|
268
|
+
# 8178 T99 1986-04-30 836.0
|
|
269
|
+
# 8179 T99 1986-05-31 878.0
|
|
270
|
+
|
|
271
|
+
forecast_df = tfm.forecast_on_df(
|
|
272
|
+
inputs=input_df,
|
|
273
|
+
freq="M", # monthly
|
|
274
|
+
value_name="y",
|
|
275
|
+
num_jobs=-1,
|
|
276
|
+
)
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
## Covariates Support
|
|
280
|
+
|
|
281
|
+
We now have an external regressors library on top of TimesFM that can support static covariates as well as dynamic covariates available in the future. We have an usage example in [notebooks/covariates.ipynb](https://github.com/google-research/timesfm/blob/master/notebooks/covariates.ipynb).
|
|
282
|
+
|
|
283
|
+
If you plan to use the **`forecast_with_covariates`** on timesfm `torch` version, you need to install **JAX** and **jaxlib**.
|
|
284
|
+
You must manually install the dependencies for **`forecast_with_covariates`** support:
|
|
285
|
+
```
|
|
286
|
+
pip install jax jaxlib
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
Let's take a toy example of forecasting sales for a grocery store:
|
|
290
|
+
|
|
291
|
+
**Task:** Given the observed the daily sales of this week (7 days), forecast the daily sales of next week (7 days).
|
|
292
|
+
|
|
293
|
+
```
|
|
294
|
+
Product: ice cream
|
|
295
|
+
Daily_sales: [30, 30, 4, 5, 7, 8, 10]
|
|
296
|
+
Category: food
|
|
297
|
+
Base_price: 1.99
|
|
298
|
+
Weekday: [0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6]
|
|
299
|
+
Has_promotion: [Yes, Yes, No, No, No, Yes, Yes, No, No, No, No, No, No, No]
|
|
300
|
+
Daily_temperature: [31.0, 24.3, 19.4, 26.2, 24.6, 30.0, 31.1, 32.4, 30.9, 26.0, 25.0, 27.8, 29.5, 31.2]
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
```
|
|
304
|
+
Product: sunscreen
|
|
305
|
+
Daily_sales: [5, 7, 12, 13, 5, 6, 10]
|
|
306
|
+
Category: skin product
|
|
307
|
+
Base_price: 29.99
|
|
308
|
+
Weekday: [0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6]
|
|
309
|
+
Has_promotion: [No, No, Yes, Yes, No, No, No, Yes, Yes, Yes, Yes, Yes, Yes, Yes]
|
|
310
|
+
Daily_temperature: [31.0, 24.3, 19.4, 26.2, 24.6, 30.0, 31.1, 32.4, 30.9, 26.0, 25.0, 27.8, 29.5, 31.2]
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
In this example, besides the `Daily_sales`, we also have covariates `Category`, `Base_price`, `Weekday`, `Has_promotion`, `Daily_temperature`. Let's introduce some concepts:
|
|
314
|
+
|
|
315
|
+
**Static covariates** are covariates for each time series.
|
|
316
|
+
- In our example, `Category` is a **static categorical covariate**,
|
|
317
|
+
- `Base_price` is a **static numerical covariates**.
|
|
318
|
+
|
|
319
|
+
**Dynamic covariates** are covaraites for each time stamps.
|
|
320
|
+
- Date / time related features can be usually treated as dynamic covariates.
|
|
321
|
+
- In our example, `Weekday` and `Has_promotion` are **dynamic categorical covariates**.
|
|
322
|
+
- `Daily_temperate` is a **dynamic numerical covariate**.
|
|
323
|
+
|
|
324
|
+
**Notice:** Here we make it mandatory that the dynamic covariates need to cover both the forecasting context and horizon. For example, all dynamic covariates in the example have 14 values: the first 7 correspond to the observed 7 days, and the last 7 correspond to the next 7 days.
|
|
325
|
+
|
|
326
|
+
We can now provide the past data of the two products along with static and dynamic covariates as a batch input to TimesFM and produce forecasts that take into the account the covariates. To learn more, check out the example in [notebooks/covariates.ipynb](https://github.com/google-research/timesfm/blob/master/notebooks/covariates.ipynb).
|
|
327
|
+
|
|
328
|
+
## Finetuning
|
|
329
|
+
|
|
330
|
+
We have provided an example of finetuning the model on a new dataset in [notebooks/finetuning.ipynb](https://github.com/google-research/timesfm/blob/master/notebooks/finetuning.ipynb).
|
|
331
|
+
|
|
332
|
+
## Contribution Style guide
|
|
333
|
+
|
|
334
|
+
If you would like to submit a PR please make sure that you use our formatting style. We use [yapf](https://github.com/google/yapf) for formatting with the following options,
|
|
335
|
+
|
|
336
|
+
```
|
|
337
|
+
[style]
|
|
338
|
+
based_on_style = google
|
|
339
|
+
# Add your custom style rules here
|
|
340
|
+
indent_width = 2
|
|
341
|
+
spaces_before_comment = 2
|
|
342
|
+
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
Please run `yapf --in-place --recursive <filename>` on all affected files.
|