# Transformer ## 過去回 [第1回:NLP Outline](https://hackmd.io/uD2hEISAShqVHD6IRtceUw) [第2回:Word2Vec](https://hackmd.io/T2QJsuwsROSQluklguDOow) [第3回:Doc2Vec](https://hackmd.io/LIT2hp_jQ56Dvn-imOVemg) [第4回:Seq2Seq](https://hackmd.io/UGLHXO6-RweY_Bys2Dp4mA) [第5回:Seq2Seq Attention](https://hackmd.io/D8xqAyHjRte3A0oENtLXeQ) ## 元論文 https://arxiv.org/pdf/1706.03762.pdf ## 参考 Transformerの行列イラストが分かりやすい http://jalammar.github.io/illustrated-transformer/ 日本語では一番かも http://deeplearning.hatenablog.com/entry/transformer 実装例が書いてある https://qiita.com/halhorn/items/c91497522be27bde17ce transfomerのTensorの流れが書いてある https://qiita.com/FuwaraMiyasaki/items/239f3528053889847825 Layer-NormalizationとLabel smoothing についても触れている https://qiita.com/omiita/items/07e69aef6c156d23c538 その他 https://deepsquare.jp/2020/07/transformer/ ## 要点 * **RNN Encoder-Decoder + AttentionはNLP界を牽引して来たが、並列処理ができないという大きな欠点があった** * **RNN をやめて ==Attention だけを使った==ものがTransformer** * **基本的には、Seq2Seqの進化系** * **BERTは、TransformerのEncoderだけを使ったモデル** ## 全体像  ## Positional Encoding  * TransformerではRNNをやめた * RNNでは、文章内の語順は、RNNへの入力の順番という形で学習されていた * RNNをやめたら、その語順はどう学習できるのか? * それを解決するのが、==Positional Encoding== である * Positional Encoding では以下の式によって、==位置情報を追加==する  ## Attention [参考](http://deeplearning.hatenablog.com/entry/transformer) ### Seq2SeqにおけるAttention   ### Attention weight の算出方法 TransfomerやSeq2Seq Attention では==Dot-Product Attention==(右側)を使っている。こちらは、パラメータが無く、高速である利点がある。  ### Attention の種類 KeyとValueが同じ場合は==Source-Traget Attention==(左側)、全部同じInputを使う場合は ==Self-Attention== という。 Transformerでは、**EncoderではSelf-Attention**が、**DecoderではSource-Target Attention** が使われている。  ### ※別図Attention(FNNのDense Weight付き図) [参考](https://qiita.com/halhorn/items/c91497522be27bde17ce#%E5%9F%BA%E6%9C%AC%E7%9A%84%E3%81%AA-attention)   ## Multi-head Self-attention mechanism ここの説明  ### Scaled Dot-Product (Self) Attention  ### Multi-head Attention 論文ではこんな絵  自分なりに書くとこう。アンサンブル学習のようなもの。もしくはCNNのフィルター  Multi-head Attention Weight を可視化するとこんな感じ [参考](https://deepsquare.jp/2020/07/transformer/)  ### Add & Norm Residual Block と Layer-Normalization が使われている。 CNNで例えると、Batch-NormalizationはBatch内のiチャネル に対して標準化されるが、Layer-Normalization は ==1 data 内の チャネルに跨って標準化==される。 Transformaer では、チャネルの数はインプットの単語数となる。  Residual Block はResNetのそれ。  ## Position-wise Feed-Forward Networks ここの箇所.  単語の位置毎に共通のWeightを持ったFNNを使っている。   ## Decoder Attention (Mask) 基本的にはEncoderと同じだが、未来の単語はMaskしないといけない。Seq2Seqの構造と同じである。ある単語の入力時点で計算されるLossは、それ以降の単語の情報を参照してはいけない。  ### Mask Softmax の入力の前に、未来の情報は-infに置き換えることでMaskする。-infにすると、Softmax の 変換で確率が0になるため。  ## ※補足※Prediction Decoder側でもSelf-Attentionしているので、予測した文字と、==それまでの文字も入力する必要がある== 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up