Commit 40d49fc2 authored by suily's avatar suily
Browse files

第一次写入README

parent 2762cabc
---
license: apache-2.0
library_name: timesfm
pipeline_tag: time-series-forecasting
---
# TimesFM # TimesFM
## 论文
TimesFM (Time Series Foundation Model) is a pretrained time-series foundation model developed by Google Research for time-series forecasting. `A decoder-only foundation model for time-series forecasting`
- https://arxiv.org/abs/2310.10688
**Resources and Technical Documentation**: ## 模型结构
TimesFM是一种基于区块的decoder-only的模型,基于自注意力机制和传统的位置编码,主要由三个主要组件组成:输入层、Transformer层和输出层
* Paper: [A decoder-only foundation model for time-series forecasting](https://arxiv.org/abs/2310.10688), to appear in ICML 2024.
* [Google Research blog](https://research.google/blog/a-decoder-only-foundation-model-for-time-series-forecasting/) <div align=center>
* [GitHub repo](https://github.com/google-research/timesfm) <img src="./doc/timesfm.png"/>
</div>
**Authors**: Google Research
## 算法原理
This is not an officially supported Google product. 输入层:将时间序列数据分割成相等长度的时序数据块(patch),然后通过残差块对每个时序数据块进行线性变化,进而得到Token。
Transformer层:应用了位置编码和自注意力机制。位置编码将时间信息注入Token(令牌)序列;自注意力允许模型学习序列中不同标记之间的依赖关系和关系:位置编码介入自注意力的构造意味着模型可以适应数据中不同的时间粒度和频率。
## Checkpoint timesfm-1.0-200m 输出层:使用层归一化和残差连接,将输出Token映射到最终预测。
TimesFM在真实世界的大型时间序列语料库上进行了预训练,可以为未见过的数据集生成可变长度的预测。
`timesfm-1.0-200m` is the first open model checkpoint:
<div align=center>
- It performs univariate time series forecasting for context lengths up to 512 time points and any horizon lengths, with an optional frequency indicator. <img src="./doc/timesfm.png"/>
- It focuses on point forecasts and does not support probabilistic forecasts. We experimentally offer quantile heads but they have not been calibrated after pretraining. </div>
- It requires the context to be contiguous (i.e. no "holes"), and the context and the horizon to be of the same frequency.
## 环境配置
## Benchmarks -v 路径、docker_name和imageID根据实际情况修改
### Docker(方法一)
Please refer to our result tables on the [extended benchmarks](https://github.com/google-research/timesfm/blob/master/experiments/extended_benchmarks/tfm_results.png) and the [long horizon benchmarks](https://github.com/google-research/timesfm/blob/master/experiments/long_horizon_benchmarks/tfm_long_horizon.png).
Please look into the README files in the respective benchmark directories within `experiments/` for instructions for running TimesFM on the respective benchmarks.
## Installation
This HuggingFace repo hosts TimesFm checkpoints. Please visit our [GitHub repo](https://github.com/google-research/timesfm) and follow the instructions there to install the `timesfm` library for model inference.
In particular, the dependency `lingvo` does not support ARM architectures and the inference code is not working for machines with Apple silicon. We are aware of this issue and are working on a solution. Stay tuned.
## Usage
### Initialize the model and load a checkpoint.
Then the base class can be loaded as,
```python
import timesfm
tfm = timesfm.TimesFm(
context_len=<context>,
horizon_len=<horizon>,
input_patch_len=32,
output_patch_len=128,
num_layers=20,
model_dims=1280,
backend=<backend>,
)
tfm.load_from_checkpoint(repo_id="google/timesfm-1.0-200m")
``` ```
docker pull image.sourcefind.cn:5000/dcu/admin/base/jax:0.4.23-ubuntu20.04-dtk24.04-py310
docker run -it --network=host --privileged=true --name=docker_name --device=/dev/kfd --device=/dev/dri --group-add video --shm-size=32G --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -v /path/your_code_data/:/path/your_code_data/ imageID /bin/bash
Note that the four parameters are fixed to load the 200m model cd /your_code_path/timesfm
pip install -r requirements.txt
```python pip install tensorflow-2.13.1+das1.0+git429d21b.abi1.dtk2404-cp310-cp310-manylinux2014_x86_64.whl
input_patch_len=32,
output_patch_len=128,
num_layers=20,
model_dims=1280,
``` ```
### Dockerfile(方法二)
```
cd ./docker
docker build --no-cache -t timesfm:latest .
docker run -it --network=host --privileged=true --name=docker_name --device=/dev/kfd --device=/dev/dri --group-add video --shm-size=32G --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -v /path/your_code_data/:/path/your_code_data/ imageID /bin/bash
1. The context_len here can be set as the max context length **of the model**. You can provide a shorter series to the `tfm.forecast()` function and the model will handle it. Currently, the model handles a max context length of 512, which can be increased in later releases. The input time series can have **any context length**. Padding / truncation will be handled by the inference code if needed. cd /your_code_path/timesfm
pip install -r requirements.txt
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. pip install tensorflow-2.13.1+das1.0+git429d21b.abi1.dtk2404-cp310-cp310-manylinux2014_x86_64.whl
```
### Perform inference ### Anaconda(方法三)
1、关于本项目DCU显卡所需的特殊深度学习库可从光合开发者社区下载安装: https://developer.hpccube.com/tool/
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. ```
DTK软件栈:dtk24.04
In particular, regarding the frequency, TimesFM expects a categorical indicator valued in {0, 1, 2}: python:python3.10
jax:0.4.23
- **0** (default): high frequency, long horizon time series. We recommend using this for time series up to daily granularity. tensorflow:2.13.1
- **1**: medium frequency time series. We recommend using this for weekly and monthly data.
- **2**: low frequency, short horizon time series. We recommend using this for anything beyond monthly, e.g. quarterly or yearly.
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
- **0**: T, MIN, H, D, B, U
- **1**: W, M
- **2**: Q, Y
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.
Examples:
Array inputs, with the frequencies set to low, medium, and high respectively.
```python
import numpy as np
forecast_input = [
np.sin(np.linspace(0, 20, 100))
np.sin(np.linspace(0, 20, 200)),
np.sin(np.linspace(0, 20, 400)),
]
frequency_input = [0, 1, 2]
point_forecast, experimental_quantile_forecast = tfm.forecast(
forecast_input,
freq=frequency_input,
)
``` ```
`Tips:以上dtk软件栈、python、jax等DCU相关工具版本需要严格一一对应`
`pandas` dataframe, with the frequency set to "M" monthly. 2、其他非特殊库直接按照下面步骤进行安装
```
cd /your_code_path/timesfm
pip install -r requirements.txt
```
## 数据集
基准测试数据集运行时会gluonts自动下载,长期基准测试数据集需从Google Drive手动下载(需要魔法):
- https://drive.google.com/file/d/1alE33S1GmP5wACMXaLu50rDIoVzBM4ik/view?usp=share_link
下载完成后,将数据解压到datasets目录下,若有自订目录需求,可修改timesfm/experiments/long_horizon_benchmarks/run_eval.py:
```
DATA_DICT = {
"ettm2": {
"boundaries": [34560, 46080, 57600],
"data_path": "./datasets/ETT-small/ETTm2.csv", # 修改数据集存放路径
"freq": "15min",
},
...
}
```
长期基准测试数据集目录结构如下:
```
── datasets
│   ├── electricity
│    ├── electricity.csv
│   ├── ETT-small
│    ├── ETTh1.csv
│    ├── ETTh2.csv
│    ├── ETTh1.csv
│ └── ETTm1.csv
│   ├── exchange_rate
│ └── exchange_rate.csv
│   ├── illness
│ └── national illness.csv
│   ├── traffic
│ └── traffic.csv
│   └── weather
│ └── weather.csv
```
## 训练
官方暂未开放
## 推理
检查点可通过以下方式进行下载:
-https://hf-mirror.com/google/timesfm-1.0-200m
```
# "model"是自定义目录,可自订
1、通过git
cd timesfm
git clone https://hf-mirror.com/google/timesfm-1.0-200m model
2、通过huggingface-cli
cd timesfm
export HF_DATASETS_CACHE="/home/suily/timesfm/model"
export HF_ENDPOINT=https://hf-mirror.com # 设置下载地址
huggingface-cli download --resume-download google/timesfm-1.0-200m --local-dir model
```
```
sh train.sh
# 由于基准测试未直接提供调用数据集的接口,须在代码内部手动进行更改:
# 修改timesfm/experiments/extended_benchmarks/run_timesfm.py:dataset_names内填入所需数据集name
```
## result
此处填算法效果测试图(包括输入、输出)
```python <div align=center>
import pandas as pd <img src="./doc/xxx.png"/>
</div>
# e.g. input_df is ### 精度
# unique_id ds y k100和A800精度相差不到0.01%
# 0 T1 1975-12-31 697458.0
# 1 T1 1976-01-31 1187650.0
# 2 T1 1976-02-29 1069690.0
# 3 T1 1976-03-31 1078430.0
# 4 T1 1976-04-30 1059910.0
# ... ... ... ...
# 8175 T99 1986-01-31 602.0
# 8176 T99 1986-02-28 684.0
# 8177 T99 1986-03-31 818.0
# 8178 T99 1986-04-30 836.0
# 8179 T99 1986-05-31 878.0
forecast_df = tfm.forecast_on_df( 测试数据:
inputs=input_df, ```
freq="M", # monthly 1、基准测试:
value_name="y", "ett_small_15min",
num_jobs=-1, "traffic",
) "m3_quarterly",
"m3_yearly",
"tourism_yearly"
2、长期基准测试:
"etth1",
"ettm1"
``` ```
根据测试结果情况填写表格:
| xxx | xxx | xxx | xxx | xxx |
| :------: | :------: | :------: | :------: |:------: |
| xxx | xxx | xxx | xxx | xxx |
| xxx | xx | xxx | xxx | xxx |
## 应用场景
### 算法类别
`时序预测`
### 热点应用行业
`交通,零售,金融,气象`
## 源码仓库及问题反馈
- https://developer.hpccube.com/codes/modelzoo/timesfm_jax
## 参考资料
- https://github.com/google-research/timesfm
---
license: apache-2.0
library_name: timesfm
pipeline_tag: time-series-forecasting
---
# TimesFM
TimesFM (Time Series Foundation Model) is a pretrained time-series foundation model developed by Google Research for time-series forecasting.
**Resources and Technical Documentation**:
* Paper: [A decoder-only foundation model for time-series forecasting](https://arxiv.org/abs/2310.10688), to appear in ICML 2024.
* [Google Research blog](https://research.google/blog/a-decoder-only-foundation-model-for-time-series-forecasting/)
* [GitHub repo](https://github.com/google-research/timesfm)
**Authors**: Google Research
This is not an officially supported Google product.
## Checkpoint timesfm-1.0-200m
`timesfm-1.0-200m` is the first open model checkpoint:
- It performs univariate time series forecasting for context lengths up to 512 time points and any horizon lengths, with an optional frequency indicator.
- It focuses on point forecasts and does not support probabilistic forecasts. We experimentally offer quantile heads but they have not been calibrated after pretraining.
- It requires the context to be contiguous (i.e. no "holes"), and the context and the horizon to be of the same frequency.
## Benchmarks
Please refer to our result tables on the [extended benchmarks](https://github.com/google-research/timesfm/blob/master/experiments/extended_benchmarks/tfm_results.png) and the [long horizon benchmarks](https://github.com/google-research/timesfm/blob/master/experiments/long_horizon_benchmarks/tfm_long_horizon.png).
Please look into the README files in the respective benchmark directories within `experiments/` for instructions for running TimesFM on the respective benchmarks.
## Installation
This HuggingFace repo hosts TimesFm checkpoints. Please visit our [GitHub repo](https://github.com/google-research/timesfm) and follow the instructions there to install the `timesfm` library for model inference.
In particular, the dependency `lingvo` does not support ARM architectures and the inference code is not working for machines with Apple silicon. We are aware of this issue and are working on a solution. Stay tuned.
## Usage
### Initialize the model and load a checkpoint.
Then the base class can be loaded as,
```python
import timesfm
tfm = timesfm.TimesFm(
context_len=<context>,
horizon_len=<horizon>,
input_patch_len=32,
output_patch_len=128,
num_layers=20,
model_dims=1280,
backend=<backend>,
)
tfm.load_from_checkpoint(repo_id="google/timesfm-1.0-200m")
```
Note that the four parameters are fixed to load the 200m model
```python
input_patch_len=32,
output_patch_len=128,
num_layers=20,
model_dims=1280,
```
1. The context_len here can be set as the max context length **of the model**. You can provide a shorter series to the `tfm.forecast()` function and the model will handle it. Currently, the model handles a max context length of 512, which can be increased in later releases. The input time series can have **any context length**. Padding / truncation will be handled by the inference code if needed.
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.
### Perform inference
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.
In particular, regarding the frequency, TimesFM expects a categorical indicator valued in {0, 1, 2}:
- **0** (default): high frequency, long horizon time series. We recommend using this for time series up to daily granularity.
- **1**: medium frequency time series. We recommend using this for weekly and monthly data.
- **2**: low frequency, short horizon time series. We recommend using this for anything beyond monthly, e.g. quarterly or yearly.
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
- **0**: T, MIN, H, D, B, U
- **1**: W, M
- **2**: Q, Y
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.
Examples:
Array inputs, with the frequencies set to low, medium, and high respectively.
```python
import numpy as np
forecast_input = [
np.sin(np.linspace(0, 20, 100))
np.sin(np.linspace(0, 20, 200)),
np.sin(np.linspace(0, 20, 400)),
]
frequency_input = [0, 1, 2]
point_forecast, experimental_quantile_forecast = tfm.forecast(
forecast_input,
freq=frequency_input,
)
```
`pandas` dataframe, with the frequency set to "M" monthly.
```python
import pandas as pd
# e.g. input_df is
# unique_id ds y
# 0 T1 1975-12-31 697458.0
# 1 T1 1976-01-31 1187650.0
# 2 T1 1976-02-29 1069690.0
# 3 T1 1976-03-31 1078430.0
# 4 T1 1976-04-30 1059910.0
# ... ... ... ...
# 8175 T99 1986-01-31 602.0
# 8176 T99 1986-02-28 684.0
# 8177 T99 1986-03-31 818.0
# 8178 T99 1986-04-30 836.0
# 8179 T99 1986-05-31 878.0
forecast_df = tfm.forecast_on_df(
inputs=input_df,
freq="M", # monthly
value_name="y",
num_jobs=-1,
)
```
\ No newline at end of file
...@@ -30,9 +30,9 @@ from jax.lib import xla_bridge ...@@ -30,9 +30,9 @@ from jax.lib import xla_bridge
# sugon测试 # sugon测试
dataset_names = [ # context_len=512 dataset_names = [ # context_len=512
"ett_small_15min", "ett_small_15min",
# "traffic", "traffic",
# "m3_quarterly", "m3_quarterly",
# "m3_yearly", "m3_yearly",
"tourism_yearly", "tourism_yearly",
] ]
......
...@@ -3,8 +3,8 @@ modelCode = 850 ...@@ -3,8 +3,8 @@ modelCode = 850
# 模型名称 # 模型名称
modelName=timesfm_jax modelName=timesfm_jax
# 模型描述 # 模型描述
modelDescription=google开发的一种预训练时间序列基础模型 modelDescription=timesfm是一个预训练时间序列基础模型,具有一定的out-of-the-box、zero-shot性能
# 应用场景 # 应用场景
appScenario=推理 appScenario=推理,时序预测,交通,零售,金融,气象
# 框架类型 # 框架类型
frameType=jax frameType=jax,tensorflow
...@@ -7,19 +7,19 @@ python3 -m experiments.extended_benchmarks.run_timesfm \ ...@@ -7,19 +7,19 @@ python3 -m experiments.extended_benchmarks.run_timesfm \
--model_path="model/checkpoints" \ --model_path="model/checkpoints" \
--backend="gpu" --backend="gpu"
# python -c "print('finish run_timesfm!!!!')" python -c "print('finish run_timesfm!!!!')"
# for dataset in etth1 ettm1 for dataset in etth1 ettm1
# do do
# for pred_len in 96 192 336 for pred_len in 96 192 336
# do do
# python3 -m experiments.long_horizon_benchmarks.run_eval \ python3 -m experiments.long_horizon_benchmarks.run_eval \
# --model_path="model/checkpoints" \ --model_path="model/checkpoints" \
# --backend="gpu" \ --backend="gpu" \
# --pred_len=$pred_len \ --pred_len=$pred_len \
# --context_len=512 \ --context_len=512 \
# --dataset=$dataset --dataset=$dataset
# done done
# done done
# python -c "print('finish run_eval!!!!')" python -c "print('finish run_eval!!!!')"
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment