[论文解读] cosFormer: Rethinking Softmax in Attention
cosFormer 替代 attention 中的 softmax,用线性、非负、余弦重加权机制,在语言建模、下游任务和长程基准测试上实现线性时间/空间复杂度,同时达到或超过原始 Transformer 的性能。
Transformer has shown great successes in natural language processing, computer vision, and audio processing. As one of its core components, the softmax attention helps to capture long-range dependencies yet prohibits its scale-up due to the quadratic space and time complexity to the sequence length. Kernel methods are often adopted to reduce the complexity by approximating the softmax operator. Nevertheless, due to the approximation errors, their performances vary in different tasks/corpus and suffer crucial performance drops when compared with the vanilla softmax attention. In this paper, we propose a linear transformer called cosFormer that can achieve comparable or better accuracy to the vanilla transformer in both casual and cross attentions. cosFormer is based on two key properties of softmax attention: i). non-negativeness of the attention matrix; ii). a non-linear re-weighting scheme that can concentrate the distribution of the attention matrix. As its linear substitute, cosFormer fulfills these properties with a linear operator and a cosine-based distance re-weighting mechanism. Extensive experiments on language modeling and text understanding tasks demonstrate the effectiveness of our method. We further examine our method on long sequences and achieve state-of-the-art performance on the Long-Range Arena benchmark. The source code is available at https://github.com/OpenNLPLab/cosFormer.
研究动机与目标
- 研究是否可以在不牺牲性能的情况下,用线性算子替代 softmax 注意力。
- 开发一个保留非负性并通过基于余弦的重加权引入新近性/局部性偏置的线性注意力变体。
- 在自回归和双向语言建模、下游分类任务以及长距离序列基准测试上评估 cosFormer。
提出的方法
- 用 ReLU 构建的线性核替代基于 softmax 的相似度,以确保非负性。
- 通过将注意力计算分解为 phi(Q) 和 phi(K) 的方式并进行线性投影,以实现线性时间/空间。
- 引入基于余弦的重加权机制以模拟 softmax 的局部性和稳定化效果,使用以 Ptolemy 为基础的分解。
- 推导 cosFormer 的公式,使 O = Q'K'^{T} cos(...)V 将分解为两个线性项的和以实现高效计算。
- 在自回归、双向和长距离任务上,与基于核的和基于模式的高效 Transformer 进行比较。
实验结果
研究问题
- RQ1在保持非负性和局部性等注意力属性的情况下,softmax 注意力是否能够有效地被线性运算所替代?
- RQ2基于余弦的重加权机制是否在各种 NLP 任务中提供与 softmax 相似的稳定性和局部性?
- RQ3cosFormer 在自回归和双向语言建模,以及下游分类和长距离基准测试中的表现如何?
主要发现
- cosFormer 在语言建模和下游任务中达到或优于原始 Transformer 的准确性,且具备线性时间/空间复杂度。
- cosFormer 在 WikiText-103 自回归任务上展现出更快的收敛速度和具竞争力的困惑度,与若干基线相比。
- 在下游分类任务(GLUE、IMDB、AMAZON)上,cosFormer 与原始 Transformer 的性能相匹配或接近,并优于许多高效 Transformer。
- 在 Long-Range Arena 基准测试中,cosFormer 实现了最先进或具有竞争力的结果,包括所有任务的最佳平均分数。
- 消融实验表明基于余弦的重加权提高了收敛速度和最终性能,表明局部性偏置对自然语言建模有益。
阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。