プリンキピア

第11章注意機構とTransformer

11.7.1. Model11.7.1 モデル

As an instance of the encoder–decoder architecture, the overall architecture of the Transformer is presented in Fig. 11.7.1. As we can see, the Transformer is composed of an encoder and a decoder. In contrast to Bahdanau attention for sequence-to-sequence learning in Fig. 11.4.2, the input (source) and output (target) sequence embeddings are added with positional encoding before being fed into the encoder and the decoder that stack modules based on self-attention.
英語のヒント
エンコーダ・デコーダ構造の一例として、Transformerの全体の構造を図11.7.1に示す。見てのとおり、Transformerはエンコーダとデコーダからなる。図11.4.2の系列から系列への学習のためのBahdanauの注意とは対照的に、入力(原文)と出力(目標)の系列の埋め込みには、自己注意にもとづくモジュールを積み重ねたエンコーダとデコーダに入れられる前に、位置符号化が加えられる。
Fig. 11.7.1図11.7.1
The Transformer architecture.
英語のヒント
Transformerの構造。
Now we provide an overview of the Transformer architecture in Fig. 11.7.1. At a high level, the Transformer encoder is a stack of multiple identical layers, where each layer has two sublayers (either is denoted as sublayer). The first is a multi-head self-attention pooling and the second is a positionwise feed-forward network. Specifically, in the encoder self-attention, queries, keys, and values are all from the outputs of the previous encoder layer. Inspired by the ResNet design of Section 8.6, a residual connection is employed around both sublayers. In the Transformer, for any input 𝐱∈ℝd at any position of the sequence, we require that sublayer(𝐱)∈ℝd so that the residual connection 𝐱+sublayer(𝐱)∈ℝd is feasible. This addition from the residual connection is immediately followed by layer normalization (Ba et al., 2016). As a result, the Transformer encoder outputs a d-dimensional vector representation for each position of the input sequence.
英語のヒント
では、図11.7.1のTransformerの構造を概観しよう。大まかに言えば、Transformerのエンコーダは、同一の層を複数積み重ねたものであり、各層には二つの副層(どちらもsublayerと表す)がある。1番目はマルチヘッドの自己注意プーリング、2番目は位置ごとのフィードフォワードネットワークである。具体的には、エンコーダの自己注意では、クエリ、キー、バリューはすべて前のエンコーダ層の出力から来る。8.6節のResNetの設計に着想を得て、二つの副層の両方のまわりに残差接続が使われる。Transformerでは、系列の任意の位置にある任意の入力𝐱∈ℝdについて、残差接続𝐱+sublayer(𝐱)∈ℝdが成り立つように、sublayer(𝐱)∈ℝdであることを求める。この残差接続による加算の直後には、層正規化(Ba et al., 2016)が続く。その結果、Transformerのエンコーダは、入力系列の各位置についてd次元のベクトル表現を出力する。
The Transformer decoder is also a stack of multiple identical layers with residual connections and layer normalizations. As well as the two sublayers described in the encoder, the decoder inserts a third sublayer, known as the encoder–decoder attention, between these two. In the encoder–decoder attention, queries are from the outputs of the decoder’s self-attention sublayer, and the keys and values are from the Transformer encoder outputs. In the decoder self-attention, queries, keys, and values are all from the outputs of the previous decoder layer. However, each position in the decoder is allowed only to attend to all positions in the decoder up to that position. This masked attention preserves the autoregressive property, ensuring that the prediction only depends on those output tokens that have been generated.
英語のヒント
Transformerのデコーダも、残差接続と層正規化をもつ同一の層を複数積み重ねたものである。エンコーダで説明した二つの副層に加えて、デコーダは、エンコーダ・デコーダ注意と呼ばれる3番目の副層を、それら二つのあいだに挿入する。エンコーダ・デコーダ注意では、クエリはデコーダの自己注意の副層の出力から来て、キーとバリューはTransformerのエンコーダの出力から来る。デコーダの自己注意では、クエリ、キー、バリューはすべて前のデコーダ層の出力から来る。ただし、デコーダの各位置は、デコーダのその位置までのすべての位置にだけ注意を向けることが許される。このマスクつきの注意は自己回帰性を保ち、予測が生成済みの出力トークンだけに依存するようにする。
We have already described and implemented multi-head attention based on scaled dot products in Section 11.5 and positional encoding in Section 11.6.3. In the following, we will implement the rest of the Transformer model.
英語のヒント
スケール化内積にもとづくマルチヘッド注意は11.5節で、位置符号化は11.6.3節で、すでに説明し実装した。以下では、Transformerのモデルの残りを実装する。