Appearance
收束 — FEDformer 端到端流程与对比
1. 端到端完整流程图
2. Tensor 变化汇总表
| 步骤 | 操作 | 输入 shape | 输出 shape |
|---|---|---|---|
| 0 | TFB 接入 _process() | x_enc(3,12,5), target(3,10,5) | x_enc(3,12,5), x_mark_enc(3,12,4), dec_input(3,10,5), x_mark_dec(3,10,4) |
| 1 | x_enc.mean(dim=1) → repeat | (3,12,5) | mean(3,4,5) |
| 2 | series_decomp(x_enc) | (3,12,5) | seasonal_init(3,12,5), trend_init(3,12,5) |
| 3 | cat[trend[-6:,:], mean] | (3,6,5) + (3,4,5) | trend_init(3,10,5) |
| 4 | F.pad(seasonal[-6:,:], (0,0,0,4)) | (3,6,5) | seasonal_init(3,10,5) |
| 5 | enc_embedding | (3,12,5) + (3,12,4) | enc_out(3,12,16) |
| 6 | Encoder(2×EncoderLayer + Layernorm) | (3,12,16) | enc_out(3,12,16) |
| 6a | ↳ AutoCorrelationLayer(FourierBlock,self-attn) | (3,12,16) | (3,12,16) |
| 6b | ↳ FourierBlock.forward(q,k,v,mask) | q(3,12,8,2)(k/v 忽略) | (3,8,2,12) |
| 6c | ↳ rfft + M=4 频率线性变换 + irfft | (3,8,2,12) | (3,8,2,12) |
| 7 | dec_embedding | (3,10,5) + (3,10,4) | dec_out(3,10,16) |
| 8 | Decoder(1×DecoderLayer + Layernorm + projection) | dec_out(3,10,16) + enc_out(3,12,16) | seasonal_part(3,10,5) + trend_part(3,10,5) |
| 8a | ↳ DecoderLayer self-attn(FourierBlock,dec_len=10) | (3,10,16) | (3,10,16) |
| 8b | ↳ DecoderLayer cross-attn(FourierCrossAttention) | q(3,10,8,2) + k(3,12,8,2) | (3,10,16) |
| 8c | ↳ FourierCrossAttention.forward | q(3,10,8,2), k(3,12,8,2), v 忽略 | (3,8,2,10) |
| 8d | ↳ xqk_ft 频域注意力矩阵 | xq_ft_(3,8,2,4) + xk_ft_(3,8,2,4) | xqk_ft(3,8,4,4) |
| 8e | ↳ tanh + V加权 + W投影 | (3,8,4,4) | xqkvw(3,8,2,4) |
| 8f | ↳ scatter + irfft | (3,8,2,4) → out_ft(3,8,2,6) | out(3,8,2,10) |
| 9 | dec_out = trend_part + seasonal_part | (3,10,5) + (3,10,5) | dec_out(3,10,5) |
| 10 | [:, -4:, :] 截取 | (3,10,5) | (3,4,5) |
3. 核心设计对比表
| 维度 | DLinear | Informer | Autoformer | Non-stationary | FEDformer | iTransformer |
|---|---|---|---|---|---|---|
| 论文会议 | ICLR 2023 | ICLR 2021 | NeurIPS 2021 | NeurIPS 2022 | ICML 2022 | ICLR 2024 |
| 核心问题 | O(L²)慢且复杂 | O(L²) 效率 | 忽略周期性 | 过平稳化 | Auto-Corr 仍局部 | 排列不变性 |
| Attention 类型 | 无 | ProbSparse | Auto-Correlation | DSAttention | FourierBlock + FourierCrossAttn | 倒置 FullAttention |
| Attention 复杂度 | — | |||||
| 时域 vs 频域 | 时域 Linear | 时域稀疏 | FFT 自相关→时域 | 时域修正分布 | 频域直接操作 | 时域倒置 |
| Q/K/V 用法 | 无 | 全部使用 | K,V 用于加权 | 全部使用+τδ | self: 只用Q;cross: K充V,v忽略 | 全部使用 |
| 分解 | 无 | 无 | Series Decomp | 无 | Series Decomp(同Autoformer) | Instance Norm |
| token 语义 | 时间步 | 时间步 | 时间步 | 时间步 | 时间步 | 变量 |
| 非平稳处理 | 无 | 无 | 趋势分解 | τ/δ 注入 | 趋势分解 | Instance Norm |
| Decoder 双路 | 无 | 无 | ✅ seasonal+trend | 无 | ✅ seasonal+trend(同Autoformer) | 无 |
| 多变量建模 | channel-independent | 混合 | 混合 | 混合 | 混合 | 跨变量注意力 |
4. FEDformer 设计决策回顾
4.1 频率稀疏假设是核心前提
所有 FourierBlock / FourierCrossAttention 的有效性依赖于:真实时序的有用信息集中在少数
mode_select="random" 是默认策略——通过随机扰动
4.2 FEDformer = Autoformer 骨架 + 注意力替换
FEDformer 对 Autoformer 的改动极小:
| 组件 | Autoformer | FEDformer | 变化 |
|---|---|---|---|
| Encoder self-attn | AutoCorrelation | FourierBlock | 替换 |
| Decoder self-attn | AutoCorrelation | FourierBlock | 替换 |
| Decoder cross-attn | AutoCorrelation | FourierCrossAttention | 替换 |
| series_decomp | series_decomp | series_decomp(同文件) | 不变 |
| EncoderLayer / DecoderLayer | 共用 | 共用 | 不变 |
| DataEmbedding_wo_pos | 共用 | 共用 | 不变 |
| my_Layernorm | 共用 | 共用 | 不变 |
| MOEDecomp(论文) | N/A | ⚠️ 未实现,用 series_decomp 替代 | TFB 简化 |
4.3 两个已知 Quirks
Quirk 1 — FourierBlock n_heads 硬编码:weights1/2 第一维写死为 8(不是 n_heads 参数)。实际后果:TFB 中 n_heads 强制为 8,否则 einsum 维度不匹配报错。FourierCrossAttention 已修正此问题(使用 num_heads)。
Quirk 2 — AutoCorrelationLayer view 语义偏差:FourierBlock/FourierCrossAttention 返回 (B, H, E, L) 形状,但 AutoCorrelationLayer.forward() 做 out.view(B, L, -1),把内存按"时间步第一"解读,实际每个"时间步"包含了不同 head 和不同时间位置的混合数据。模型收敛但语义不完全对应 Transformer 的"每时间步独立"约定。
4.4 三参数不可配置问题
version、mode_select、modes 是 FEDformer.__init__ 的 Python 默认参数,但 _init_model() 只传 self.config,三个参数永远取默认值 "fourier", "random", 32(实际 modes 受序列长度截断)。这是 TFB 接入层的实现细节,不影响模型正确性,但用户无法通过命令行调整频率选择策略。
4.5 v 在 FourierCrossAttention 中完全无效
FourierCrossAttention 接受 (q, k, v) 三元输入但只用 q 和 k(其中 k 同时充当 v)。传入的 v 执行了一次 permute 但后续从未被引用。这意味着 encoder 输出中的值向量 (v) 信息和键向量 (k) 信息完全相同,模型学习的是 K=V 约束下的频域注意力。
5. 推荐阅读路径回顾
| 目标 | 路径 |
|---|---|
| 快速理解设计直觉 | [[00-总览]] §二核心创新 → §八关键Quirks |
| 理解 TFB 如何调用 | [[01-Layer0-接入界面]] |
| 理解主预测链 | [[02-Layer1-forecast主链]] |
| 深入频域 self-attn | [[03A-Layer2A-FourierBlock]] §5.4 M频率线性变换 |
| 深入频域 cross-attn | [[03B-Layer2B-FourierCrossAttention]] §5.4-5.7 |
| 调试运行 | [[调试形参]] |