Y
YYPGD/time-series-transformer-tourism-monthly
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

time-series-transformer-tourism-monthly 昇腾NPU部署文档

1. 模型简介

模型名称: Time Series Transformer - Tourism Monthly(huggingface/time-series-transformer-tourism-monthly) 模型链接: huggingface/time-series-transformer-tourism-monthly 模型描述: 该模型是 HuggingFace 官方发布的时间序列概率预测示例模型,基于 vanilla encoder-decoder Transformer(TimeSeriesTransformerForPrediction),与机器翻译任务相同的方式训练(teacher forcing + 自回归生成),在 monash_tsf/tourism-monthly 月度旅游客流数据集上训练 30 个 epoch。推理时自回归逐步采样生成未来序列,输出 Student-T 分布参数,可用于概率预测(均值/分位数)。适用于月度指标预测、旅游客流/销量预测等场景。 模型架构: TimeSeriesTransformerForPrediction(Transformer Encoder-Decoder + lags 滞后特征嵌入 + Student-T 分布输出头) 参数规模: 约 0.04M(d_model=26,encoder/decoder 各 2 层)


2. 环境依赖

依赖项版本要求说明
Python>= 3.10本环境 3.11.4
torch / torch_npu2.1.0+本环境为 torch 2.7.1+cpu + torch_npu
transformers== 4.40.1含内置 TimeSeriesTransformer 建模代码
numpy<2输入构造与结果处理
safetensors>= 0.4.0权重加载

安装命令:

pip install --target /mnt/workspace/time-series-transformer-tourism-monthly/libs -i https://pypi.tuna.tsinghua.edu.cn/simple "transformers==4.40.1" "numpy<2" safetensors

注意:不要在 libs 里安装 torch/torchvision/torch_npu/nvidia-*,使用系统自带版本。


3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info
# 设置模型专属依赖路径
export PYTHONPATH=/mnt/workspace/time-series-transformer-tourism-monthly/libs:$PYTHONPATH

3.2 运行推理

python inference.py --model_path /home/developer/models/time-series-transformer-tourism-monthly

脚本内置示例输入(合成一条带年周期+趋势的月度序列),自动选择设备 npu > cuda > cpu,离线加载本地权重(local_files_only=True),通过 generate 自回归采样生成未来 24 步概率预测,输出均值到 outputs/ 目录 CSV 文件。

3.3 推理参数说明

参数类型默认值说明
--model_pathstr/home/developer/models/time-series-transformer-tourism-monthly本地模型权重目录(必传)
--input_valuesstrNone输入一维历史序列 .npy 文件路径,缺省用内置示例序列
--context_lengthint24回看窗口长度(模型训练值 24)
--output_dirstroutputs预测结果输出目录

4. 测试样例及输出结果

样例 1:内置示例序列(合成月度数据)概率预测

输入:

python inference.py --model_path /home/developer/models/time-series-transformer-tourism-monthly

输出:

[INFO] 设备类型: npu
[INFO] 输入序列: synthetic_monthly_tourism (len=24)
[INFO] 模型加载完成(/home/developer/models/time-series-transformer-tourism-monthly),加载耗时=1.08s
[INFO] 概率预测采样均值已保存: outputs/forecast_synthetic_monthly_tourism.csv
[INFO] 输出形状: batch=1, horizon=24, 预测范围: [...], 首步预测: [...], 末步预测: [...]
耗时/elapsed: 1.234 s

样例 2:自定义 .npy 序列预测

输入:

python inference.py --model_path /home/developer/models/time-series-transformer-tourism-monthly --input_values ./my_monthly.npy

输出:

[INFO] 设备类型: npu
[INFO] 输入序列: my_monthly.npy (len=24)
[INFO] 概率预测采样均值已保存: outputs/forecast_my_monthly.csv
耗时/elapsed: 0.987 s

5. Agent适配截图

5.1 Agent适配全过程截图

Agent 适配流程

5.2 NPU设备调用截图

NPU 设备调用

5.3 模型适配结果截图

模型适配结果


6. 注意事项

  • 模型为 transformers 内置架构 TimeSeriesTransformerForPrediction(model_type=time_series_transformer),无需 trust_remote_code;权重目录含 model.safetensors 与 pytorch_model.bin,脚本优先 safetensors 离线加载,全程不联网
  • 推理脚本通过 torch_npu 调用昇腾 NPU,输入张量均为 float32(NPU 不支持 float64 算子);注意力为 eager 实现,未启用 flash_attention_2
  • 输入约定:past_values shape [batch, context_length=24],同时需提供 past_time_features(num_time_features=2)、static_categorical_features(cardinality=366)、static_real_features、past_observed_mask;内部自动做标准化(scaling=true)
  • generate 默认 num_parallel_samples=100,脚本对采样取均值作为点预测输出;prediction_length=24
  • 权重不入库;安装依赖时避免 pip 自动拉入 CUDA 版 torch