模块更新于 2026-11-20· 浏览 4· 下载 0
FAN 傅里叶分析网络
傅里叶级数进结构:FAN 层把 cos(Wx)、sin(Wx) 与激活分支拼接输出,正余弦分量显式编码周期模式,激活分支保留非周期表达能力
结构图

傅里叶级数进结构:FAN 层把 cos(Wx)、sin(Wx) 与激活分支拼接输出,正余弦分量显式编码周期模式,激活分支保留非周期表达能力
论文信息
- 论文:FAN: Fourier Analysis Networks
- 出处:arXiv 2024
核心痛点
MLP、Transformer 乃至 KAN 拟合周期函数基本靠"死记硬背",一到域外(OOD)外推就崩,连 sin(x) 都拟合不好;根源在于结构里没有理解周期性的显式机制,泛化能力被训练数据的规模和多样性锁住。
模块亮点
- 傅里叶级数进结构:FAN 层把 cos(Wx)、sin(Wx) 与激活分支拼接输出,正余弦分量显式编码周期模式,激活分支保留非周期表达能力
- 域外照样能推:拟合周期函数时,测试数据无论在训练域内还是域外都显著优于 MLP / KAN / Transformer;符号公式表示任务上参数越多优势越大,KAN 反而性能下滑
- 又省又涨:FAN 层可无缝替换 MLP 层,参数量和 FLOPs 更少;Transformer 里的 MLP 换成 FAN 后,时序预测 MSE 平均降 14.3%~15.0%、MAE 降 7.6%~7.9%,语言建模 Loss 最高降 14.65%、准确率最高升 8.50%
适用场景
领域为通用网络架构 / 序列建模;原文验证了周期函数拟合、符号公式表示、时间序列预测和语言建模;凡是用 MLP 的地方都可以替换,尤其适合时序、金融、语音等暗含周期规律的任务。
插入位置建议
- 替换 Transformer 块中的 MLP / FFN 层,原文就是这么做的
- 替代 MLP 直接堆叠成骨干网络,用于函数拟合、符号公式表示等数学推理任务
- 放进时序预测、语言模型等序列模型的前馈部分,与注意力、循环或 SSM 结构自由组合
适用任务
- 通用网络架构
- 序列建模
完整代码
fan.py
import torch
import torch.nn as nn
import torch.nn.functional as F
"""《FAN: Fourier Analysis Networks》
尽管神经网络,特别是以 MLP 和 Transformer 为代表的神经网络取得了显着的成功,但我们揭示了它们在周期性的建模和推理方面表现出潜在的缺陷,
即它们倾向于记住周期性数据,而不是真正理解周期性的基本原理。
然而,周期性是各种形式的推理和泛化的关键特征,通过观察中的重复模式支撑自然系统和工程系统的可预测性。
在本文中,我们提出了 FAN,这是一种基于傅里叶分析的新型网络架构,它能够有效地对周期性现象进行建模和推理。
通过引入傅里叶级数,周期性自然地集成到神经网络的结构和计算过程中,从而实现对周期性模式的更准确表达和预测。
作为多层感知器 (MLP) 的有前途的替代品,FAN 可以在各种模型中无缝替代 MLP,参数和 FLOP 更少。
通过广泛的实验,我们证明了 FAN 在周期函数建模和推理方面的有效性,以及 FAN 在一系列实际任务中的优越性和泛化性,包括符号公式表示、时间序列预测和语言建模。
"""
class FANLayer(nn.Module):
"""
FANLayer: The layer used in FAN (https://arxiv.org/abs/2410.02675).
Args:
input_dim (int): The number of input features.
output_dim (int): The number of output features.
p_ratio (float): The ratio of output dimensions used for cosine and sine parts (default: 0.25).
activation (str or callable): The activation function to apply to the g component. If a string is passed,
the corresponding activation from torch.nn.functional is used (default: 'gelu').
use_p_bias (bool): If True, include bias in the linear transformations of p component (default: True).
There is almost no difference between bias and non-bias in our experiments.
"""
def __init__(self, input_dim, output_dim, p_ratio=0.25, activation='gelu', use_p_bias=True):
super(FANLayer, self).__init__()
# Ensure the p_ratio is within a valid range
assert 0 < p_ratio < 0.5, "p_ratio must be between 0 and 0.5"
self.p_ratio = p_ratio
p_output_dim = int(output_dim * self.p_ratio)
g_output_dim = output_dim - p_output_dim * 2 # Account for cosine and sine terms
# Linear transformation for the p component (for cosine and sine parts)
self.input_linear_p = nn.Linear(input_dim, p_output_dim, bias=use_p_bias)
# Linear transformation for the g component
self.input_linear_g = nn.Linear(input_dim, g_output_dim)
# Set the activation function
if isinstance(activation, str):
self.activation = getattr(F, activation)
else:
self.activation = activation
def forward(self, src):
"""
Args:
src (Tensor): Input tensor of shape (batch_size, input_dim).
Returns:
Tensor: Output tensor of shape (batch_size, output_dim), after applying the FAN layer.
"""
# Apply the linear transformation followed by the activation for the g component
g = self.activation(self.input_linear_g(src))
# Apply the linear transformation for the p component
p = self.input_linear_p(src)
# Concatenate cos(p), sin(p), and activated g along the last dimension
output = torch.cat((torch.cos(p), torch.sin(p), g), dim=-1)
return output
if __name__ == '__main__':
input = torch.rand(8,16)
block = FANLayer(input_dim=16, output_dim=16, p_ratio=0.25, activation='gelu', use_p_bias=True)
output = block(input)
print(input.size())
print(output.size())环境与依赖
Python 3.9+
继续浏览
下一篇FAN 频率自适应归一化频域剥非平稳:对每个输入实例做 DFT,TopK 挑出幅度最大的 K 个主频分量并从输入中减去,骨干只预测平稳残差;FFT 复杂度 O(L log L),GPU 友好相关资源
模块2026-11-20
ICB 交互卷积模块
大小核互相门控:两条并行 1D 卷积分支(核 1 与核 3),分别抓局部与更长依赖;各自 GELU 后与对方逐元素相乘,再相加经第三层卷积融合输出
时间序列ICB交互卷积
时间序列MIT
模块2026-11-20
MDM 多尺度可分解混合模块
先分解、再混合:用平均池化逐级下采样得到多个从细到粗的时间模式,再从最粗尺度向最细尺度做前馈残差混合,细粒度细节与粗粒度趋势互补增强,全程纯 MLP、无自注意力
时间序列MDM多尺度分解
时间序列MIT
模块2026-11-20
AdaWave 自适应小波块
小波基可学习:用提升方案的 split–predict–update 三步做小波分解,预测和更新算子全部换成可学习的分组一维卷积,小波基跟着数据自适应学出来,逐级输出低频近似和高频细节
时间序列AdaWave小波变换
时间序列MIT