Sunday, November 07, 2021

ConvS2S(三):Illustrated

 ConvS2S(三):Illustrated

2021/08/18

-----



https://pixabay.com/zh/photos/clouds-cloudy-stormy-weather-5303057/

-----


-----

Outline

1. ConvS2S
2. I/O Embedding
3. Padding and Mask
4. GLU
5. Encoder
6. Decoder
7. Multi-Hop Attention
8. Details

-----



-----

Figure 1. Illustration of batching during training. The English source sentence is encoded (top) and we compute all attention values for the four German target words (center) simultaneously. Our attentions are just dot products between decoder context representations (bottom left) and encoder representations. We add the conditional inputs computed by the attention (center right) to the decoder states which then predict the target words (bottom right). The sigmoid and multiplicative boxes illustrate Gated Linear Units.

圖 1. 訓練期間批處理的圖示。 英語源句子被編碼(頂部),我們同時計算四個德語目標詞(中心)的所有注意力值。 我們的注意力只是解碼器上下文表示(左下)和編碼器表示之間的點積。 我們將注意力(右中)計算的條件輸入添加到解碼器狀態,然後預測目標詞(右下)。 sigmoid 和乘法框說明了門控線性單元。

# ConvS2S。

說明:

Convolutional Sequence to Sequence,簡稱 ConvS2S。顧名思義,最大的改進在於將 LSTM 運算單元改為一維卷積因而可以利用 GPU 的平行運算特性。其實尚有許多小技巧。

--


# ConvS2S

說明:

Encoder 有 u 層,Decoder 有 L 層。參考論文公式二以及 3.2 最後一列。稍後的投影片 ConvS2S 2/3 與 3/3 有上述資訊。

--

說明:

one hot encoding 壓縮成 768 維。

768 轉成 512 或 1024 或 2048。

https://zhuanlan.zhihu.com/p/27234078

https://www.telesens.co/2019/04/21/understanding-incremental-decoding-in-fairseq/

--

What is the positional encoding in the transformer model?

「We also equip our model with a sense of order by embedding the absolute position of input elements p = (p1, . . . , pm) where pj belongs to Rf .」

https://datascience.stackexchange.com/questions/51065/what-is-the-positional-encoding-in-the-transformer-model

-----


Modified from # ConvS2S

說明:

「這邊有個細節:為了保證 Conv 的輸出長度和輸入是相同的,會在每層的 input 做 padding,Encoder 的部分如下圖:」

Encoder。<p>, ... ,</s>, <p>。

Padding 的部分,由於使用一維的 Conv3,Encoder 端的 They agree,除了先加上句末的標記 </s>,前後要各補一個 <p>。


「但是在 Decoder, 為了 不讓某個時間點 看到 該時間點以後的資訊,會有稍微不同的結構: 假設 kernel size 為  k ,則在 input 前後 pad (k-1) 的單位,最後把後端的 k 個單位刪除。如下圖 (k=3):」

Decoder。<p>, <p>, <s>, ... , </s>, <p>, <p>。

Decoder 端的德文 Sie stimmen zu,除了句首的標記 <s>,也要補上兩個 <p>。Attention 的矩陣可以看到 <s> Sie stimmen tu 與 They agree </s> 的權重對應。

https://ycts.github.io/weeklypapers/convSeq2seq/

-----


-----

# GLU。

說明:

句子的向量,先分別經過兩個一維卷積的運算。上半部經過 sigmoid 的運算,讓它具有 LSTM 的門的作用。然後再與另一半點乘。

-----


# ConvS2S

說明:

ConvS2S 的 GLU 會加上殘差連結。

https://zhuanlan.zhihu.com/p/27464080

-----


Modified from # ConvS2S

-----

Encoder

說明:

Encoder 最後一層(共 u 層)的輸出,作為 Decoder 每一層(共 L 層)(共八層) attention 的參考來源。

https://reniew.github.io/44/

-----

Decoder

說明:

會用到 Attention 的權重來決定 Encoder 端每個字的份量。

https://reniew.github.io/44/

Encoder 一次讀完,Decoder 逐字輸出。每次吐出一個字。

https://ycts.github.io/weeklypapers/convSeq2seq/

-----


Modified from # ConvS2S

說明:

參考投影片 ConvS2S 2/3 的說明。z 是 Key,e 是 Value。QK 先算出權重。權重再根據 K + V 算出 Context。Context 還要再加上 h 然後成為下一層 decoder 的輸入或是最後的輸出。

https://deeplearning.hatenablog.com/entry/convs2s

--


# ConvS2S

--

# ConvS2S

說明:

八層 Attention。

一、三、六:線性。

二、八:整個來源句的資訊。

四:名詞。

五、七:建立。配合德文(文法結構)的翻譯。

-----

byte-pair encoding (BPE)

「BPE 最早由 Philip Gage 提出,用來做數據壓縮上。它的原理是將常見連續的兩個符號以另一個符號表達。例如 ababab 中 a 後面很常接著 b 我們就用 c 表達 ab,並得到一個新的序列 ccc,c = ab 。」

https://theblackcat102.github.io/BPE/

--

「Layer 1, 3 and 6 exhibit a linear alignment. The first layer shows the clearest alignment, although it is slightly off and frequently attends to the corresponding source word of the previously generated target word. 」

第 1、3 和 6 層呈現線性排列。 第一層顯示了最清晰的對齊方式,儘管它略微偏離並且經常關注先前生成的目標詞的相應源詞。

「Layer 2 and 8 lack a clear structure and are presumably collecting information about the whole source sentence. 」

第 2 層和第 8 層缺乏清晰的結構,可能正在收集有關整個源語句的信息。

「The fourth layer shows high alignment scores on nouns such as “festival”, “way” and “work” for both the generated target nouns as well as their preceding words. Note that in German, those preceding words depend on gender and object relationship of the respective noun. 」

第四層顯示了對生成的目標名詞及其前面的詞的“節日”、“方式”和“工作”等名詞的高對齊分數。 請注意,在德語中,前面的詞取決於相應名詞的性別和賓語關係。

「Finally, the attention scores in layer 5 and 7 focus on “built”, which is reordered in the German translation and is moved from the beginning to the very end of the sentence. One interpretation for this is that as generation progresses, the model repeatedly tries to perform the re-ordering. “aufgebaut” can be generated after a noun or pronoun only, which is reflected in the higher scores at positions 2, 5, 8, 11 and 13.」

最後,第 5 層和第 7 層的注意力分數集中在“built”上,它在德語翻譯中重新排序,並從句子的開頭移到結尾。 對此的一種解釋是,隨著生成的進行,模型會反复嘗試執行重新排序。 “aufgebaut”可以僅在名詞或代詞之後生成,這反映在位置 2、5、8、11 和 13 的較高分數中。

-----


Modified from # ConvS2S。

-----


Modified from # ConvS2S。

-----

Encoder 公式

說明:

d:embedding 的維度。

p:position vector。

k:卷積核大小。

列一

加上位置向量。

列二 

2d 表示列三的 A、B,兩倍大小。

列三

GLU。

列四

zj 是 K。ej 是 V。

-----

Decoder 公式


說明:

l:第 l 層。

i:此處為 4。

j:此處為 3。

u:encoder 最後一層。

列一

GLU 的運算。

列二

殘差連接。

列三

列四括號內的 i - 1。可參考下方超連結的動態圖。

列四

先透過線性層的轉換,再加上上一個時間點的值。

-----

gi

「gi 是上一個時間點 i−1 的 target embedding。」

「下圖用動畫更具體的示意 attention 的流程。」

Bahdanau 使用 decoder t-1 的時間(以及 encoder 的輸出)作為 attention 的計算來源。

Loung 使用 decoder t 的時間(以及 encoder 的輸出)作為 attention 的計算來源。

ConvS2S 兼具兩者之長,使用 t-1 的時間(gi)與 t 的時間,作為 attention 的計算來源。

https://ycts.github.io/weeklypapers/convSeq2seq/

--

列五

計算權重。

列六

計算上下文向量。

-----

Output 公式


說明:

列一

Decoder 隱藏層的向量加上上下文向量,作為下一層的輸入,或者最後的輸出。

列二

Decoder 最後一層的輸出向量 hi 透過線性層再透過 softmax 可以預測下一輸出字的機率。

https://norman3.github.io/papers/docs/fairseq.html

-----

CBOW 周圍的字預測中間的單字


# Word2vec 3。

說明:

透過詞向量矩陣可得到詞向量。

-----

References

# ConvS2S。被引用 1772 次。

Gehring, Jonas, et al. "Convolutional sequence to sequence learning." arXiv preprint arXiv:1705.03122 (2017).

https://arxiv.org/pdf/1705.03122.pdf


# GLU

Dauphin, Yann N., et al. "Language modeling with gated convolutional networks." Proceedings of the 34th International Conference on Machine Learning-Volume 70. JMLR. org, 2017.

https://arxiv.org/pdf/1612.08083.pdf


# Word2vec 3。被引用 645 次。

Rong, Xin. "word2vec parameter learning explained." arXiv preprint arXiv:1411.2738 (2014).

https://arxiv.org/pdf/1411.2738.pdf

-----

The Star Also Rises: NLP(四):ConvS2S

https://hemingwang.blogspot.com/2019/04/convs2s.html

-----

No comments: