






















今天在 GitHub Trending 上看到一个重磅项目:Google TimesFM,Google Research 开源的时间序列基础模型,支持零样本时序预测,已经在 Google 内部多个产品中落地。
TimesFM(Time Series Foundation Model)是 Google Research 开发的预训练时间序列基础模型,专为时间序列预测任务设计。该项目已于 ICML 2024 发表论文 A decoder-only foundation model for time-series forecasting。
核心特性:
TimesFM 采用 decoder-only Transformer 架构,这与 GPT 系列的思路一致——通过自回归方式预测未来时间步。模型将时间序列视为一种"语言",学习通用的时序模式,从而实现跨领域的零样本预测。
从源码分析,TimesFM 的技术选型如下:
from_pretrained 加载预训练权重torch>=2.0.0,支持 torch.set_float32_matmul_precision("high") 加速jax[cuda] + scikit-learn 实现外部协变量输入safetensors>=0.5.3 安全序列化格式从代码示例中可以提取核心预测配置:
model.compile(
timesfm.ForecastConfig(
max_context=1024, # 最大上下文长度
max_horizon=256, # 最大预测步长
normalize_inputs=True, # 输入归一化
use_continuous_quantile_head=True, # 启用连续分位数预测
force_flip_invariance=True, # 强制翻转不变性
infer_is_positive=True, # 推断值为正
fix_quantile_crossing=True, # 修复分位数交叉问题
)
)
这些配置体现了 TimesFM 的设计哲学:翻转不变性(正向和负向序列应产生对称预测)、输入归一化(消除不同量纲的影响)、分位数交叉修正(确保分位数单调性)。
模型的推理流程为:
推荐方式:通过 PyPI 安装
# 使用 PyTorch 后端
pip install timesfm[torch]
# 使用 Flax 后端
pip install timesfm[flax]
# 需要 XReg 协变量支持
pip install timesfm[xreg]
本地开发安装:
git clone https://github.com/google-research/timesfm.git
cd timesfm
uv venv
source .venv/bin/activate
uv pip install -e .[torch]
import torch
import numpy as np
import timesfm
torch.set_float32_matmul_precision("high")
# 加载预训练模型(200M 参数版本)
model = timesfm.TimesFM_2p5_200M_torch.from_pretrained(
"google/timesfm-2.5-200m-pytorch"
)
model.compile(
timesfm.ForecastConfig(
max_context=1024,
max_horizon=256,
)
)
# 对两条时序数据进行预测
point_forecast, quantile_forecast = model.forecast(
horizon=12,
inputs=[
np.linspace(0, 1, 100), # 线性序列
np.sin(np.linspace(0, 20, 67)), # 正弦序列
],
)
print(point_forecast.shape) # (2, 12) - 两条序列各12步点预测
print(quantile_forecast.shape) # (2, 12, 10) - 包含均值+10个分位数
模型的核心 API 非常简洁,forecast() 方法接收:
horizon:预测步长inputs:时间序列列表(支持不同长度)输出两个结果:
point_forecast:点预测值,shape 为 (batch, horizon)quantile_forecast:分位数预测,shape 为 (batch, horizon, 10),包含均值和第10到第90百分位数分位数预测:启用 use_continuous_quantile_head=True 后,模型不仅输出点预测,还输出概率分布,适用于需要风险评估的场景(如金融风控、库存管理)。
LoRA 微调:项目提供了基于 HuggingFace PEFT 的微调示例,位于 timesfm-forecasting/examples/finetuning/,适合在特定领域数据上进一步优化。
Agent 集成:TimesFM 支持 Agent 调用模式,已提供 AGENTS.md 和 SKILL.md,可以通过 Vertex AI Model Garden 的 Docker化端点进行 agentic 调用。
max_context(如 1024 或 2048)fix_quantile_crossing=True 可自动修正pip install timesfm==1.3.0 安装旧版TimesFM 代表了时间序列预测领域的一个重要方向——将基础模型的理念引入时序分析。相比传统方法,它具备零样本跨域预测能力;相比其他时序大模型,它的 200M 参数量更加轻量,16K 上下文覆盖了大多数实际场景。作为 Google 内部已经在 BigQuery ML、Google Sheets 等产品中落地的模型,TimesFM 的工程成熟度值得信赖。对于从事金融预测、需求预测、异常检测等时序分析工作的开发者来说,这是一个值得关注和尝试的开源项目。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。