Time Series Transformer 是基于 Transformer 架构的时间序列预测模型,在 tourism-monthly 数据集上训练了 30 个 epoch。
该模型采用标准的 Encoder-Decoder Transformer 结构进行时间序列预测,训练方式类似于机器翻译任务。推理时,模型自回归地生成样本。
| 属性 | 值 |
|---|---|
| 模型类型 | TimeSeriesTransformerForPrediction |
| 参数量 | 0.0305M (30.5K) |
| d_model | 26 |
| Encoder 层数 | 2 |
| Decoder 层数 | 2 |
| Context Length | 24 |
| Prediction Length | 24 |
| Feature Size | 27 |
| Attention Heads | 2 (encoder), 2 (decoder) |
| Distribution Output | student_t |
| 权重精度 | float32 |
(batch_size, past_length) 的历史时间序列(batch_size, past_length, num_time_features) 的时间特征(batch_size, past_length) 的观测掩码(batch_size, 1) 的静态类别特征(batch_size, 1) 的静态实数特征(batch_size, prediction_length, num_time_features) 的未来时间特征(batch_size, num_parallel_samples, prediction_length) 的预测序列| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| 操作系统 | CentOS 7.6+ / Ubuntu 18.04+ | 支持主流 Linux 发行版 |
| CANN | 5.0.RC2+ | 华为昇腾计算架构 |
| Python | 3.10 / 3.12 | 推荐 3.10+ |
| PyTorch | 2.0.0+ | 推荐 2.10 |
| torch_npu | 与 PyTorch 版本匹配 | 昇腾 PyTorch 扩展 |
| transformers | 4.40.1 | 模型加载(必须此版本) |
# 1) 激活已预装 torch / torch_npu 的 conda 环境
conda activate pt2100
# 2) 安装模型依赖(使用华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple \
transformers==4.40.1
# 3) 验证环境
python -c "
import torch, torch_npu
print('torch:', torch.__version__)
print('torch_npu:', torch_npu.__version__)
print('NPU 可用:', torch.npu.is_available())
"# 进入适配目录
cd /workspace/agent/time-series-transformer-tourism-monthly-ascend
# 确认上游权重存在
ls -la /workspace/agent/time-series-transformer-tourism-monthly/
# 期望输出:config.json model.safetensors (~130KB) ...# 检查 NPU 设备状态
npu-smi info
# 验证 torch_npu 安装
python -c "import torch; import torch_npu; print('NPU 可用:', torch.npu.is_available())"# 基本用法
python inference.py --device npu:0
# 查看 NPU 显存使用
python inference.py --device npu:0 --show_npu_memory
# 指定其他 NPU 卡
python inference.py --device npu:1
# CPU 模式(仅在 NPU 不可用时使用)
python inference.py --device cpuconda activate pt2100
cd /workspace/agent/time-series-transformer-tourism-monthly-ascend
python inference.py --device npu:0 --show_npu_memory实际输出:
[W819 06:00:28.866541060 FunctionLoader.cpp:48] Warning: LD_PRELOAD detected...
[W819 06:00:35.876352450 VariableFallbackKernel.cpp:250] Warning: CAUTION: The operator 'aten::_standard_gamma' is not currently supported on the NPU backend and will fall back to run on the CPU...
[设备] npu:0
NPU设备数量: 1
设备 0: Ascend910B2
[权重信息] 文件: /workspace/agent/time-series-transformer-tourism-monthly/model.safetensors
[配置] 模型类型: time_series_transformer
[配置] d_model: 26
[配置] encoder_layers: 2
[配置] decoder_layers: 2
[配置] context_length: 24
[配置] prediction_length: 24
[配置] feature_size: 27
[配置] distribution_output: student_t
[参数量] 0.0305M
加载模型...
模型: TimeSeriesTransformer (Tourism Monthly)
- 任务: 时间序列预测 (Encoder-Decoder Transformer)
- 参数量: 0.0305M
- Context长度: 24
- 预测长度: 24
[NPU显存 · 模型加载后] 总显存=61.0GB | 已分配=0.2MB | 缓存=2.00MB | 峰值=0.2MB
构建测试输入: 随机时间序列 (past_length=61)
past_values形状: (1, 61)
past_time_features形状: (1, 61, 2)
运行推理...
[NPU显存 · 推理完成后] 总显存=61.0GB | 已分配=0.2MB | 缓存=8.00MB | 峰值=6.6MB
==============================================================================
输出摘要
==============================================================================
模型: TimeSeriesTransformer (Tourism Monthly)
past_values形状: (1, 61)
输出形状: (1, 100, 24)
(batch_size, num_samples, prediction_length)
总耗时: 0.52s
输出 finite: True
输出均值: 0.6293
输出标准差: 0.9626
输出范围: [-12.2153, 14.2845]✅ 验证要点:
(1, 100, 24): batch=1, num_samples=100 (并行采样), prediction_length=24解决方案:
source /usr/local/Ascend/ascend-toolkit/latest/set_env.sh)npu-smi info 检查设备状态解决方案:TimeSeriesTransformer 模型与 transformers 4.40.1 兼容。更高版本可能存在 API 变化。
解释:是的。根据模型设计,past_observed_mask 应为 2D 形状 (batch_size, past_length),不是 3D。
解释:该模型支持固定的 prediction_length (24)。对于更长预测,需要迭代调用或使用其他模型。
time-series-transformer-tourism-monthly-ascend/
├── inference.py # 推理脚本(torch_npu 推理)
├── README.md # 本文档
├── requirements.txt # 依赖列表
└── assets/ # 资源占位目录上游权重目录(只读,未被修改):
/workspace/agent/time-series-transformer-tourism-monthly/
├── config.json # 模型配置
├── model.safetensors # 权重(0.0305M, ~130KB)
├── pytorch_model.bin # PyTorch 格式权重
└── README.md # 原始文档# TimeSeriesTransformer-Tourism-Monthly 昇腾 NPU 推理环境依赖
# 核心依赖(必须)
torch>=2.0.0
torch_npu>=2.0.0
transformers==4.40.1
safetensors>=0.4.0
numpy>=1.21.0使用方式:
# 激活环境
conda activate pt2100
# 安装依赖(华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple -r requirements.txt