Appearance
Autoformer — 收束
本文档是 Autoformer 精读文档集的终点:端到端流程图、tensor 变化汇总、与其他模型的设计对比,以及核心设计决策回顾。
1. 端到端 Mermaid 流程图
2. Tensor 变化汇总表
以全局 toy 参数(B=2, seq_len=12, pred_len=4, dec_len=10, enc_in=5, d_model=8)追踪完整 forward pass。
| 步骤 | 操作 | 关键 shape | 文档 |
|---|---|---|---|
| 0 | 输入四元组 | x_enc(2,12,5), x_mark_enc(2,12,4), x_dec(2,10,5), x_mark_dec(2,10,4) | [[01-Layer0-接入界面]] |
| 1 | self.decomp(x_enc) → seasonal_init(2,12,5), trend_init(2,12,5) | (2,12,5) → 2×(2,12,5) | [[02-Layer1-forecast主链]] |
| 2 | 构造 trend_init(decoder 用) | mean(x_enc) concat → (2,10,5) | [[02-Layer1-forecast主链]] |
| 3 | 构造 seasonal_init(decoder 用) | zeros concat → (2,10,5) | [[02-Layer1-forecast主链]] |
| 4 | enc_embedding(x_enc, x_mark_enc) | (2,12,5) + (2,12,4) → (2,12,8) | [[03A-Layer2A-DataEmbedding]] |
| 5 | encoder(enc_out) — 2 × EncoderLayer | (2,12,8) → (2,12,8) | [[03B-Layer2B-Encoder]] |
| 6 | dec_embedding(seasonal_init, x_mark_dec) | (2,10,5) + (2,10,4) → (2,10,8) | [[03A-Layer2A-DataEmbedding]] |
| 7 | decoder.DecoderLayer — self-attn + cross-attn + FFN | (2,10,8) seasonal · (2,10,5) residual_trend | [[03C1-Layer3-DecoderLayer]] |
| 8 | trend += residual_trend 累加 | (2,10,5) + (2,10,5) → (2,10,5) | [[03C-Layer2C-Decoder]] |
| 9 | my_Layernorm(seasonal) + Linear(8→5) | (2,10,8) → (2,10,5) seasonal_part | [[03C-Layer2C-Decoder]] |
| 10 | dec_out = trend_part + seasonal_part | (2,10,5) + (2,10,5) → (2,10,5) | [[02-Layer1-forecast主链]] |
| 11 | [:, -4:, :] 取预测段 | (2,10,5) → (2,4,5) | [[01-Layer0-接入界面]] |
3. 核心设计对比表
| 维度 | Autoformer | Informer | DLinear | PatchTST |
|---|---|---|---|---|
| 注意力机制 | FFT 互相关(AutoCorrelation) | ProbSparse 稀疏查询筛选 | 无注意力 | 标准多头 FullAttention |
| 时间复杂度 | ||||
| 分解结构 | 贯穿编解码(Progressive Decomp) | 无内置分解 | 一次前置分解 | 无分解 |
| Decoder 趋势路径 | 独立 trend 路径,逐层累加 | 无独立 trend 路径 | 独立 trend + seasonal Linear | 无 |
| Encoder 序列长度 | 不变(无 distilling) | 逐层减半(ConvLayer distilling) | 不变 | patch 化后 patch_num ≪ L |
| 位置编码 | 无(DataEmbedding_wo_pos) | 有(正弦位置编码) | 无 | patch 内 flatten,有学习位置编码 |
| 参数量 | 中等 | 中等 | 极少(2个 Linear) | 大(Transformer × 多头) |
| 适用场景 | 强周期性时序(季节性数据) | 长序列,ProbSparse 降复杂度 | 趋势性时序,快速基线 | 多变量 Patch 建模,通道独立 |
4. Autoformer 特色设计决策回顾
4.1 为什么用 FFT 互相关代替点积注意力?
点积注意力度量"token i 和 token j 的相似度",是位置对齐的匹配,没有显式利用时序周期性。
AutoCorrelation 计算的是
corr[τ] 表示"Q 右移 τ 步后与 K 的相似程度",直接反映周期性 lag 强度。Top-k lag 选取 + roll 聚合使模型专注于周期性长距离依赖,而非点对点的 token 匹配。
4.2 为什么 Progressive Series Decomposition 要贯穿每一层?
每次注意力或 FFN 操作都可能混入低频趋势信息。若只在入口做一次分解,趋势会通过多次残差连接持续累积,最终污染 seasonal 路径。
每段后立即 decomp 相当于"实时趋势滤波":
- Encoder:每次 decomp 丢弃 trend → seasonal 路径始终保持高频信号,给 Decoder cross-attention 提供纯净周期表示
- Decoder:每次 decomp 保留 trend 并累加 → 3次 decomp 的 trend1+trend2+trend3 逐步精化趋势预测
4.3 为什么 Decoder 的 trend 投影用 Conv1d(k=3) 而不是 Linear?
seasonal 路径用 nn.Linear(8→5) —— Position-wise,每个时间步独立,足够。
trend 路径用 Conv1d(k=3, padding=1, circular) —— kernel=3 使 trend 投影有局部时间感受野,在维度压缩的同时做轻微时间平滑,与趋势的低频平滑特性一致。Circular padding 保持时序边界的循环一致性,与 TokenEmbedding、moving_avg 等组件风格统一。
4.4 Training vs Inference 的 lag 选取差异
| Training | Inference | |
|---|---|---|
| 策略 | Batch 内共享 top-k lag(先对 B 均值再 topk) | 每个样本独立 top-k lag |
| 实现 | torch.mean(..., dim=0) → torch.topk | torch.topk(mean_value, ...) per sample |
| 并行性 | 高(batch 内共享索引,避免 gather 循环) | 低(sample-level 独立 gather) |
| 精准度 | 低(忽略样本差异) | 高(per-sample 自适应) |
| 实现方式 | torch.roll 循环聚合 | torch.gather + repeat 避免循环 |
4.5 DataEmbedding_wo_pos 的 ⚠️ 设计细节
DataEmbedding_wo_pos.__init__() 中确实创建了 self.position_embedding = PositionalEmbedding(d_model),但 forward() 中从未调用,等价于"有一个不插电的位置编码器"。这是 Autoformer 论文的设计选择:作者认为 AutoCorrelation 机制已通过 FFT 周期检测隐式捕获了时序结构,无需额外的显式位置编码。
5. 文档覆盖范围速查
6. 推荐阅读路径
快速直觉版(10分钟):00-总览 → 02-Layer1 § 5.1 宏观逻辑 → 04A-Layer5 § 5.1
完整精读版(按 BFS 顺序):00 → 01 → 02 → 03D → 03A → 03B → 03B1 → 03C → 03C1 → 04 → 04A → 05
对比阅读(只看 Autoformer 创新):03D-SeriesDecomp(decomp 核心)→ 04A-AutoCorrelation(FFT 核心)→ 03C1-DecoderLayer(trend 路由)