从ViT开始,Transformer在视觉领域得到应用。为了降低Transformer中softmax attention操作的计算量和显存,Sparse Global Attention被提出和使用,例如在PVT和PVT v2中。另一种策略是Window attention,如Swin Transformer提出一种Shifted Window方法让ViT具有多尺度的能力,并且降低计算量,能接受大分辨率的图片;Cross-Shaped Window attention则有一半的head分别进行纵向和横向的窗口attention;ViTdet则提出一种local attention+global attention的方法解决该问题。还有一种策略是Linear Attention。

Linear Attention(带有线性归一化) 相比 Softmax Attention有两个点不到位,第一个点是Softmax能让大的更大小的更小,而Linear的出来后比较平滑。第二点是Softmax计算出的Attention map是满秩的,而Linear计算出的是不满秩的,也就是说Linear计算出的对于V的选择是过于同质化的。针对这两点,Flatten Transformer(ICCV2023)提出一种改进名为Focused Linear Attention,旨在弥补二者的性能差距,计算方法如下:
$$O=\phi(Q) \phi(K)^T V+\operatorname{DWC}(V) .$$
DWC是depth-wise convolution,其中计算相似度的过程如下:
$$
\begin{gathered}
\operatorname{Sim}\left(Q_i, K_j\right)=\phi_p\left(Q_i\right) \phi_p\left(K_j\right)^T \\
\text { where } \phi_p(x)=f_p(\operatorname{ReLU}(x)), f_p(x)=\frac{\|x\|}{\left\|x^{* * p}\right\|} x^{* * p}
\end{gathered}
$$
$x^{* * p}$ 表示对x逐位取p次方。

不过Flatten Transformer在应用在各种网络上比如Swin-T中的时候也不是所有的都替换,论文实验表示只换前两个stage比较好,超参p=3。比其他Linear Attention好,但是计算量要求不严格时,比Softmax Attention没特别大优势,提速1.5~2倍吧。

标签: Transformer

添加新评论