模型名称: Time Series Transformer(huggingface/time-series-transformer-tourism-monthly)
模型链接:
模型描述: HuggingFace 官方教学模型,在 monash_tsf 的 tourism-monthly 数据集(澳大利亚月度旅游人数)上训练 30 epochs。采用 vanilla encoder-decoder Transformer 结构,训练方式类似机器翻译;推理时从 Student-T 分布逐时间步采样生成。支持静态类别特征(366 个目的地)、静态实数特征、时间特征与 16 个 lag 特征(最大 lag=37)。本部署在昇腾 NPU 上完成 61→24 步预测与分布采样。
模型架构: encoder-decoder Transformer(编码器/解码器各 2 层,d_model=26,2 头注意力)+ Student-T 分布头 + 16 个 lag 特征 + 时间特征。
参数规模: 约 30.5K 参数(轻量教学模型)。
适配引擎: 昇腾 torch_npu(torch.npu 设备接口调用 NPU)。
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10(推荐 3.11) | 本机 3.11.14 |
| CANN Toolkit | 8.0.RC2+ | 本机 8.5.1 |
| torch | 2.9.0+cpu | 本机 |
| torch_npu | 2.9.0.post1 | 昇腾 NPU 适配层 |
| transformers | 4.57.6 | 模型加载/推理 |
| safetensors | 0.4.3+ | 权重加载 |
硬件: 昇腾 Ascend910_9362(64GB HBM ×2)。
# 方式一:推理脚本自动下载(推荐)
python3 inference.py --device npu:0
# 方式二:手动下载
mkdir -p model
curl -sSL -o model/config.json "https://hf-mirror.com/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json"
curl -sSL -o model/model.safetensors "https://hf-mirror.com/huggingface/time-series-transformer-tourism-monthly/resolve/main/model.safetensors"npu-smi info
python3 -c "import torch, torch_npu; print(torch.npu.device_count())" # 输出 2python3 inference.py --device npu:0 --warmup 1 --runs 3命令:
python3 inference.py --device npu:0 --warmup 1 --runs 3输出(关键部分):
[模型加载] 完成, 参数量 30.5K, 设备 npu:0
配置: context=24, prediction=24, 分布=student_t
[推理] 合成旅游月度数据: 61 步历史(含37步lag) -> 24 步预测 (Student-T 分布采样 x10000)
历史末值 11.356 / 预测均值前 5 步 [18.146, 19.062, 18.35, 18.18, 18.764]
预测均值范围: [17.853, 19.869]
单样本范围: [-14.117, 23.935]
[性能基准] device=npu:0 warmup=1 runs=3
平均推理时延 = 225.00 ms/条 (4.4 FPS)
(输入 61 步历史 -> 输出 24 步)结果分析: 模型在昇腾 NPU 上完成 61→24 步旅游人数预测,Student-T 分布采样正常,预测均值 [17.85, 19.87] 落在合理旅游人次区间(历史末值 11.36 之后的季节性回升),单样本展现合理波动,验收通过。
TimeSeriesTransformerForPrediction),无需 trust_remote_code。past_values 序列长度须 ≥ context_length + max_lag = 24 + 37 = 61,否则报 lags cannot go further than history length。prediction_distribution(params 仅在提供 future_values 时计算);应使用 model.generate() 做推理采样(返回 SampleTSPredictionOutput.sequences)。reshape(-1, pred_len) 处理。past_values / past_time_features / past_observed_mask / static_categorical_features / static_real_features / future_time_features。