この記事の読者
キーワード・知ってると理解がしやすい
- Attention
- Memory Network
Attention まとめ yhayato1320.hatenablog.com
Index
- Index
- Transformer とは
- Architecture
- Attention Layer
- Position-Wise Fully Connected Feed Forward Network
- ネットワークの入力
- 学習 / Training
- 参考
Transformer とは
Seq2Seq (Encoder - Decoder Model) をベースに
再帰的なニューラルネットワーク(RNN etc)を廃止し、
代わりに、Feed Forward Network (FFN)と
Multi Head Self Attention の 2 つレイヤで構成されたモデル.
メリット
RNN のような再帰的な計算をなくすことで、計算コストを低下させる.
つまり、再帰的な計算だと、前の計算が終わらないと次の計算ができないが、
時系列の情報を行列で扱うので、行列演算でえいやできる.
Architecture
Encoder と Decoder の2つから構成されている. (Encoder-Decoder Model)
- Encoder 入力系列 :
- 入力系列の長さ :
- Encoder 出力 (Decoder 入力) :
- 出力(入力)系列の長さ :
- Decoder 入力系列 :
- 入力系列の長さ :
Encoder
いくかの (発表論文では6) のLayer(= Encoder Layer)で、構成されている.
その Encoder Layer は 2 つの Layer で構成されている.
- Multi-Head Self-Attention Layer (Attention)
- Position-Wise Fully Connected Feed-Forward Network (FFN)
また、Residual Connections を導入して、以前の情報を生かす.
その後、Layer Normalization を行う.
Decoder
いくかの (発表論文では6) のLayer(= Decoder Layer)で、構成されている.
その Decoder Layer は 3 つの Layer で構成されている.
- Position-Wise Fully Connected Feed-Forward Network (FFN)
- Multi-Head Self-Attention Layer (Attention)
- Masked Multi-Head Self-Attention Layer (Attention)
Encoder と異なるのは、「Masked Multi-Head Self-Attention Layer」の部分.(あと 2 つは同様)
Attention Layer
Attention Layer は、Encoder / Decoder のどちらにも組み込まれている.
- Encoder : Multi-Head Self-Attention Layer
- Decoder : Multi-Head Self-Attention Layer + Masked Multi-Head Self-Attention Layer
Multi-Head Self-Attention Layer の性質・工夫・構成要素は以下.
- Self Attention
- Scaled Dot Product Attention
- Multi Head Attention
NLP のタスクとして穴埋めを行うため、マスクを施した入力に対応した形式にしたもの.
(Masked Self Attention?)
Self Attention
入力に対し、入力自身のどの部分に注目するかを重み付けする仕組み.
Encoder の情報も重要視するかどうかも、重み付けするので、Self Attention というよりは、Source Target Attention に近いイメージか?
Scaled Dot Product Attention
Query と Key の類似度を計算する手段である スコア関数に、行列の内積(Dot Product) を採用している Attention.
Multi Head Attention
表現力を増やすために、複数のAttention のための重みを利用する仕組み.
(層を増やす訳ではないので、逐次的な計算が増える訳ではなく、
並列的な計算を行列計算で行える.)
Position-Wise Fully Connected Feed Forward Network
Encoder / Decoder にある、Attention の後にある処理.
全結合 + ReLU + 全結合の処理.
位置単位(単語単位)に、共通のFeed Forward Network を適用する.
Layer ごとには、異なる重みを利用する.
ネットワークの入力
Encoder / Decoder 以外の部分の Architecture にも着目してみる.
(主に入力部分)
2つの処理が、行われている.
- Embedding
- Encoding
Embedding
事前学習モデル(分散表現)を利用して、(Encoder / Decoder への)
入力情報(入力トークンと出力トークン)をベクトルに変換する.
Encoder の Embedding Layer (分散表現)と、Decoder の Embedding Layer (分散表現)と、
Decoder の出力部分の線形変換の重みを共通のものを利用する.
参考 : Using the output embedding to improve language models.
Positional Encoding
Encoder と Decoder への入力前(Embedding の後)に行う処理.
目的は、入力の位置関係(時系列関係)を表現すること.
Transformer は、再帰的なシステム(ネットワーク)をなくしたため、(単語の)時系列的ま情報が表現されていない. そのため、時系列関係の情報を加えることで、時系列情報を所持する.
使用される関数は、sin / cos を利用する. この関数の決定には、様々な選択肢があり、検討の余地がある.
学習 / Training
データセット
ハードウェアと訓練時間
Optimizer
Adam を利用.
正則化 / Regularization
3 つの正則化を実施.
- Encoder / Decoder
- 各サブレイヤーの正規化 (Layer Normalization) の前に Dropout を適用
- Word Embedding / Positional Encoding の後に Dropout を適用
- ラベルスムージング / Label Smoothing
参考
- Attention Is All You Need
- [2017 Google]
- 1 Introduction
- 3 Model Architecture
- 3.1 Encoder and Decoder Stacks
- 3.2 Attention
- 3.2.1 Scaled Dot-Product Attention
- 3.2.2 Multi-Head Attention
- 3.3 Position-wise Feed-Forward Networks
- 3.4 Embeddings and Softmax
- 3.5 Positional Encoding
- 5 Training
- 5.1 Training Data and Batching
- 5.2 Hardware and Schedule
- 5.3 Optimizer
- 5.4 Regularization
- arxiv.org
Web サイト
- 深層学習界の大前提Transformerの論文解説!