Showing posts with label _AI:2020. Show all posts
Showing posts with label _AI:2020. Show all posts

Monday, December 13, 2021

NLP(六):BERT Overview

NLP(六):BERT Overview

2019/01/17

說明:

Natural Language Processing (NLP) 的 Pre-trained Models 從 2013 的 Word2vec 到 2018 的 BERT 有極大的進步 [1]-[11]。簡單說,Word2vec, GloVe, fastText 是淺層模型,ELMo, AWD-LSTM, ULMFiT 是深層的 LSTM 模型。OpenAI GPT 是深層的 Transformer 模型。BERT 則是基於雙向的 ELMo 模型與深層的 Transformer 模型之優點合併成為雙向的 Transformer 模型。

建議讀者可先瞭解 Transformer 的原理 [5],再分別以一篇文章 [6] 或兩篇文章 [7], [8] 或三篇文章 [9]-[11] 漸次理解 Pre-trained Models 的演進過程。

-----

Outline:

一、Word2vec, GloVe, fastText
二、ELMo, AWD-LSTM, ULMFiT
三、OpenAI GPT
四、BERT
五、from Word2vec to BERT

-----


Fig. 1. BERT [1]。

-----

一、 Word2vec、GloVe、fastText

◎ 三個淺層模型。

「早在 2013 年 Google 提出了 Word2Vec 之後,NLP 領域的深度學習就開始使用預訓練模型,而後斯坦福大學提出的 GloVe 和 Facebook 提出的 fastText 則是進一步發展。然而在今年之前,這方面的嘗試大都局限於使用淺層網絡,在詞的層面上進行建模。針對具體的應用場景,要達到較好的效果依然需要非常大量的標註語料。預訓練深層模型以及之上的遷移學習在圖像領域的成功,引領著 NLP 領域專家們也在思考如何實現同樣的範式。多年的努力與探索,終於在今年迎來了豐收。 」[4]

二、ELMo、AWD-LSTM、ULMFiT

◎ ELMo 是雙向的 LSTM 模型。

「首先是年初發表於 NAACL-HIT 2018 的 ELMo 預訓練模型,用正向和反向兩個 LSTM 語言模型(BiLM)在通用語料上進行訓練,將得到的預訓練好的模型(即 ELMo)用於深度網絡的輸入上,在多個任務上能夠明顯改善已有的模型的效果。 」[4]

◎ ULMFiT 是三層的 AWD-LSTM 模型。

「此後,FastAI 基於三層 AWD-LSTM 構建出的語言模型,使用大規模通用語料預訓練出 ULMFiT 模型。將該模型應用於特定領域,只要使用非常少量的標註數據就可以達到普通模型需要大量標註數據的效果。這個模型的成功,使得大家看到了遷移學習在 NLP 領域上的曙光。」 [4]

三、OpenAI GPT

◎ OpenAI GPT 是單向的 Transformer(捨棄 LSTM,只保留 FNN)。

「緊接著,OpenAI 使用 Transformer 和無監督結合的方法在大規模通用語料上進行訓練,得到預訓練好的 GPT 模型。針對特定的場景,在預訓練好的 GPT 模型基礎上,用小得多的數據集進行有監督學習,獲得了當時最好的成績。 」[4]

四、BERT

◎ BERT 綜合雙向的 ELMo 與單向的 OpenAI GPT 之優點,推出雙向 Transformer 的版本。

「2018 年10月,Google 在 GPT 的基礎上進一步改進,提出了基於 Transofrmer 的 BERT 模型。在訓練 BERT 的過程中,Google 構造出 MLM(Masked Language Model)語言模型,這是一個“真”雙向語言模型。並在通用的大規模語料 BooksCorpus(800M words)加上英文維基百科(2,500M words)上進行無監督訓練,得到預訓練模型 BERT。論文中,使用預訓練的模型 BERT 在 11 個任務上進行有監督的微調(遷移學習),其效果全部達到當前最優。特別地,在斯坦福問答評測數據集(SQuAD 1.1)上超越了人類專家的評測結果。」[4]

五、from Word2vec to BERT

-----


Fig. 2. From fastText to ELMo [2]。

-----


Fig. 3. CBOW and skip-gram [3]。

-----

Task #1: Masked LM

◎ CBOW and Masked-LM

「CBOW 方法,它的核心思想是:在做語言模型任務的時候,我把要預測的單詞摳掉,然後根據它的上文 Context-before 和下文 Context-after 去預測單詞。其實 BERT 怎麼做的? BERT就是這麼做的。從這裡可以看到方法間的繼承關係。當然 BERT 作者沒提 Word2vec 及 CBOW 方法,這是我的判斷,BERT 作者說是受到完形填空任務的啟發,這也很可能,但是我覺得他們要是沒想到過 CBOW 估計是不太可能的。」[6]。

-----

Task #2: Next Sentence Prediction

◎ Skip-gram and Skip-Thought

「除了用上 Masked-LM 的方法使得雙向 Transformer 下的語言模型成為現實,BERT 還利用和借鑒了 Skip-thoughts 方法中的句子預測問題,來學習句子級別的語義關係,具體做法則是將兩個句子組合成一個序列,當然組合方式會按照下面將要介紹的方式,然後讓模型預測這兩個句子是否是先後近鄰的兩個句子,也就是會把 "Next Sentence Prediction" 問題建模成為一個二分類問題。訓練的時候,數據中有 50% 的情況這兩個句子是先後關係,而另外 50% 的情況下,這兩個句子是隨機從語料中湊到一起的,也就是不具備先後關係,以此來構造訓練數據。句子級別的預測思路和之前介紹的 Skip-thoughts 基本一致,當然更本質的思想來源還是來自於 Word2vec 中的 skip-gram 模型。」[10]。

◎ Skip-Thought and Quick-Thought

「2018年的時候,在 Skip-thoughts 的基礎上,Google Brain 的 Logeswaran 等人將這一思想做了進一步改進,他們認為 Skip-thoughts 的 Decoder 效率太低,且無法在大規模語料上很好的訓練(這是 RNN 結構的通病)。所以他們把 Skip-thoughts 的生成任務改進成為了一個分類任務,具體說來就是把同一個上下文窗口中的句子對標記為正例,把不是出現在同一個上下文窗口中的句子對標記為負例,並將這些句子對輸入模型,讓模型判斷這些句子對是否是同一個上下文窗口中,很明顯,這是一個分類任務。可以說,僅僅幾個月之後的 BERT 正是利用的這種思路。而這些方法都和 Skip-thoughts 一脈相承。」[11]。

-----

符號說明:

# basic
// advanced

-----

Paper

# Word2vec
Le, Quoc, and Tomas Mikolov. "Distributed representations of sentences and documents." International Conference on Machine Learning. 2014.
http://proceedings.mlr.press/v32/le14.pdf

# GloVe
Pennington, Jeffrey, Richard Socher, and Christopher Manning. "Glove: Global vectors for word representation." Proceedings of the 2014 conference on empirical methods in natural language processing (EMNLP). 2014.
https://nlp.stanford.edu/pubs/glove.pdf

# fastText
Bojanowski, Piotr, et al. "Enriching word vectors with subword information." Transactions of the Association for Computational Linguistics 5 (2017): 135-146.
https://www.mitpressjournals.org/doi/pdfplus/10.1162/tacl_a_00051

# ELMo
Peters, Matthew E., et al. "Deep contextualized word representations." arXiv preprint arXiv:1802.05365 (2018).
https://arxiv.org/pdf/1802.05365.pdf

# AWD-LSTM
Merity, Stephen, Nitish Shirish Keskar, and Richard Socher. "Regularizing and optimizing LSTM language models." arXiv preprint arXiv:1708.02182 (2017).
https://arxiv.org/pdf/1708.02182.pdf

# ULMFiT
Howard, Jeremy, and Sebastian Ruder. "Universal language model fine-tuning for text classification." Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). Vol. 1. 2018.
http://www.aclweb.org/anthology/P18-1031

# OpenAI GPT
Radford, Alec, et al. "Improving language understanding by generative pre-training." URL https://s3-us-west-2. amazonaws. com/openai-assets/research-covers/languageunsupervised/language understanding paper. pdf (2018).
https://www.cs.ubc.ca/~amuham01/LING530/papers/radford2018improving.pdf

# BERT
Devlin, Jacob, et al. "Bert: Pre-training of deep bidirectional transformers for language understanding." arXiv preprint arXiv:1810.04805 (2018).
https://arxiv.org/pdf/1810.04805.pdf

-----

References

# BERT
[1] Google AI Blog  Open Sourcing BERT  State-of-the-Art Pre-training for Natural Language Processing
https://ai.googleblog.com/2018/11/open-sourcing-bert-state-of-art-pre.html

[2] 想研究 NLP,不了解詞嵌入與句嵌入怎麼行? _ 香港矽谷
https://www.hksilicon.com/articles/1646194

[3] A Review of the Neural History of Natural Language Processing - AYLIEN
http://blog.aylien.com/a-review-of-the-recent-history-of-natural-language-processing/

# Word2vec GloVe fastText 淺層 ELMo ULMFiT Open AI GPT BERT 深層
[4] 帮AI摆脱“智障”之名,NLP这条路还有多远  - AI科技大本营 - CSDN博客
https://blog.csdn.net/dQCFKyQDXYm3F8rB0/article/details/83754091

# Transformer
[5] Seq2seq pay Attention to Self Attention  Part 2(中文版)
https://medium.com/@bgg/seq2seq-pay-attention-to-self-attention-part-2-%E4%B8%AD%E6%96%87%E7%89%88-ef2ddf8597a4

-----

一文道盡預訓練模型
 
[6] 从Word Embedding到Bert模型—自然语言处理中的预训练技术发展史 - 知乎
https://zhuanlan.zhihu.com/p/49271699
    
-----

兩篇文章分別解釋淺層模型與深層模型

# word2vec、GloVe、fastText
[7] word2vec、glove和 fasttext 的比较 - sun_brother的博客 - CSDN博客
https://blog.csdn.net/sun_brother/article/details/80327070

# ELMo、ULMFiT、OpenAI GPT
[8]【NLP】语言模型和迁移学习 - 知乎
https://zhuanlan.zhihu.com/p/42618178

-----

三篇系列文章詳細解釋預訓練模型。

[9] NLP的巨人肩膀(上) - 简书
https://www.jianshu.com/p/fa95963c9abd

[10] NLP的巨人肩膀(中) - 简书
https://www.jianshu.com/p/81dddec296fa

[11] NLP的巨人肩膀(下) - 简书
https://www.jianshu.com/p/922b2c12705b

Sunday, December 12, 2021

NLP(五):Transformer

NLP(五):Transformer

2019/01/17


2020/12/15

前言: 

Transformer,以及由雙向 Transformer Encoder 構成的預訓練模型 BERT,可說是現代 NLP 的起點。Transformer 同時也用到 兩篇 CV 的論文,NIN 的 Conv1 來升降維,以及 ResNet 的 identity mapping(由早期 NLP 的主要元件 LSTM 的直通架構得到靈感)。而 Transformer 的 self attention 也被 CV 拿回去使用。可以說,當代深度學習,CV 與 NLP 是不斷交互影響的。

在十篇經典的深度學習論文(LeNet、NIN、ResNet、FCN、YOLO、LSTM、Seq2seq、Attention、ConvS2S、Transformer)裡:

LeNet 主要體現了一個完整的 CNN,NIN 提供了 Conv1 作為深度學習的必備元件,ResNet 提供了 identity mapping 作為深度學習的必備元件。FCN 與 YOLO 則為 CNN 在 CV 的主要應用。可以說 LeNet、NIN、ResNet 這一棵 Image Classification 的大樹,長出了 Semantic Segmentation 與 Object Detection 兩條主幹。

另一方面,從 LSTM 這個基本元件,套用 Encoder-Decoder 的架構,產生了 Seq2seq。由單一 Context 向量細部化,透過多 Context 向量,於是有了 Attention。由 RNN 轉向 一維 CNN 的 ConvS2S,除了可以使用 GPU 平行運算的功能,最主要的成果還有導入 QKV,讓詞向量的分解更細緻。ConvS2S 的 QKV 作用於 Encoder 與 Decoder 之間,Transformer 的 QKV 則是分別先在 Encoder 與 Decoder 先進行。然後從 Encoder 的最後一層的 KV 與 Decoder 每一層的 Q 連起來。 使得 QKV 又更細緻了。Seq2seq、Attention、ConvS2S、Transformer,我把它們命名為 Source Target Output Relational Model,簡稱 STORM。

前年年底,我自學了三個月的 NLP,知道 NLP 五篇經典論文,由於在網路上遇到博士班的口試委員,因為被稱讚文章寫的不錯,於是自告奮勇到他們實驗室報告,NLP 五篇之外,也選了 CV 五篇。Transformer 就是為了報告,強迫自己讀懂得。後來報告時間不夠,還找了地方跟 PyTorch Taichung 的社長一個人講完。ResNet 被問倒了。後來年底實驗了三個月共十次的收費論文研討,共三十小時,除了 ResNet v1 與 v2,另外讀了 Dropout、Essemble、與 Visualization 的版本,總算對 ResNet 有了基本的掌握。

2019 年底的收費論文研討大約有十位參加者,我讓所有參加者免費聽 2020 的全方位 AI 課程,並且邀請其中五位,每人報告兩次,含我自己十次,一共二十次,共六十小時。新的參加者大約也是十人。定價由三萬調整到五萬,實際收費則是由一萬五千左右調整為四萬左右。收入遠不到需要報稅的水準,但是可以有很多時間讀論文。

從社團論文研討到收費論文研討再到全方位 AI 課程,其實核心都是希望深度學習者可以自己報告論文,而不是聽別人報告論文,這個觀念並不是很能被理解或接受。我自己,則是透過不斷地寫論文導讀,與進行論文報告,而持續進步。

Transformer 上次報告,只理解表面與大意。經過一年的持續學習,除了報告 Word2vec 以及 Short Attention 的 QKV 之外,ConvS2S 也更深入理解了,對於理解 Transformer 的架構幫助很大。細節上,則搞清楚 Positional Embedding。

這篇前言寫的有點長,算是為

丁酉:從 LeNet 到 LeNet 實作團到 DRL 實作團到 PyTorch Taiwan。
戊戌:從 PyTorch Taipei 開始,主要報告 CV。年底開始自學 NLP。
己亥:策劃十篇經典論文,給 PyTorch New Taipei 作為報告主題。年底進行收費論文研討。
庚子:全方位 AI 課程。

這四年的深度學習,進行一個簡單的回顧。

-----


Fig. Transformer(圖片來源)。

圖一、變壓器。

-----

Summary: 

Transformer 可以跟 ConvS2S 一起讀,比較其中的異同。

相同的部分,都是 Encoder-Decoder 的架構。一開始都是 Word Embedding 與 Positional Embedding。都有用到 QKV 的觀念。Encoder 的最後一層,作為 Decoder 每一層的輸入。Decoder 每一層的輸出,作為 Decoder 下一層的輸入。Decoder 的最後一層,每次輸出一個字。

相異的部分,Transformer 的 Encoder,每一層會有一個 Multi-Head 的 self attention。另外 Decoder 的每一層,除了與 Encoder 連在一起的 Multi-Head attention,還先做了 Masked Multi-Head self attention。

-----


圖二、Outline。

-----

Outline

1.1. Transformer
1.2. I/O Embedding
1.3. Positional Encoding

2.1. Attention
2.2. Multi-Head
2.3. Multi-Layer
2.4. Skip Connection
2.5. Layer Normalization
2.6. FNN

3.1. Masked
3.2. Linear and Softmax
3.3. Prediction

4.1. QKV
4.2. Softmax and Root of dk

-----

1. Transformer

2. I/O Embedding
Word2Vec。

3. Positional Encoding
公式跟跟訓練結果差不多,用公式。

4. Attention
Query、Key、Value。

5. Multi-Head
多個卷積核。

6. Skip Connection
ResNet(Dropout)。

7. Layer Normalization
BN 每筆資料每層的某個位置正規化
LN 某筆資料某層的所有位置正規化
正規化 - 減 mean 再除以 STD

8. FFN
調整輸出跟輸入的維度

9. Masked
保持 Auto Regression,output 不考慮 i 之後字的因素。

10. Linear and Softmax
打分數

下面三個是 attention 公式:

11. Softmax
Logistic Regression 的推廣
資料為 0 ~ 1 之間,總和為 1 的離散機率分布

12. QKV
QK 對齊(內積、餘弦相似)。(如果 FNN 的話,是加法)。

13. Root of dk
dk:dimension of K,避免向量的分量值透過 Logistic Regression 後都是 1 或者都是 0,造成無 attention。

-----

1.1. Transformer

-----


Fig. 1. Transformer [1]。

https://mchromiak.github.io/articles/2017/Sep/12/Transformer-Attention-is-all-you-need/#.X87jeOgzaUl

圖三、Transformer。

左邊是 3 Stages 的 Encoder,右邊是 5 Stages 的 Decoder。右邊比左邊多的部分是 Stage 3 的連接層與 Stage 5 的輸出層。細節將在稍後的章節說明。

-----

1.2. I/O Embedding

----- 

Weight Matrix of Word2vec


https://mc.ai/deep-nlp-word-vectors-with-word2vec/

圖四、1-hot Encoding to Vector。

假定字彙數量是 10k,維度 10,000 的 one hot encoding 可以先經由 Word Embedding 壓縮成維度 300 的向量。

-----

1.3. Positional Encoding

-----


 圖五、Positional Encoding。

公式 [1]。

pos:position。一個 word 在 sentence 中的位置。

i:dimension。Positional Embedding 向量的 index,最大值為 dmodel。

dmodel:Word Embedding 的維度。768、512,等等。

k:新增的位置向量的 offset。新增向量可由之前向量的線性組合構成,係數為 sin(k) 與 cos(k),參考圖六。

-----



圖六。

dpos 相當於圖五的 dmodel。

-----



圖七。

十個位置向量的編碼。交錯的版本。橫軸代表位置向量的分量,縱軸代表字在句中的位置。顏色代表數值。

-----

圖八。

廿個位置向量的編碼。未交錯的版本。橫軸代表位置向量的分量,縱軸代表字在句中的位置。顏色代表數值。

-----



圖九。

相加。

-----

2.1. Attention

-----



圖十、QKV。

第一層代表第一個字。
第二層代表第二個字。

-----

2.2. Multi-Head

-----



圖十一、Multi Heads。

作者試出來多頭效果比較好,其作用類似 CNN 每層的多個特徵圖。

----- 



圖十二、QKV一。

QK 構成 Attention Table。

-----



圖十三、QKV二。

QKV一的實際運算。

----- 



圖十四。

Layer 5 的八個多頭其中一個。

----- 



圖十五。

Layer 5 的八個多頭其中兩個。

----- 



圖十六。

Layer 5 的八個多頭。

-----



圖十七、Multi Heads。

先經過 self attention 再通過 FFN,作用與 Conv1 相同。

-----

2.3. Multi-Layer

-----



圖十八、Multi Layer。

六層 Decoder,每層的輸入都是 Encoder 的最後一層。

-----



圖十九、Decoder 的運算單元。

可以有多層,此處的例子為 6 層。

KV 來自 Encoder 的最後一層。Q 來自 Decoder 的前一層。

-----

2.4. Skip Connection

-----



圖二十、Carry。

ResNet 的 identity mapping 的靈感來源。

-----

2.5. Layer Normalization

-----


// Papers With Code   Positional Normalization

https://www.groundai.com/project/positional-normalization/1

圖廿一、Normalization。

這張圖比 Group Normalization 論文裡面的圖清楚。

-----


// Weight Normalization and Layer Normalization Explained (Normalization in Deep Learning Part 2) _ Machine Learning Explained


圖廿二、BN vs LN。

一個 Batch 裡面有好幾個 Layer。

-----

2.6. FFN

-----


Fig. 4.2. Conv1。

https://blog.csdn.net/zhangjunhit/article/details/55101559

圖廿三、Feed forward Network(FFN)。

每個輸出層的一個點,都由不同特徵圖的同一個位置的點,轉換得來。係數靠訓練。

-----

3.1. Masked

-----



圖廿四、Masked。

未來的資訊,不可使用。

-----

1




2




3




4





-----

3.2. Linear and Softmax

-----



圖廿五、輸出層。

先透過線性層決定要輸出哪個字,由數值轉為機率。然後輸出那個字。

-----



圖廿六、Softmax。

將數值轉為離散機率分布。

-----

3.3. Prediction

-----


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

圖廿七、預測下一個字。

一次輸出一個字。

-----

4.1. QKV

-----



圖廿八、QKV。

-----

4.2. Softmax and Root of dk

-----



圖廿九、Root of dk。

dk:dimension of K。k 為 QKV(三者的維度相同)的 K 的維度。除以一個值(往原點靠近),避免向量的分量值透過 Logistic Regression 後都是 1(正值太大) 或者都是 0(負值太小),造成無 attention。

-----


https://medium.com/@joealato/attention-in-nlp-734c6fa9d983

圖三十、PKV。

從 Attention 到 Key-Value 到 QKV。

-----

「Google 在論文中說到他們比較過直接訓練出來的位置向量和上述公式計算出來的位置向量,效果是接近的。因此顯然我們更樂意使用公式構造的 Position Embedding 了。」


-----

「缺點在原文中沒有提到,是後來在 Universal Transformers中指出的,在這裡加一下吧,主要是兩點: 實踐上:有些 rnn 輕易可以解決的問題 transformer 沒做到,比如復制 string,尤其是碰到比訓練時的 sequence 更長的時理論上:transformers 非 computationally universal(圖靈完備),(我認為)因為無法實現 “while” 循環 4. 總結 Transformer 是第一個用純 attention 搭建的模型,不僅計算速度更快,在翻譯任務上也獲得了更好的結果。Google 現在的翻譯應該是在此基礎上做的,但是請教了一兩個朋友,得到的答案是主要看數據量,數據量大可能用 transformer 好一些,小的話還是繼續用 rnn-based model」。


-----

References

◎ 論文

[1] Transformer
Vaswani, Ashish, et al. "Attention is all you need." Advances in Neural Information Processing Systems. 2017.
https://papers.nips.cc/paper/7181-attention-is-all-you-need.pdf

-----

◎ 英文參考資料

# 綜述
Attention  Attention!
https://lilianweng.github.io/lil-log/2018/06/24/attention-attention.html

The Illustrated Transformer – Jay Alammar – Visualizing machine learning one concept at a time
https://jalammar.github.io/illustrated-transformer/

The Transformer – Attention is all you need. - Michał Chromiak's blog
https://mchromiak.github.io/articles/2017/Sep/12/Transformer-Attention-is-all-you-need/#.XEbkXPLxh1A 

Paper Dissected  'Attention is All You Need' Explained _ Machine Learning Explained
http://mlexplained.com/2017/12/29/attention-is-all-you-need-explained/

Transformer Architecture  Attention Is All You Need
https://medium.com/@adityathiruvengadam/transformer-architecture-attention-is-all-you-need-aeccd9f50d09

What is a Transformer  – Inside Machine learning – Medium
https://medium.com/inside-machine-learning/what-is-a-transformer-d07dd1fbec04 

Transformers – Towards Data Science
https://towardsdatascience.com/transformers-141e32e69591

9.3. Transformer — Dive into Deep Learning 0.7 documentation
https://d2l.ai/chapter_attention-mechanism/transformer.html

The Annotated Transformer
http://nlp.seas.harvard.edu/2018/04/03/attention.html

# skip connection
Understanding LSTM Networks -- colah's blog
http://colah.github.io/posts/2015-08-Understanding-LSTMs/ 

# layer normalization
The Star Also Rises  Normalization
https://hemingwang.blogspot.com/2019/10/normalization.html

# layer normalization
Weight Normalization and Layer Normalization Explained (Normalization in Deep Learning Part 2) _ Machine Learning Explained
https://mlexplained.com/2018/01/13/weight-normalization-and-layer-normalization-explained-normalization-in-deep-learning-part-2/

# softmax
Softmax - Hands-On Natural Language Processing with Python [Book]
https://www.oreilly.com/library/view/hands-on-natural-language/9781789139495/3550f443-190d-4931-8d57-29db02a3abff.xhtml
 
-----

◎ 日文參考資料

論文解説 Attention Is All You Need (Transformer) - ディープラーニングブログ
http://deeplearning.hatenablog.com/entry/transformer

# Masked
論文解説 Convolutional Sequence to Sequence Learning (ConvS2S) - ディープラーニングブログ
http://deeplearning.hatenablog.com/entry/convs2s 

-----

◎ 簡體中文參考資料

一文读懂「Attention is All You Need」_ 附代码实现 _ 机器之心
https://www.jiqizhixin.com/articles/2018-01-10-20

tensor-to-tensor[理论篇] - daiwk-github博客
https://daiwk.github.io/posts/platform-tensor-to-tensor.html#422-attention

【NLP】Transformer详解 - 知乎
https://zhuanlan.zhihu.com/p/44121378

详解Transformer (Attention Is All You Need) - 知乎
https://zhuanlan.zhihu.com/p/48508221 

《Attention is All You Need》浅读(简介+代码) - 科学空间|Scientific Spaces
https://kexue.fm/archives/4765

深度学习中的注意力模型(2017版) - 知乎
https://zhuanlan.zhihu.com/p/37601161

放弃幻想,全面拥抱Transformer:自然语言处理三大特征抽取器(CNN_RNN_TF)比较 - 知乎
https://zhuanlan.zhihu.com/p/54743941

# Conv1
CNN网络中的 1 x 1 卷积是什么? - AI小作坊 的博客 - CSDN博客
https://blog.csdn.net/zhangjunhit/article/details/55101559

-----

◎ 繁體中文參考資料

Attention Is All You Need:基於注意力機制的機器翻譯模型 – Youngmi huang – Medium
https://medium.com/@cyeninesky3/attention-is-all-you-need-%E5%9F%BA%E6%96%BC%E6%B3%A8%E6%84%8F%E5%8A%9B%E6%A9%9F%E5%88%B6%E7%9A%84%E6%A9%9F%E5%99%A8%E7%BF%BB%E8%AD%AF%E6%A8%A1%E5%9E%8B-dcc12d251449
 
Seq2seq pay Attention to Self Attention  Part 2(中文版)
https://medium.com/@bgg/seq2seq-pay-attention-to-self-attention-part-2-%E4%B8%AD%E6%96%87%E7%89%88-ef2ddf8597a4 

Easy Introduction to Transformer - Jexus Scripts
https://voidism.github.io/note/2019/02/05/Transformer_Intro/

# masked
自迴歸模型 - 維基百科,自由的百科全書
https://zh.m.wikipedia.org/zh-tw/%E8%87%AA%E8%BF%B4%E6%AD%B8%E6%A8%A1%E5%9E%8B

-----

◎ 代碼實作

How to code The Transformer in Pytorch – Towards Data Science
https://towardsdatascience.com/how-to-code-the-transformer-in-pytorch-24db27c8f9ec 

如何在CUDA中为Transformer编写一个PyTorch自定义层 _ 机器之心
https://www.jiqizhixin.com/articles/19032504

Sunday, September 05, 2021

NLP(二):Seq2seq

NLP(二):Seq2seq

2019/01/02

施工中...

-----

說明:


Seq2seq 的核心觀念是:整句讀完再翻譯。

-----


[2]。

-----

◎ 一、Seq2seq 的動機

動機在於若以矩陣的方式處理文字,則字數必須是固定的。以 RNN 形式,則句子的長度才有彈性。

「要知道,在以往的很多模型中,我們一般都說輸入特徵矩陣,每個樣本對應矩陣中的某一行,就是說,無論是第一個樣本還是最後一個樣本,他們都有一樣的特徵維度。但是對於翻譯這種例子,難道我們要讓每一句話都有一樣的字數嗎,那樣的話估計五言律詩和七言絕句又能大火一把了,哈哈。但是這不科學呀,所以就有了 seq2seq 這種結構。」[8]。

-----

◎ 二、Seq2seq 的架構

架構是編碼器解碼器。先將輸入句壓縮成單向量,再把單向量重組為想要的輸出。

「Sequence to Sequence 是由 Encoder 與 Decoder 兩個 RNN 構成,它的運作原理其實與人類的思維很相似,當我們看到一段話時,會先將這句話理解吸收,再根據我們理解的內容說出回覆,Sequence to Sequence 就是在模擬這個過程。」[9]。

-----

◎ 三、Seq2seq 的範例

翻譯、對話、摘要、創作,都可以用 Seq2seq 完成。

「輸入一句英文,輸出一句法文,就寫好了一個翻譯系統。 
輸入一個問題,輸出一句回覆,就架好一個聊天機器人。 
輸入一篇文章,輸出一份總結,就構成一個摘要系統。 
輸入幾個關鍵字,輸出一首短詩,就成就了一名詩人。」[9]。

-----

◎ 四、Seq2seq 的作法

端看訓練的資料集而定。

「而 Seq2seq 靈活的架構,也讓這個模型被廣泛地應用到各種不同的任務上:例如 Chatbot、Google Inbox 的 Auto-Reply。只要你有一個配對好的文本集(問與答、信件與回覆、圖片與描述),就可以把資料餵進模型裡訓練產生一個 Seq2seq 系統。」[10]。

-----

◎ 五、Seq2seq 的不足

多向量可以比單向量更精細地執行任務,所以 Seq2seq 的單向量模型之後,又有 Attention 的多向量模型被開發出來。

「現在我們具備 RNN/LSTM 的知識,可以發現 Seq2seq 中,Decoder 的公式和 RNN 根本就是同一個模子出來的,差別在於 Decoder 多了一個 C,這個 C 是指 context vector/thought vector。context vector 可以想成是一個含有所有輸入句訊息的向量,也就是 Encoder 當中,最後一個 hidden state。簡單來說,Encoder 將輸入句壓縮成固定長度的 context vector,context vector 即可完整表達輸入句,再透過 Decoder 將 context vector 內的訊息產生輸出句。」[11]。

「為什麼要用 attention model?The attention model 用來幫助解決機器翻譯在句子過長時效果不佳的問題。 這種新的架構替輸入句的每個文字都創造一個 context vector,而非僅僅替輸入句創造一個從最終的 hidden state得來的 context vector,舉例來說,如果一個輸入句有 N 個文字,就會產生 N 個 context vector,好處是,每個 context vector 能夠被更有效的解碼。 」[11]。

-----


論文圖一,Seq2seq 的架構 [1]。

-----



[1]

-----



[1]

-----



[1]

-----



[1]

-----



[1]

-----


[1]

-----


Appendix

# Hidden state

「For vanilla recurrent neural networks and GRU’s, the output is the hidden state. When you see both an output and a hidden state represented as two different variables, usually the output is something that ran through some sort of activation like softmax for classification.」[7]。

隱藏狀態跟輸出的差異在於通常輸出會比隱藏狀態多一個激活函數,例如 Softmax,以用來分類。

----- 

References

◎ 論文

[1] Seq2seq - using LSTM
Sutskever, Ilya, Oriol Vinyals, and Quoc V. Le. "Sequence to sequence learning with neural networks." Advances in neural information processing systems. 2014.
http://papers.nips.cc/paper/5346-sequence-to-sequence-learning-with-neural-networks.pdf

-----

◎ 英文

[2] Forecasting with Neural Networks - An Introduction to Sequence-to-Sequence Modeling Of Time Series – Joseph Eddy – Data scientist, teacher, debate coach

# 1.6k claps 
[3] Word Level English to Marathi Neural Machine Translation using Encoder-Decoder Model
https://towardsdatascience.com/word-level-english-to-marathi-neural-machine-translation-using-seq2seq-encoder-decoder-lstm-model-1a913f2dc4a7 

# 921 claps
[4] Seq2Seq model in TensorFlow - Towards Data Science
https://towardsdatascience.com/seq2seq-model-in-tensorflow-ec0c557e560f


# 77 claps
[5] Introduction to RNNs, Sequence to Sequence Language Translation and Attention
https://towardsdatascience.com/introduction-to-rnns-sequence-to-sequence-language-translation-and-attention-fc43ef2cc3fd

# 33 claps
[6] Tuned version of seq2seq tutorial - Towards Data Science
https://towardsdatascience.com/tuned-version-of-seq2seq-tutorial-ddb64db46e2

[7] Difference between output and hidden state in RNN   MLQuestions
https://www.reddit.com/r/MLQuestions/comments/9hpkc4/difference_between_output_and_hidden_state_in_rnn/

-----

◎ 簡中

[8] seq2seq学习笔记 - 大学之道,在明明德 - CSDN博客
https://blog.csdn.net/Jerr__y/article/details/53749693

-----

◎ 繁中

[9] 從零開始的 Sequence to Sequence _ 雷德麥的藏書閣
http://zake7749.github.io/2017/09/28/Sequence-to-Sequence-tutorial/

[10] 教電腦寫作:AI球評——Seq2seq模型應用筆記(PyTorch + Python3) – Yi-Hsiang Kao – Medium
https://medium.com/@gau820827/%E6%95%99%E9%9B%BB%E8%85%A6%E5%AF%AB%E4%BD%9C-ai%E7%90%83%E8%A9%95-seq2seq%E6%A8%A1%E5%9E%8B%E6%87%89%E7%94%A8%E7%AD%86%E8%A8%98-pytorch-python3-31e853573dd0 

[11] Seq2seq pay Attention to Self Attention  Part 1(中文版)
https://medium.com/@bgg/seq2seq-pay-attention-to-self-attention-part-1-%E4%B8%AD%E6%96%87%E7%89%88-2714bbd92727

-----

◎ 代碼實作


[12] Deploying a Seq2Seq Model with the Hybrid Frontend — PyTorch Tutorials 1.0.0.dev20181228 documentation
https://pytorch.org/tutorials/beginner/deploy_seq2seq_hybrid_frontend_tutorial.html 

[13] Seq2Seq – Long – Medium
https://medium.com/@Aj.Cheng/seq2seq-18a0730d1d77

[14] Seq2Seq model in TensorFlow – Towards Data Science
https://towardsdatascience.com/seq2seq-model-in-tensorflow-ec0c557e560f 

[15] A ten-minute introduction to sequence-to-sequence learning in Keras
https://blog.keras.io/a-ten-minute-introduction-to-sequence-to-sequence-learning-in-keras.html

[16] Write a Sequence to Sequence (seq2seq) Model — Chainer 5.0.0 documentation
https://docs.chainer.org/en/stable/examples/seq2seq.html


-----

◎ 其他

[18] The Star Also Rises: BLEU

The Star Also Rises: [翻譯] Word Level English to Marathi Neural Machine Translation using Encoder-Decoder Model

The Star Also Rises: What's the main points of Seq2seq?

-----

Thursday, December 17, 2020

[翻譯] The Illustrated Transformer

[翻譯] The Illustrated Transformer

2019/10/02

----- 

-----

References

-----

The Illustrated Transformer – Jay Alammar – Visualizing machine learning one concept at a time
https://jalammar.github.io/illustrated-transformer/

Tuesday, November 24, 2020

NLP(三):Attention

NLP(三):Attention

2019/01/18

-----


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

# Seq2seq

-----


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

# Attention

-----




Fig. 2. An illustration of the attention mechanism (RNNSearch) proposed by [Bahdanau, 2014]. Instead of converting the entire input sequence into a single context vector, we create a separate context vector for each output (target) word. These vectors consist of the weighted sums of encoder’s hidden states.



# Global Attention。[1]。

-----


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

# Global Attention and Local Attention。

-----



-----



-----

References

◎ 論文

[1] Attention - using GRU
Bahdanau, Dzmitry, Kyunghyun Cho, and Yoshua Bengio. "Neural machine translation by jointly learning to align and translate." arXiv preprint arXiv:1409.0473 (2014).
https://arxiv.org/pdf/1409.0473.pdf

[2] Global Attention - using LSTM
Luong, Minh-Thang, Hieu Pham, and Christopher D. Manning. "Effective approaches to attention-based neural machine translation." arXiv preprint arXiv:1508.04025 (2015).
https://arxiv.org/pdf/1508.04025.pdf 

[3] Visual Attention
Xu, Kelvin, et al. "Show, attend and tell: Neural image caption generation with visual attention." International conference on machine learning. 2015.
http://proceedings.mlr.press/v37/xuc15.pdf

-----




-----

◎ 英文參考資料

# 綜述
[1] Attention  Attention!
https://lilianweng.github.io/lil-log/2018/06/24/attention-attention.html 

# 綜述
# 680 claps
[2] Attention in NLP – Kate Loginova – Medium
https://medium.com/@joealato/attention-in-nlp-734c6fa9d983

# 1.4K claps
Attn  Illustrated Attention - Towards Data Science
https://towardsdatascience.com/attn-illustrated-attention-5ec4ad276ee3

# 1.3K claps
A Brief Overview of Attention Mechanism - SyncedReview - Medium
https://medium.com/syncedreview/a-brief-overview-of-attention-mechanism-13c578ba9129

# 799 claps
Intuitive Understanding of Attention Mechanism in Deep Learning
https://towardsdatascience.com/intuitive-understanding-of-attention-mechanism-in-deep-learning-6c9482aecf4f

# 126 claps
Understanding Attention Mechanism - Shashank Yadav - Medium
https://medium.com/@shashank7.iitd/understanding-attention-mechanism-35ff53fc328e

Attention and Memory in Deep Learning and NLP – WildML
http://www.wildml.com/2016/01/attention-and-memory-in-deep-learning-and-nlp/

-----

◎ 日文參考資料

深層学習による自然言語処理 - RNN, LSTM, ニューラル機械翻訳の理論 - ディープラーニングブログ
http://deeplearning.hatenablog.com/entry/neural_machine_translation_theory

-----

◎ 簡體中文參考資料

# 綜述
Attention-Mechanisms-paper_Attention-mechanisms-paper.md at master · yuquanle_Attention-Mechanisms-paper · GitHub
https://github.com/yuquanle/Attention-Mechanisms-paper/blob/master/Attention-mechanisms-paper.md

深度学习中的注意力模型(2017版) - 知乎
https://zhuanlan.zhihu.com/p/37601161

自然语言处理中的Attention Model:是什么及为什么 - 张俊林的博客 - CSDN博客
https://blog.csdn.net/malefactor/article/details/50550211



目前主流的attention方法都有哪些? - 知乎
https://www.zhihu.com/question/68482809/answer/264632289

# 110 claps
自然语言处理中注意力机制综述 - 知乎
https://zhuanlan.zhihu.com/p/54491016 

# 14 claps
NLP硬核入门-Seq2Seq和Attention机制 - 知乎
https://zhuanlan.zhihu.com/p/73589030

注意力机制(Attention Mechanism)在自然语言处理中的应用 - Soul Joy Hub - CSDN博客
https://blog.csdn.net/u011239443/article/details/80418489

【NLP】Attention Model(注意力模型)学习总结 - 郭耀华 - 博客园
https://www.cnblogs.com/guoyaohua/p/9429924.html
 
-----

◎ 繁體中文參考資料

# 486 claps
[1] Seq2seq pay Attention to Self Attention  Part 1(中文版)
https://medium.com/@bgg/seq2seq-pay-attention-to-self-attention-part-1-%E4%B8%AD%E6%96%87%E7%89%88-2714bbd92727

-----

◎ 代碼實作

Translation with a Sequence to Sequence Network and Attention — PyTorch Tutorials 1.0.0.dev20181228 documentation
https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html

PyTorch 1.1 Tutorials   テキスト   Sequence to Sequence ネットワークと Attention で翻訳 – PyTorch
http://torch.classcat.com/2019/07/20/pytorch-1-1-tutorials-text-seq2seq-translation/

# 832 claps
Attention in Deep Networks with Keras - Towards Data Science
https://towardsdatascience.com/light-on-math-ml-attention-with-keras-dc8dbc1fad39