模型名称: 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 层)
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 本环境 3.11.4 |
| torch / torch_npu | 2.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-*,使用系统自带版本。
# 检查 NPU 设备
npu-smi info
# 设置模型专属依赖路径
export PYTHONPATH=/mnt/workspace/time-series-transformer-tourism-monthly/libs:$PYTHONPATHpython inference.py --model_path /home/developer/models/time-series-transformer-tourism-monthly脚本内置示例输入(合成一条带年周期+趋势的月度序列),自动选择设备 npu > cuda > cpu,离线加载本地权重(local_files_only=True),通过 generate 自回归采样生成未来 24 步概率预测,输出均值到 outputs/ 目录 CSV 文件。
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model_path | str | /home/developer/models/time-series-transformer-tourism-monthly | 本地模型权重目录(必传) |
| --input_values | str | None | 输入一维历史序列 .npy 文件路径,缺省用内置示例序列 |
| --context_length | int | 24 | 回看窗口长度(模型训练值 24) |
| --output_dir | str | outputs | 预测结果输出目录 |
输入:
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输入:
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


TimeSeriesTransformerForPrediction(model_type=time_series_transformer),无需 trust_remote_code;权重目录含 model.safetensors 与 pytorch_model.bin,脚本优先 safetensors 离线加载,全程不联网[batch, context_length=24],同时需提供 past_time_features(num_time_features=2)、static_categorical_features(cardinality=366)、static_real_features、past_observed_mask;内部自动做标准化(scaling=true)