paper:Hierarchical Attention Networks for Document Classification

又是一篇很久之前用到的模型,今天回來整理,發現分類的模型都好簡單啊,然後看到模型基於GRU,總覺得有點不想看,因爲帶時間序列的訓練起來太慢了,最進沒怎麼關注分類的新模型,不過我覺得CNN和transformer結構(self attention)的搭配應該是分類問題的趨勢,不過這篇文章後面的attention效果可視化還是不錯的~

該模型就是基於分類問題提出來的,所以背景什麼的也就不說了,很簡單,直接切入模型正題。

博主標記版論文地址

1.模型概述

對於一個document含有這樣的層次結構,document由sentences組成,sentence由words組成。words和sentences都是高度上下文依賴的,同一個詞或sentence在不同的上下文中,其表現的重要性會有差別。因此,這篇論文中使用了兩個attention機制,來表示結合了上下文信息的詞或句子的重要程度。(這裏結合的上下文的詞或句子,就是經過RNN處理後的隱藏狀態)。

層級“注意力”網絡的網絡結構如圖1所示,網絡可以被看作爲兩部分,第一部分爲詞“注意”部分,另一部分爲句“注意”部分。整個網絡通過將一個句子分割爲幾部分(例如可以用“,”講一句話分爲幾個小句子),對於每部分,都使用雙向RNN結合“注意力”機制將小句子映射爲一個向量,然後對於映射得到的一組序列向量,我們再通過一層雙向RNN結合“注意力”機制實現對文本的分類。
在這裏插入圖片描述

2.模型詳情

論文裏面竟然用了不小的篇幅在說gru,瞬間就覺得有灌水的嫌疑,不過這也不像cmu的作風啊,可能早期文章還是比較偏底層,比較簡單吧~
這裏關於gru在我早期的文章裏面有提及過,這裏就不說了

2.1.Word Encoder

xit=Wewit,t[1,T]x_{it}=W_ew_{it}, t\in [1, T]
hit=GRU(xit),t[1,T] \overrightarrow h_{it}=\overrightarrow {GRU}(x_{it}),t\in[1,T]
hit=GRU(xit),t[T,1] \overleftarrow h_{it}=\overleftarrow {GRU}(x_{it}),t\in [T,1]
hit=[hit,hit]h_{it} = [\overrightarrow h_{it},\overleftarrow h_{it}]

其中:

  • ithi^{th} sentence in the document, and ttht^{th} means the tth word in the sentence.
  • 這裏的xit=Wewit,t[1,T]x_{it}=W_ew_{it}, t\in [1, T]應該表達的就是word lookup的過程,paper中說的不清楚,但是在所有我看到的實現裏面都是直接lookup

2.2.Word Attention layer

Not all words contribute equally to the representation of the sentence meaning. Hence, we introduce attention mechanism to extract such words that are important to the meaning of the sentence and aggregate the representation of those informative words to form a sentence vector.

Attention機制說到底就是給予sentence中每個結合了上下文信息的詞一個權重。關鍵在於這個權重怎麼確定?
uit=tanh(Wwhit+bw)u_{it}=tanh(W_wh_{it}+b_w)
αit=exp(uitTuw)tTexp(uitTuw)\alpha_{it}=\dfrac{exp(u_{it}^Tu_w)}{\sum_t^Texp(u_{it}^Tu_w)}
si=tTαithits_i=\sum_t^T\alpha_{it}h_{it}
這裏首先是將 hith_{it} 通過**一個全連接層得到 hidden representation uitu_{it},然後計算 uitu_{it}uwu_w 的相似性。**並通過softmax歸一化得到每個詞與 uwu_w 相似的概率。越相似的話,這個詞所佔比重越大,對整個sentence的向量表示影響越大。

這裏我覺得這裏的attention就是“self attention”,只不過沒有transformer正式提出來,也沒有那個複雜,沒有那個正式,不過總還是有self attention的影子。

2.3.Sentence Encoder

hi=GRU(si),t[1,L]\overrightarrow h_{i}=\overrightarrow {GRU}(s_{i}),t\in[1,L]
hi=GRU(si),t[L,1] \overleftarrow h_{i}=\overleftarrow {GRU}(s_{i}),t\in [L,1]
Hi=[hi,hi]H_i=[\overrightarrow h_{i}, \overleftarrow h_{i}]
hih_i summarizes the neighbor sentences around sentence i but still focus on sentence i.

2.4.Sentence Attention

ui=tanh(WsHi+bs)u_i=tanh(W_sH_i+b_s)
αi=exp(uiTus)iLexp(uiTus)\alpha_i=\dfrac{exp(u_i^Tu_s)}{\sum_i^Lexp(u_i^Tu_s)}
v=iLαihiv = \sum_i^L\alpha_ih_i
同樣的 usu_s 表示: a sentence level context vector usu_s

2.5.Document Classification

The document vector v is a high level representation of the document and can be used as features for document classification:
p=softmax(Wcv+bc)p=softmax(W_cv+b_c)

2.6.延伸閱讀

在查相關資料的時候發現了這個,以後再研究吧~
Multilingual Hierarchical Attention Networks for Document Classification

2.7.源碼

一份比較簡潔的代碼:tf-hierarchical-rnn

參考文獻

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章