1. 程式人生 > >ICLR 2020 | 拋開卷積,multi-head self-attention能夠表達任何卷積操作

ICLR 2020 | 拋開卷積,multi-head self-attention能夠表達任何卷積操作

> 近年來很多研究將nlp中的attention機制融入到視覺的研究中,得到很不錯的結果,於是,論文側重於從理論和實驗去驗證self-attention可以代替卷積網路獨立進行類似卷積的操作,給self-attention在影象領域的應用奠定基礎 **論文: On the Relationship between Self-Attention and Convolutional Layers** ![](https://upload-images.jianshu.io/upload_images/20428708-9d587ea0e40c1328.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) * **論文地址:[https://arxiv.org/abs/1911.03584](https://arxiv.org/abs/1911.03584)** * **論文程式碼:[https://github.com/epfml/attention-cnn](https://github.com/epfml/attention-cnn)** # Introduction ***   transformer的提出對NLP領域的研究有很大的促進作用,得益於attention機制,特別是self-attention,會考慮詞間的相似性,對當前詞進行加權輸出。受到詞間關係學習的啟發,self-attention也開始用於視覺任務中,但大都是attention和convonlution的結合。Ramachandran在19年的研究中,用full attention model達到了resnet baseline的精度,模型引數和計算量相比卷積網路減輕了不少   因此,論文主要研究self-attention layer在圖片處理上是否能達到convolutional layer的效果,貢獻如下: * 在理論層面,論文通過構造性證明self-attention layers能夠替代任何卷積層 * 在實際層面,論文通過構造multi-head self-attention layer進行實驗,證明attention-only架構的前幾層的確學習到了關注query pixel附近的g網格區域特徵 # Background on attention mechanisms for vision *** ### The multi-head self-attention layer   定義$X\in \mathbb{R}^{T\times D_{in}}$為輸入矩陣,包含$T$個$D_{in}$維的token,在NLP中,token對應著序列化的詞,同樣地也可以對應序列化的畫素 ![](https://upload-images.jianshu.io/upload_images/20428708-4ae132f9d1fa5fd6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![](https://upload-images.jianshu.io/upload_images/20428708-b3734c90e2cb9539.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)   self-attention layer從$D_{in}$到$D_{out}$的計算如公式1,2所示,$A$為attention scores,softmax將score轉換為attention probabilities,該層的引數包含查詢矩陣(query matrix)$W_{qry}\in \mathbb{R}^{D_{in}\times D_k}$,關鍵詞矩陣(key matrix)$W_{key}\in \mathbb{R}^{D_{in}\times D_k}$,值矩陣(value matrix)$W_{val}\in \mathbb{R}^{D_{in}\times D_{out}}$,都用於對輸入進行變化,基本跟NLP中的self-attention一致 ![](https://upload-images.jianshu.io/upload_images/20428708-1e06e9722163d728.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)   因為只考慮相關性,self-attention一個很重要的屬性是,不管輸入的順序如何改變,輸出都是不變的,這對於希望順序對結果有影響的case影響很大,因此在self-attention基礎上為每個token學習一個positional encoding引數,$P\in \mathbb{R}^{T\times D_{in}}$為包含位置資訊的embedding向量,可以有多種形式 ![](https://upload-images.jianshu.io/upload_images/20428708-ef33900a2f9b47fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)   這裡採用multiple heads版本的self-attention,每個head的引數矩陣都不一樣,能夠提取不同的特徵,$N_h$個head輸出$D_h$維結果concat後對映成$D_{out}$維的最終輸出,兩個新引數,對映矩陣(projection matrix)$W_{out}\in \mathbb{R}^{N_hD_h\times D_{out}}$,偏置$b_{out}\in \mathbb{R}^{D_{out}}$ ### Attention for images ![](https://upload-images.jianshu.io/upload_images/20428708-e4a2b63587ebda82.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![](https://upload-images.jianshu.io/upload_images/20428708-8b9d96170ecc202d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)   卷積是最適合神經網路的圖片操作方法,給予圖片$X\in \mathbb{R}^{W\times H\times D_{in}}$,卷積在$(i,j)$的操作如公式5,$W\in \mathbb{R}^{K\times K\times D_{in}\times D_{out}}$,$b\in \mathbb{R}^{D_{out}}$,K為卷積核的大小 ![](https://upload-images.jianshu.io/upload_images/20428708-b56cbcb06dc8d9dc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)   在圖片上應用self-attention,定義查詢畫素和關鍵詞畫素$q,k\in[W]\times [H]$,輸入的向量大小為$X\in \mathbb{R}^{W\times H\times D_{in}}$為了保持一致性,用1D的符號來代表2D座標,比如$p=(i,j)$,用$X_p$代表$X_{ij}$,用$A_p$代表$A_{ij}$ ### Positional encoding for images   位置編碼目前主要有兩種,分別是絕對位置(absolute)編碼和相對(relative)位置編碼 ![](https://upload-images.jianshu.io/upload_images/20428708-6f5e035c02fba956.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)   在絕對位置編碼中,每個畫素擁有一個位置向量$P_p$(學習的或固定的),於是公式2可以轉換為公式7 ![](https://upload-images.jianshu.io/upload_images/20428708-92953465bb6fa134.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)   相對位置編碼的核心是隻考慮查詢畫素和查詢畫素之間的位置差異,如公式8,大體是將公式7的每一項的絕對位引數改為相對位置引數。attention scores只跟偏移$\delta:=k-q$,$u$和$v$是learnable引數,每個head都不一樣,而每個偏移的相對位置編碼$r_\delta\in \mathbb{R}^{D_p}$是head共享的。關鍵詞權重分成了兩部分,$W_{key}$屬於輸入,$\widehat {W}_{key}$屬於偏移 ![](https://upload-images.jianshu.io/upload_images/20428708-33abae4f6eceb7c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)   公式9稱為二次編碼(quadratic encoding),引數$\Delta^{(h)}=(\Delta_1^{(h)},\Delta_2^{(h)})$和$\alpha^{(h)}$分別代表中心點以及attention區域的大小,都是通過學習得來的,而$\delta=(\delta_1,\delta_2)$則是固定的,代表查詢畫素和關鍵詞畫素的相對位移 # Self-attention as a convolutional layer *** ![](https://upload-images.jianshu.io/upload_images/20428708-5b4bde3549b09128.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)   定理1,對於multi-head self-attention,$N_h$個head,每個head輸出$D_h$維,整體最終輸出$D_{out}$,相對位置編碼$D_p\ge 3$維,可以表示任何卷積,核大小為$\sqrt{N_h}\times \sqrt{N_h}$,output channel為$min(D_h,D_{out})$   對於output channel不是固定$D_{out}$,論文認