TimesFM(Time Series Foundation Model)安装(2)

embedded/2024/12/26 18:48:59/

TimesFM(Time Series Foundation Model)安装简介

readme

TimesFM(Time Series Foundation Model)安装简介(1)-CSDN博客icon-default.png?t=O83Ahttps://blog.csdn.net/chenchihwen/article/details/144359861?spm=1001.2014.3001.5501

在Window 上安装并执行出现报错,

{"name": "TypeError","message": "TimesFmBase.__init__() got an unexpected keyword argument 'context_len'","stack": "---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[9], line 83 from jax._src import config4 config.update(5     \"jax_platforms\", {\"cpu\": \"cpu\", \"gpu\": \"cuda\", \"tpu\": \"\"}[timesfm_backend]6 )
----> 8 model = timesfm.TimesFm(9     context_len=512,10     horizon_len=128,11     input_patch_len=32,12     output_patch_len=128,13     num_layers=20,14     model_dims=1280,15     backend=timesfm_backend,16 )17 model.load_from_checkpoint(repo_id=\"google/timesfm-1.0-200m\")TypeError: TimesFmBase.__init__() got an unexpected keyword argument 'context_len'"
}

决定在 小红帽ubuntu UBUNTU

安装 timesFM

在 ide.cloud.tencent.com 的环境上进行安装 环境 

We recommend at least 16GB RAM to load TimesFM dependencies.

慎选环境,确保>16G

安装Conda  3.10 python

重要步骤 安装  pyenv and poetry

## Installation

### Local installation using poetry

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:

确认已经完成安装

(base) root@VM-0-170-ubuntu:/workspace/timesfm# pyenv --version
pyenv 2.4.22
(base) root@VM-0-170-ubuntu:/workspace/timesfm# poetry --version
Poetry (version 1.8.5)

这里安装完需要设置环境变量,如果不能看到 version 版本时

Add `export PATH="/root/.local/bin:$PATH"` to your shell configuration file

要修改环境变量

nano ~/.bash_profile

nano ~/.bash_profile 执行后添加环境变量,在 bash_profile里应该是这个参数

/root/.bash_profile                                                                                              
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
export PATH="/root/.local/bin:$PATH"

修改后要执行 ctrl-O  : write out

再执行 ctrl-X :Exit

刷新环境变量,这时后才会生效,执行以下代码

source ~/.bash_profile

克隆timesFM

git clone https://github.com/google-research/timesfm/gitgit clone https://github.com/google-research/timesfm.git
Cloning into 'timesfm'...
remote: Enumerating objects: 667, done.
remote: Counting objects: 100% (665/665), done.
remote: Compressing objects: 100% (316/316), done.
remote: Total 667 (delta 353), reused 568 (delta 306), pack-reused 2 (from 1)
Receiving objects: 100% (667/667), 1.94 MiB | 3.76 MiB/s, done.
Resolving deltas: 100% (353/353), done.

在 timesfm 下找到 pyproject.toml 在最后面添加 aliyun 的source,不然安装不起来

[[tool.poetry.source]]
name = "aliyun"
url = "https://mirrors.aliyun.com/pypi/simple/"
priority = "primary"

 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:

pyenv install 3.10
pyenv install 3.11
pyenv versions # to list the versions available (lets assume the versions are 3.10.15 and 3.11.10)

For PAX version installation do the following.

在 timesfm git 目录下 执行

pyenv local 3.10.15
poetry env use 3.10.15
poetry lock
poetry install -E  pax

在 timesfm git 目录下 执行

➜  timesfm git:(master) pyenv local 3.10.15
poetry env use 3.10.15
poetry lock
poetry install -E  pax
pyenv: version `3.10.15' not installedCould not find the python executable python3.10
Creating virtualenv timesfm-jW3uZHTw-py3.11 in /root/.cache/pypoetry/virtualenvs
Updating dependencies
Resolving dependencies... (378.2s)

过程中还要安装numpy

根据 readme 

Please look into the README files in the respective benchmark directories within `experiments/` for instructions for running TimesFM on the respective benchmarks.

## Running TimesFM on the benchmark

We need to add the following packages for running these benchmarks. Follow the installation instructions till before `poetry lock`. Then,

```
poetry add git+https://github.com/awslabs/gluon-ts.git
poetry lock
poetry install --only <pax or pytorch>
```

To run the timesfm on the benchmark do:

```
poetry run python3 -m experiments.extended_benchmarks.run_timesfm --model_path=google/timesfm-1.0-200m(-pytorch) --backend="gpu"
```

 执行 poetry shell 

(base) root@VM-0-170-ubuntu:/workspace/timesfm# poetry shell
Spawning shell within /root/.cache/pypoetry/virtualenvs/timesfm-p1AFFT58-py3.10
. /root/.cache/pypoetry/virtualenvs/timesfm-p1AFFT58-py3.10/bin/activate
bash: __vsc_prompt_cmd_original: command not found
(base) root@VM-0-170-ubuntu:/workspace/timesfm# . /root/.cache/pypoetry/virtualenvs/timesfm-p1AFFT58-py3.10/bin/activate
bash: __vsc_prompt_cmd_original: command not found
(timesfm-py3.10) (base) root@VM-0-170-ubuntu:/workspace/timesfm# 

执行命令 poetry run python3 -m experiments.extended_benchmarks.run_timesfm --model_path=google/timesfm-1.0-200m

在成功执行之前,还需安装 jax, 

run_timesfm.py 的代码如下

# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Evaluation script for timesfm."""import os
import sys
import timefrom absl import flags
import numpy as np
import pandas as pd
import timesfmfrom .utils import ExperimentHandlerdataset_names = ["m1_monthly","m1_quarterly","m1_yearly","m3_monthly","m3_other","m3_quarterly","m3_yearly","m4_quarterly","m4_yearly","tourism_monthly","tourism_quarterly","tourism_yearly","nn5_daily_without_missing","m5","nn5_weekly","traffic","weather","australian_electricity_demand","car_parts_without_missing","cif_2016","covid_deaths","ercot","ett_small_15min","ett_small_1h","exchange_rate","fred_md","hospital",
]context_dict = {"cif_2016": 32,"tourism_yearly": 64,"covid_deaths": 64,"tourism_quarterly": 64,"tourism_monthly": 64,"m1_monthly": 64,"m1_quarterly": 64,"m1_yearly": 64,"m3_monthly": 64,"m3_other": 64,"m3_quarterly": 64,"m3_yearly": 64,"m4_quarterly": 64,"m4_yearly": 64,
}_MODEL_PATH = flags.DEFINE_string("model_path", "google/timesfm-1.0-200m","Path to model")
_BATCH_SIZE = flags.DEFINE_integer("batch_size", 64, "Batch size")
_HORIZON = flags.DEFINE_integer("horizon", 128, "Horizon")
_BACKEND = flags.DEFINE_string("backend", "gpu", "Backend")
_NUM_JOBS = flags.DEFINE_integer("num_jobs", 1, "Number of jobs")
_SAVE_DIR = flags.DEFINE_string("save_dir", "./results", "Save directory")QUANTILES = list(np.arange(1, 10) / 10.0)def main():results_list = []tfm = timesfm.TimesFm(hparams=timesfm.TimesFmHparams(backend=_BACKEND.value,per_core_batch_size=_BATCH_SIZE.value,horizon_len=_HORIZON.value,),checkpoint=timesfm.TimesFmCheckpoint(huggingface_repo_id=_MODEL_PATH.value),)run_id = np.random.randint(100000)model_name = "timesfm"for dataset in dataset_names:print(f"Evaluating model {model_name} on dataset {dataset}", flush=True)exp = ExperimentHandler(dataset, quantiles=QUANTILES)if dataset in context_dict:context_len = context_dict[dataset]else:context_len = 512train_df = exp.train_dffreq = exp.freqinit_time = time.time()fcsts_df = tfm.forecast_on_df(inputs=train_df,freq=freq,value_name="y",model_name=model_name,forecast_context_len=context_len,num_jobs=_NUM_JOBS.value,)total_time = time.time() - init_timetime_df = pd.DataFrame({"time": [total_time], "model": model_name})results = exp.evaluate_from_predictions(models=[model_name],fcsts_df=fcsts_df,times_df=time_df)print(results, flush=True)results_list.append(results)results_full = pd.concat(results_list)save_path = os.path.join(_SAVE_DIR.value, str(run_id))print(f"Saving results to {save_path}", flush=True)os.makedirs(save_path, exist_ok=True)results_full.to_csv(f"{save_path}/results.csv")if __name__ == "__main__":FLAGS = flags.FLAGSFLAGS(sys.argv)main()

执行结果:

(timesfm-py3.10) (base) root@VM-0-170-ubuntu:/workspace/timesfm# poetry run python3 -m experiments.extended_benchmarks.run_timesfm --model_path=google/timesfm-1.0-200m
TimesFM v1.2.0. See https://github.com/google-research/timesfm/blob/master/README.md for updated APIs.
Loaded Jax TimesFM.
/root/.cache/pypoetry/virtualenvs/timesfm-p1AFFT58-py3.10/lib/python3.10/site-packages/gluonts/json.py:102: UserWarning: Using `json`-module for json-handling. Consider installing one of `orjson`, `ujson` to speed up serialization and deserialization.warnings.warn(
Fetching 5 files: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 63937.56it/s]
Multiprocessing context has already been set.
Constructing model weights.
Constructed model weights in 2.83 seconds.
Restoring checkpoint from /root/.cache/huggingface/hub/models--google--timesfm-1.0-200m/snapshots/8775f7531211ac864b739fe776b0b255c277e2be/checkpoints.
WARNING:absl:No registered CheckpointArgs found for handler type: <class 'paxml.checkpoints.FlaxCheckpointHandler'>
WARNING:absl:Configured `CheckpointManager` using deprecated legacy API. Please follow the instructions at https://orbax.readthedocs.io/en/latest/api_refactor.html to migrate by May 1st, 2024.
WARNING:absl:train_state_unpadded_shape_dtype_struct is not provided. We assume `train_state` is unpadded.
ERROR:absl:For checkpoint version > 1.0, we require users to provide`train_state_unpadded_shape_dtype_struct` during checkpointsaving/restoring, to avoid potential silent bugs when loadingcheckpoints to incompatible unpadded shapes of TrainState.
Restored checkpoint in 1.50 seconds.
Jitting decoding.
Jitted decoding in 20.78 seconds.
Evaluating model timesfm on dataset m1_monthly
/root/.cache/pypoetry/virtualenvs/timesfm-p1AFFT58-py3.10/lib/python3.10/site-packages/gluonts/time_feature/seasonality.py:47: FutureWarning: 'M' is deprecated and will be removed in a future version, please use 'ME' instead.offset = pd.tseries.frequencies.to_offset(freq)
Multiprocessing context has already been set.
/root/miniforge3/lib/python3.10/multiprocessing/popen_fork.py:66: RuntimeWarning: os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is multithreaded, so this will likely lead to a deadlock.self.pid = os.fork()
Processing dataframe with single process.
Finished preprocessing dataframe.
Finished forecasting.

 预测结果

           dataset       metric    model        value
0  tourism_monthly          mae  timesfm  1970.148438
1  tourism_monthly         mase  timesfm     1.541883
2  tourism_monthly  scaled_crps  timesfm     0.121862
3  tourism_monthly        smape  timesfm     0.101539
4  tourism_monthly         time  timesfm     0.762044---------------dataset       metric    model        value
0  tourism_quarterly          mae  timesfm  7439.246094
1  tourism_quarterly         mase  timesfm     1.731996
2  tourism_quarterly  scaled_crps  timesfm     0.087743
3  tourism_quarterly        smape  timesfm     0.083795
4  tourism_quarterly         time  timesfm     0.839042-----------------dataset       metric    model         value
0  tourism_yearly          mae  timesfm  82434.085938
1  tourism_yearly         mase  timesfm      3.233205
2  tourism_yearly  scaled_crps  timesfm      0.129402
3  tourism_yearly        smape  timesfm      0.181012
4  tourism_yearly         time  timesfm      1.023866


http://www.ppmy.cn/embedded/145402.html

相关文章

在 Ubuntu 20.04 上离线安装和配置 Redis

下面是从零开始配置 Redis 的完整步骤&#xff0c;包括从安装 Redis 到离线安装 Redis 包的步骤。本文将覆盖如何从下载 Redis 安装包、手动安装 Redis、创建所需目录、配置 Redis、以及如何配置 Redis 为系统服务&#xff0c;确保服务可以在启动时自动运行。 步骤 1: 准备环境…

RK3568(一)——SDK环境搭建

虚拟机安装 搭建文件互传方法 网络配置 虚拟机网络配置 开发板网络设置 临时设置ip地址&#xff08;重启失效&#xff09; ifconfig eth0 192.168.137.10 netmask 255.255.255.0 route add default gw 192.168.137.1设置动态ip vi etc/network/interfaces auto eth0 if…

基于单片机智能控制的饮水机控制系统

基于单片机智能控制的饮水机控制系统&#xff0c;以STC89C52单片机为核心&#xff0c;利用防水型DS18B20温度传感器对饮水机内的水温做出检测&#xff0c;其次利用水位传感器对饮水机内的水量做出检测&#xff0c;并显示在OLED液晶显示屏上。用户在使用饮水机时&#xff0c;通过…

大模型qiming面试内容整理-编码能力评估

编码能力评估是大模型相关岗位面试中非常关键的一环,面试官通常希望通过这个环节了解候选人对编程语言、算法与数据结构的掌握情况,以及其在实践中解决实际问题的能力。以下是编码能力评估的常见内容和类型,特别是针对机器学习、大模型和深度学习方向: 编程语言熟练度 ● P…

ios 开发配置蓝牙

如果使用了蓝牙功能, 又没有配置, 会出现以下错误: This app has crashed because it attempted to access privacy-sensitive data without a usage description. The apps Info.plist must contain an NSBluetoothAlwaysUsageDescription key with a string value explaini…

酷柚易汛进销存系统PHP+Uniapp

移动端订货通、商品管理、库存管理、订单管理、客户管理、供应商、财务管理、经营分析 版本更新V1.6.4 1、新增供应商分类不可添加重复类别2、新增客户分类不可添加重复类别3、新增商品分类不可添加重复类别4、新增支出类别不可添加重复类别5、新增收入类别不可添加重复类别6…

Linux下网卡实现NAT转发

目标 在嵌入式Linux设备下&#xff0c;使用单一的网卡&#xff08;前提支持STA&#xff0b;AP共存&#xff09;&#xff0c;使用NAT&#xff08;网络地址转换&#xff09;实现软路由&#xff0c;以自身为热点&#xff0c;将接收到的流量数据全部转发出去。 一&#xff0c;STA…

专题二十六_贪心策略(3)_算法专题详细总结

目录 贪心策略 1. 单调递增的数字&#xff08;medium&#xff09; 解析&#xff1a; 代码编写&#xff1a; 总结&#xff1a; 2. 坏了的计算器&#xff08;medium&#xff09; 解析&#xff1a; 解法一&#xff1a;正向推导&#xff1a; 解法二&#xff1a;正难则反 代…