Attention Mechanism

Attention

( K , V ) = \left[ \left( \mathbf { k } _ { 1 } , \mathbf { v } _ { 1 } \right) , \cdots , \left( \mathbf { k } _ { N } , \mathbf { v } _ { N } \right) \right]表示N個輸入信息,給定任務相關(guān)的查詢向量\mathbf q時,注意力函數(shù)為:
\begin{aligned} \operatorname { att } ( ( K , V ) , \mathbf { q } ) & = \sum _ { i = 1 } ^ { N } \alpha _ { i } \mathbf { v } _ { i } \\ & = \sum _ { i = 1 } ^ { N } \frac { \exp \left( s \left( \mathbf { k } _ { i } , \mathbf { q } \right) \right) } { \sum _ { j } \exp \left( s \left( \mathbf { k } _ { j } , \mathbf { q } \right) \right) } \mathbf { v } _ { i } \end{aligned}
其中s \left( \mathbf { k } _ { j } , \mathbf { q } \right)為score function,表示\mathbf {k} _ {j}在查詢向量\mathbf q的注意力大小。

在絕大多數(shù)場景中,K=V。

下表總結(jié)了常用的score function的計算方法

Name score function Citation
Additive/Concat s \left( \mathbf { k } _ { j } , \mathbf { q } \right) = v ^ { \top } \tanh \left( \mathbf { W }\mathbf { q } + \mathbf { U } \mathbf { k } _ { j } \right) Bahdanau at al.,2015
Location s \left( \mathbf { k } _ { j } , \mathbf { q } \right)= \mathbf { W } \mathbf { q } Luong at al.,2015
General s \left( \mathbf { k } _ { j } , \mathbf { q } \right)= \mathbf { q } ^ { \top } \mathbf { W }\mathbf { k } _ { j } Luong at al.,2015
Dot Product s \left( \mathbf { k } _ { j } , \mathbf { q } \right)= \mathbf { q } ^ { \top } \mathbf { k } _ { j } Luong at al.,2015
Scaled Dot-Product s \left( \mathbf { k } _ { j } , \mathbf { q } \right)= \frac { \mathbf { q } ^ { \top }\mathbf { k } _ { j } } { \sqrt { n } } Vaswani at al.,2017

Self-Attention

假設輸入序列為X = \left[ \mathbf { x } _ { 1 } , \cdots , \mathbf { x } _ { N } \right] \in \mathbb { R } ^ { d _ { 1 } \times N },輸出序列為H = \left[ \mathbf { h } _ { 1 } , \cdots , \mathbf { h } _ { N } \right] \in \mathbb { R } ^ { d _ { 2 } \times N },首先我們可以通過線性變換得到三組向量序列:
\begin{array} { l } { Q = W _ { Q } X \in \mathbb { R } ^ { d _ { 3 } \times N } } \\ { K = W _ { K } X \in \mathbb { R } ^ { d _ { 3 } \times N } } \\ { V = W _ { V } X \in \mathbb { R } ^ { d _ { 2 } \times N } } \end{array}
其中Q, K, V分別為查詢向量序列,鍵向量序列和值向量序列,W_Q, W_K, W_V分別為可學習的參數(shù)矩陣。
輸出向量\mathbf { h } _ { i }可通過以下方法計算得到:
\begin{aligned} \mathbf { h } _ { i } & = \operatorname { att } \left( ( K , V ) , \mathbf { q } _ { i } \right) \\ & = \sum _ { j = 1 } ^ { N } \alpha _ { i j } \mathbf { v } _ { j } \\ & = \sum _ { j = 1 } ^ { N } \operatorname { softmax } \left( s \left( \mathbf { k } _ { j } , \mathbf { q } _ { i } \right) \right) \mathbf { v } _ { j } \end{aligned}
其中i , j \in [ 1 , N ]為輸出和輸入向量序列的位置,連接權(quán)重\alpha_{ij} 由注意力機制動態(tài)生成。由于自注意力模型的權(quán)重是動態(tài)生成的,因此可以處理變長的信息序列。

自注意力模型計算的權(quán)重\alpha_{ij}只依賴\mathbf{q} _ {i}\mathbf {k} _ j的相關(guān)性,而忽略了輸入信息的位置信息。因此在單獨使用時,自注意力模型一般需要加入位置編碼信息來進行修正Vaswani at al., 2017

最新研究

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容