1
gcw_xUvjaUyi/time-series-transformer-tourism-monthly-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

time-series-transformer-tourism-monthly-npu

huggingface/time-series-transformer-tourism-monthly 昇腾 Ascend NPU 推理适配。

该模型是 HuggingFace 时间序列课程示例模型:经典 encoder-decoder Transformer 架构 (transformers 内置 TimeSeriesTransformerForPrediction),在 monash_tsf / tourism-monthly 月频数据上训练,tiny 规模(仅几百 KB 权重),无自定义 remote code。

  • 设备:昇腾 Ascend 910(torch_npu),固定使用 npu:0
  • 全代码不含任何 CUDA 调用;模型与输入 Tensor 全部运行在 npu:0
  • 推理:官方 model.generate() 自回归采样,从概率分布输出 prediction_length 步样本, 再取 mean / median / quantiles 作为预测结果

环境依赖

pip install -r requirements.txt
# 若 pypi 官方源慢, 可使用清华镜像:
# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt

说明:transformers 的 TimeSeriesTransformerForPrediction 依赖 gluonts (gluonts.core.component),务必安装 gluonts。

权重下载

权重从 hf-mirror.com 镜像自动下载,缓存到本地 model_weights/(./model_weights/huggingface__time-series-transformer-tourism-monthly)。 启动时优先加载本地缓存,命中则跳过下载。

export HF_ENDPOINT=https://hf-mirror.com

CLI 推理

python3 inference.py --text "[<数值数组>]" [--horizon 24]
  • --text:月频历史序列的 JSON 数值数组(时间序列模型统一用 --text 传入数值数组)。
  • --horizon:预测步数,默认取 config.prediction_length(本模型为 24)。

脚本会自动取历史序列末尾 past_length = context_length + max(lags_sequence) = 61 个点 作为模型上下文;若输入不足 61 个点,则用首值向左补齐。示例:

python3 inference.py --text "[20.0,20.8295,21.636,22.3971,23.0918,23.7009,24.5,25.2,24.8,23.5,22.9,23.1,24.0,25.4,27.1,28.9,30.2,31.0,30.5,29.0,28.1,28.8,30.3,31.9,32.7,32.1,30.6,29.7,30.4,31.8,33.0,33.4,32.0,30.5,29.9,30.6,31.9,33.2,33.6,32.2,30.8,30.1,30.7,31.8,32.9,33.1,31.7,30.4,29.8,30.3,31.6,32.8,33.0,31.8,30.5,29.9,30.4,31.7,33.0,33.3,32.1,30.7]" --horizon 12

输出示例:

{
  "model": "huggingface/time-series-transformer-tourism-monthly",
  "device": "npu:0",
  "frequency": "monthly",
  "context_length": 24,
  "lookback": 61,
  "past_length": 61,
  "horizon": 12,
  "samples": 100,
  "start_month": 0,
  "mean": [ ... ],
  "median": [ ... ],
  "quantiles": { "0.1": [...], "0.25": [...], "0.5": [...], "0.75": [...], "0.9": [...] }
}

FastAPI 服务

uvicorn inference:app --host 0.0.0.0 --port 8002

POST /predict,请求体:

{
  "text": [20.0, 20.8295, 21.636, 22.3971, 23.0918, 23.7009, 24.5, 25.2, 24.8, 23.5, 22.9, 23.1, 24.0, 25.4, 27.1, 28.9, 30.2, 31.0, 30.5, 29.0, 28.1, 28.8, 30.3, 31.9, 32.7, 32.1, 30.6, 29.7, 30.4, 31.8, 33.0, 33.4, 32.0, 30.5, 29.9, 30.6, 31.9, 33.2, 33.6, 32.2, 30.8, 30.1, 30.7, 31.8, 32.9, 33.1, 31.7, 30.4, 29.8, 30.3, 31.6, 32.8, 33.0, 31.8, 30.5, 29.9, 30.4, 31.7, 33.0, 33.3, 32.1, 30.7],
  "horizon": 12
}

响应为与 CLI 相同的预测 JSON(含 mean / median / quantiles)。

交互式文档:http://127.0.0.1:8002/docs。

时间特征与静态特征

  • 时间特征:月周期正弦/余弦两维(sin(2π·m/12)、cos(2π·m/12)),维数等于 config.num_time_features(本模型为 2)。脚本会依据输入序列月相自动对齐起始月份。
  • 静态特征:1 个类别特征(cardinality=366,取 ID=0)+ 1 个实值特征(取 0.0), 与 config 中 num_static_categorical_features=1、num_static_real_features=1 对应。

模型配置要点

项值
model_typetime_series_transformer
context_length24
prediction_length24
lags_sequence max37(past_length = 24 + 37 = 61)
num_time_features2
num_static_categorical_features / cardinality1 / [366]
num_static_real_features1
distribution_outputstudent_t
scalingtrue