天天看點

遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結

遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結

前言

Attention 自2015年被提出後,在 NLP 領域,圖像領域遍地開花。Attention 賦予模型區分辨識能力,從紛繁的資訊中找到應當 focus 的重點。2017年 self attention 的出現,使得 NLP 領域對詞句 representation 能力有了很大的提升,整個 NLP 領域開啟了全面擁抱 transformer 的年代。

本文會主要從2個方面來介紹 Attention。

初識 Attention,主要扒一扒 Attention 的曆史,然後給出一個通用的架構來回答一個終極問題:what is Attention?

細數 Attention,以上文給出的通用架構視角來審視所有的 Attention,在這個章節,你會和各種各樣的 Attention 相遇、相識、相戀(global/local、soft/hard、Bagdanau attention、 Luong attention、 self-attention、 multi-head attention , 以及它們的别名),了解它們之間的聯系與差異。

初識Attention

History

Attention 的發展可以粗暴地分為兩個階段。

2015-2017年,自從 attention 提出後,基本就成為 NLP 模型的标配,各種各樣的花式 attention 鋪天蓋地。不僅在 Machine Translation,在 Text summarization,Text Comprehend(Q&A), Text Classification 也廣泛應用。奠定基礎的幾篇文章如下:

2015年 ICLR 《Neural machine translation by jointly learning to align and translate》首次提出 attention(基本上算是公認的首次提出),文章提出了最經典的 Attention 結構(additive attention 或者 又叫 bahdanau attention)用于機器翻譯,并形象直覺地展示了 attention 帶來源語目智語的對齊效果,解釋深度模型到底學到了什麼,人類表示服氣。

2015年 EMNLP 《Effective Approaches to Attention-based Neural Machine Translation》在基礎 attention 上開始研究一些變化操作,嘗試不同的 score-function,不同的 alignment-function。文章中使用的 Attention(multiplicative attention 或者 又叫 Luong attention)結構也被廣泛應用。

2015年 ICML 《Show, Attend and Tell: Neural Image Caption Generation with Visual Attention》是 attention(提出hard/soft attention的概念)在 image caption 上的應用,故事圓滿,符合直覺,人類再次表示很服氣。

在上面幾篇奠基之作之上,2016和2017年 attention 開枝散葉,無往不利。Hiearchical Attention,Attention over Attention,multi-step Attention……這些或叫得上名的或叫不上名。

2017年-至今是屬于 transformer 的時代。基于 transformer 強大的表示學習能力,NLP 領域爆發了新一輪的活力,BERT、GPT 領跑各項 NLP 任務效果。奠基之作無疑是:

2017年 NIPS《Attention is all you need》提出 transformer 的結構(涉及 self-attention,multi-head attention)。基于 transformer 的網絡可全部替代sequence-aligned 的循環網絡,實作 RNN 不能實作的并行化,并且使得長距離的語義依賴與表達更加準确(據說2019年的 transformer-xl《Transformer-XL:Attentive Lanuage Models Beyond a fixed-length context》通過片段級循環機制結合相對位置編碼政策可以捕獲更長的依賴關系)。

what is Attention ?

直奔主題,終極叩問 "what is attention?" 。這個章節,嘗試以統一的抽象架構來定義 attention。如同先編寫一個抽象類,後續章節涉及所有的 attention 都繼承于這個抽象類。這裡我寫了兩個抽象類,一個叫 alignment-based,一個叫 memroy-based。(兩名字我給起的,保留最終解釋權)。

  • alignment-based

如下圖所示的 model setting,輸入 c(context,有的論文寫s),y(input,有的地方也寫作 h),輸出 z。圖中,英文表達原汁原味,細品一下。

遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結

image.png

圖檔來源:

https://towardsdatascience.com/memory-attention-sequences-37456d271992

我們細拆 Attention Model,以經典的 Bahdanau attention 為例,看看抽象出來的三部曲:

  • score function :度量環境向量與目前輸入向量的相似性;找到目前環境下,應該 focus 哪些輸入資訊;
遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結
  • alignment function :計算 attention weight,通常都使用 softmax 進行歸一化;
遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結
  • generate context vector function :根據 attention weight,得到輸出向量;
遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結

下圖,更加直覺地展示這三個接口的位置:

遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結

自此之後,要認清一個 attention 的詳情,隻需要搞清楚這三個部分,所有的變換都是在3個位置進行調整,當然變化最豐富的是 score function。在後一個章節會詳細對比不同種類的 attention 在這三個次元上的變換。

  • memory-based

另一種視角是 QKV 模型,假設輸入為 q,Memory 中以(k,v)形式存儲需要的上下文。感覺在 Q&A 任務中,這種設定比較合理,transformer 是采用的這種模組化方式。k 是 question,v 是 answer,q 是新來的 question,看看曆史 memory 中 q 和哪個 k 更相似,然後依葫蘆畫瓢,根據相似 k 對應的 v,合成目前 question 的 answer。

遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結
https://zhuanlan.zhihu.com/p/35571412

在這種模組化方式下,也分為三步:

  • address memory (score function):,在 memory 中找相似;
  • normalize(alignment function) :
  • read content (gen context vector function) :

其實還是沒有逃出上文三部曲的架構。隻是将 input 分裂成了 kvpair。

後文都會以統一的三部曲模組化方式(score function,alignment function,generate context vector function)來分析所有 attention。

Attention in Detail

在上文,我們 high-level 地了解了 attention 三部曲的模組化方式,接下來要把所有Attention 拉出來排排坐。

Framework

如下圖,通常聽到的一些 attention,他們的差異其實主要展現在 score-function 層面,其次是展現在 generate context vector function 的層面。我們分别來看看,這些 attention 之間的差異與聯系。

遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結
  • generate context vector function

hard / soft attention 是在文章《Show, Attend and Tell: Neural Image Caption Generation with Visual Attention》提出的概念,最直覺的一種了解是,hard attention 是一個随機采樣,采樣集合是輸入向量的集合,采樣的機率分布是alignment function 産出的 attention weight。是以,hard attention 的輸出是某一個特定的輸入向量。soft attention 是一個帶權求和的過程,求和集合是輸入向量的集合,對應權重是 alignment function 産出的 attention weight。hard / soft attention 中,soft attention 是更常用的(後文提及的所有 attention 都在這個範疇),因為它可導,可直接嵌入到模型中進行訓練,hard attention 文中 suggests a Monte Carlo based sampling approximation of gradient。

  • alignment function

在 soft attention 中,又劃分了 global/local attention(In this paper :《Effective Approaches to Attention-based Neural Machine Translation》)。

直覺了解就是帶權求和的集合不一樣,global attention 是所有輸入向量作為權重集合,使用 softmax 作為 alignment function,local 是部分輸入向量才能進入這個池子。為什麼用 local,背後邏輯是要減小噪音,進一步縮小重點關注區域。接下來的問題就是,怎麼确定這個 local 範圍?文中提了兩個方案 local-m 和 local-p。local-m 基于的假設生硬簡單,就直接 pass了。local-p 有一個預估操作,預計目前時刻應該關注輸入序列(總長度為S)的什麼位置 pt(引入了兩個參數向量,vp,wp),然後在 alignment function 中做了一點兒調整,在 softmax 算出來的attention wieght 的基礎上,加了一個以 pt 為中心的高斯分布來調整 alignment 的結果。

作者最後闡述 local-p + general(score-function 參考上圖中multiplicative attention 中的 general 版本)的方式效果是最好的。但從global/local 視角的分類來看,更常用的依然還是 global attention,因為複雜化的local attention 帶來的效果增益感覺并不大。

遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結
  • score-function

如何生成輸出向量,有上面提及的那些變換。接下來是變化更加豐富的 score function。最為常用的 score function 有上文圖中的那幾種(基本全乎了吧)。其實本質就是度量兩個向量的相似度。如果兩個向量在同一個空間,那麼可以使用 dot 點乘方式(或者 scaled dot product,scaled 背後的原因是為了減小數值,softmax 的梯度大一些,學得更快一些),簡單好使。如果不在同一個空間,需要一些變換(在一個空間也可以變換),additive 對輸入分别進行線性變換後然後相加,multiplicative 是直接通過矩陣乘法來變換(你是不是也曾迷惑過為什麼attention 要叫做 additive 和 multiplicative attention?)。

後文我們将介紹幾個具有代表性的 attention,通過具體的 attention example 來進一步了解。以及一些花樣 attention,看大家都怎樣變着法兒用 attention。

Bahdanau Attention & Luong Attention

在對比之中,認知更清晰,一圖表達所有。這兩個 Attention 就是整個 Attention 的奠基之作。Tensorflow 中實作了這兩種 Attention 的 API。

遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結

Self Attention & Multi-head Attention

  • why self attention ?

有很多文章寫 self-attention,但是寫 why self-attention 的并不多。是以打算多花點筆墨來寫 why。

RNN 的長距離依賴比較 tricky:RNN 很強大(可以作為 encoder 對長度任意的序列進行特征抽取,基于特征抽取的能力可以勝任分類任務,另一方面可以作為Generators 學習 Language Model),其實核心就是長距離依賴(gate architectures - 線性操作讓資訊可以保持并流動,并選擇性地讓資訊通過),可以對長度任意的序列進行表達,但是這種方式還是比較 tricky。并且這種序列模組化方式,無法對具有層次結構的資訊進行很好的表達。

RNN 由于遞歸的本質,導緻無法并行。

遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結

CNN 在 NLP 中扮演了 n-gram 的 detector 角色,在層内可以并行。CNN works well,基于的假設是局部資訊互相依賴。CNN 具有 Hierarchical Receptive Field,使得任意任意兩個位置之間的長度距離是對數級别的。

遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結

是以有沒有一種方法,能夠做到既能又能還能?

相對于 CNN,要 constant path length 不要 logarithmic path length , 要 variable-sized perceptive field,不要固定 size 的 perceptive field;

相對于 RNN,考慮長距離依賴,還要可以并行!

這就是 self attention。下圖可以看到 self-attention 和 convolution 有點兒神似,它摒棄了 CNN 的局部假設,想要尋找長距離的關聯依賴。看下圖就可以了解 self-attention 的這幾個特點:

  • constant path length & variable-sized perceptive field :任意兩個位置(特指遠距離)的關聯不再需要通過 Hierarchical perceptive field 的方式,它的 perceptive field 是整個句子,是以任意兩個位置建立關聯是常數時間内的。
  • parallelize : 沒有了遞歸的限制,就像 CNN 一樣可以在每一層内實作并行。
遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結
https://nlp.stanford.edu/seminar/details/lkaiser.pdf

self-attention 借鑒 CNN中 multi-kernel 的思想,進一步進化成為 Multi-Head attention。每一個不同的 head 使用不同的線性變換,學習不同的 relationship。

  • what is self-attention?

已經有很多很好的文章介紹 transformer 和 self-attention,以及内部細節。有興趣的同學可以看下參考資料【11】,介紹得比較詳細,下圖是完整版本的 multi-head attention 的示例圖(引用自上述連結中)。這是基于上文中提及了 QKV 的 memory-based 的模組化方式。需要說明的幾個點:

  1. QKV 都是對輸入 x 的線性映射。
  2. score-function 使用 scaled-dot product。
  3. multihead 的方式将多個 head 的輸出 z,進行 concat 後,通過線性變換得到最後的輸出 z。
遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結

transformer 架構中 self-attention 本身是一個很大的創新,另一個有意思的是 three ways of attention 的設計。attention weight 一列以英譯中,encoder 輸入machine learning,decoder 輸入機器學習。

  1. Encoder self-attention:Encoder 階段捕獲目前 word 和其他輸入詞的關聯;
  2. MaskedDecoder self-attention :Decoder 階段捕獲目前 word 與已經看到的解碼詞之間的關聯,從矩陣上直覺來看就是一個帶有 mask 的三角矩陣;
  3. Encoder-Decoder Attention:就是将 Decoder 和 Encoder 輸入建立聯系,和之前那些普通 Attention 一樣;
遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結

在 transformer 中除了上訴提及的東西,還有 positional encoding,residuals 這些小而美的東西。在複雜度方面在原文中也與 RNN-CNN 進行了對比。

花樣 Attention

下面簡要介紹幾種花樣的 attention:

RNN 對序列模組化,但是缺乏層次資訊。而語言本身是具有層次結構,短語組成句子,句子組成篇章。是以研究者十分希望把語言中的這些層次結構在模型中得以展現,Hierarchical 的方式就出現了。《Hierarchical Attention Networks for Document Classification》,從 word attention 到 sentence attention,如下圖一。

在比對或者檢索任務中(如Q&A,IR),要衡量 query,doc 相似度,這時候attention 的方法中,query 和 doc 就互為對方的 cotext,query 對 doc 算一次attention,doc對query 算一次 attention,《Attention-over-Attention Neural Networks for Reading Comprehension 》,如下圖二。

上文介紹 why self-attention 時已經提及了 RNN 和 CNN 的一些優點和問題,幾乎和 transformer 同時,facebook 發表了《Convolutional Sequence to Sequence Learning》,同樣地想借用 CNN 的優點來補足 RNN 不能并行的弱點,用 CNN 結合 attention 來對序列進行模組化,如下圖三。

随着 transformer 的爆紅,圍繞 transformer 的花邊,出現了 weighted-transformer 《Weighted Transformer Network For Machine Translation》。今年出現了 transformer-xl 《Transformer-xl :attentive language models beyond a fixed-length context》,如下圖四, 想達到對任意長度的輸入進行特征抽取,而不是 transformer 切成 segment 的定長輸入。

遍地開花的 Attention ,你真的懂嗎?前言初識AttentionAttention in Detail總結

總結

Why Attention Works?

從上面的模組化,我們可以大緻感受到 Attention 的思路簡單,四個字“帶權求和”就可以高度概括,大道至簡。做個不太恰當的類比,人類學習一門新語言基本經曆四個階段:死記硬背(通過閱讀背誦學習文法練習語感)->提綱挈領(簡單對話靠聽懂句子中的關鍵詞彙準确了解核心意思)->融會貫通(複雜對話懂得上下文指代、語言背後的聯系,具備了舉一反三的學習能力)->登峰造極(沉浸地大量練習)。

這也如同attention的發展脈絡,RNN 時代是死記硬背的時期,attention 的模型學會了提綱挈領,進化到 transformer,融彙貫通,具備優秀的表達學習能力,再到 GPT、BERT,通過多任務大規模學習積累實戰經驗,戰鬥力爆棚。

要回答為什麼 attention 這麼優秀?是因為它讓模型開竅了,懂得了提綱挈領,學會了融會貫通。

那又是如何開竅的?是因為它懂得了"context is everything"。

1.在語言模型中:語言模型(language model)是整個 NLP 領域的基礎,語言模型的精準程度基本上直接掌握所有 NLP 任務效果的命脈。而 context 又掌握着語言模型的命脈,語義不孤立,在特定 context 下展示特定的一面,模型如果可以學習到這些知識,就可以達到見人說人話,見鬼說鬼話的理想狀态。

在語義表達上能把 context 用好的都是成功的典範(參考:word2vec 靠學習 word 及其 context 發家,ELMo-deep contextualized word representations, BERT 從句子中摳掉一個詞用上下文去預測這個詞,transformer-xl 較 transformer 使用更全面的 context 資訊,XLNet 一大重要貢獻也是研究如何使用上下文資訊來訓練語言模型)。

2.在其他領域中:Attention 是把 context 用好的典範之一。Attention 背後本質的思想就是:在不同的 context 下,focusing 不同的資訊。這本來就是一個普适的準則。是以 Attention 可以用到所有類似需求的地方,不僅僅是 NLP,圖像,就看你對 context 如何定義。

在很多的應用場景,attention-layer 肩負起了部分 feature-selection,featue-representation 的責任。舉個例子,transfer learning with Domain-aware attention network for item recommemdation in e-commerce 中提及:不同場景的使用者的行為有不同的偏好(場景是 context,價格,品牌是不同的資訊),天貓使用者對品牌看重,親淘使用者 focus 價格,可以通過 attention-layer 學習到不同 context 下,使用者的 Attention 在哪裡。在 ctr 預估中,Deep Interest Network for Click-Through Rate Prediction 出發點類似。在推薦場景中,文章 Feature Aware Multi-Head Attention 在手淘猜你喜歡排序模型中的應用 。這些都是attention 在業務場景落地的參考。

最後,國際搜尋和推薦算法部招賢納士啦!如果對資訊檢索、NLP、圖像處理、機器學習方向有興趣,想要加入我們,履歷申請位址:[email protected]

1.Bahdanau D, Cho K, Bengio Y. Neural machine translation by jointly learning to align and translate[J]. arXiv preprint arXiv:1409.0473, 2014.

2.Xu K, Ba J, Kiros R, et al. Show, attend and tell: Neural image caption generation with visual attention[J]. arXiv preprint arXiv:1502.03044, 2015.

3.Luong M T, Pham H, Manning C D. Effective approaches to attention-based neural machine translation[J]. arXiv preprint arXiv:1508.04025, 2015.

4.Vaswani A, Shazeer N, Parmar N, et al. Attention is all you need[C]//Advances in neural information processing systems. 2017: 5998-6008.

5.Dai Z, Yang Z, Yang Y, et al. Transformer-xl: Attentive language models beyond a fixed-length context[J]. arXiv preprint arXiv:1901.02860, 2019.

6.Yang Z, Yang D, Dyer C, et al. Hierarchical attention networks for document classification[C]//Proceedings of the 2016 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies. 2016: 1480-1489.

7.Cui Y, Chen Z, Wei S, et al. Attention-over-attention neural networks for reading comprehension[J]. arXiv preprint arXiv:1607.04423, 2016.

8.Gehring J, Auli M, Grangier D, et al. Convolutional sequence to sequence learning[C]//Proceedings of the 34th International Conference on Machine Learning-Volume 70. JMLR. org, 2017: 1243-1252.

9.

https://github.com/kimiyoung/transformer-xl

10.

https://lilianweng.github.io/lil-log/2018/06/24/attention-attention.html

11.

https://jalammar.github.io/illustrated-transformer/

12.

https://www.jianshu.com/p/b1030350aadb

13.

https://arxiv.org/abs/1706.06978

繼續閱讀