第11章注意機構とTransformer
11.9.1. Encoder-Only11.9.1 エンコーダのみ
When only the Transformer encoder is used, a sequence of input tokens is converted into the same number of representations that can be further projected into output (e.g., classification). A Transformer encoder consists of self-attention layers, where all input tokens attend to each other. For example, vision Transformers depicted in Fig. 11.8.1 are encoder-only, converting a sequence of input image patches into the representation of a special “<cls>” token. Since this representation depends on all input tokens, it is further projected into classification labels. This design was inspired by an earlier encoder-only Transformer pretrained on text: BERT (Bidirectional Encoder Representations from Transformers) (Devlin et al., 2018).
英語のヒント
Transformerのエンコーダだけを使う場合、入力トークンの系列は同じ数の表現に変換され、それをさらに出力(たとえば分類)に射影できる。Transformerのエンコーダは自己注意の層からなり、そこではすべての入力トークンが互いに注意を向け合う。たとえば、図11.8.1に描いた視覚のTransformerはエンコーダのみであり、入力の画像のパッチの系列を特別な「<cls>」トークンの表現に変換する。この表現はすべての入力トークンに依存するので、さらに分類のラベルに射影される。この設計は、テキストで事前学習された以前のエンコーダのみのTransformer、BERT(Bidirectional Encoder Representations from Transformers)(Devlin et al., 2018)に着想を得ている。
11.9.1.1. Pretraining BERT11.9.1.1 BERTの事前学習
Fig. 11.9.1図11.9.1
Left: Pretraining BERT with masked language modeling. Prediction of the masked “love” token depends on all input tokens before and after “love”. Right: Attention pattern in the Transformer encoder. Each token along the vertical axis attends to all input tokens along the horizontal axis.
英語のヒント
左:マスク言語モデリングによるBERTの事前学習。マスクされた「love」トークンの予測は、「love」の前と後のすべての入力トークンに依存する。右:Transformerのエンコーダの注意のパターン。縦軸に沿った各トークンは、横軸に沿ったすべての入力トークンに注意を向ける。
BERT is pretrained on text sequences using masked language modeling: input text with randomly masked tokens is fed into a Transformer encoder to predict the masked tokens. As illustrated in Fig. 11.9.1, an original text sequence “I”, “love”, “this”, “red”, “car” is prepended with the “<cls>” token, and the “<mask>” token randomly replaces “love”; then the cross-entropy loss between the masked token “love” and its prediction is to be minimized during pretraining. Note that there is no constraint in the attention pattern of Transformer encoders (right of Fig. 11.9.1) so all tokens can attend to each other. Thus, prediction of “love” depends on input tokens before and after it in the sequence. This is why BERT is a “bidirectional encoder”. Without need for manual labeling, large-scale text data from books and Wikipedia can be used for pretraining BERT.
英語のヒント
BERTは、マスク言語モデリングを使ってテキストの系列で事前学習される。ランダムにマスクしたトークンをもつ入力テキストをTransformerのエンコーダに入れ、マスクしたトークンを予測させる。図11.9.1に示すように、元のテキストの系列「I」「love」「this」「red」「car」の前に「<cls>」トークンを付け、「<mask>」トークンがランダムに「love」を置き換える。そして、事前学習のあいだ、マスクしたトークン「love」とその予測とのあいだの交差エントロピー損失を最小化する。Transformerのエンコーダの注意のパターン(図11.9.1の右)には制約がないので、すべてのトークンが互いに注意を向け合えることに注意してほしい。したがって、「love」の予測は、系列の中でそれより前と後の入力トークンに依存する。これが、BERTが「双方向のエンコーダ」である理由である。手作業のラベルづけが要らないので、本やウィキペディアからの大規模なテキストデータをBERTの事前学習に使える。
11.9.1.2. Fine-Tuning BERT11.9.1.2 BERTの微調整
The pretrained BERT can be fine-tuned to downstream encoding tasks involving single text or text pairs. During fine-tuning, additional layers can be added to BERT with randomized parameters: these parameters and those pretrained BERT parameters will be updated to fit training data of downstream tasks.
英語のヒント
事前学習したBERTは、単一のテキストやテキストの組を含む下流の符号化の課題に合わせて微調整できる。微調整のあいだ、ランダムな値のパラメータをもつ層を追加でBERTに加えられる。これらのパラメータと事前学習したBERTのパラメータは、下流の課題の訓練データに当てはまるように更新される。
Fig. 11.9.2図11.9.2
Fine-tuning BERT for sentiment analysis.
英語のヒント
感情分析のためのBERTの微調整。
Fig. 11.9.2 illustrates fine-tuning of BERT for sentiment analysis. The Transformer encoder is a pretrained BERT, which takes a text sequence as input and feeds the “<cls>” representation (global representation of the input) into an additional fully connected layer to predict the sentiment. During fine-tuning, the cross-entropy loss between the prediction and the label on sentiment analysis data is minimized via gradient-based algorithms, where the additional layer is trained from scratch while pretrained parameters of BERT are updated. BERT does more than sentiment analysis. The general language representations learned by the 350-million-parameter BERT from 250 billion training tokens advanced the state of the art for natural language tasks such as single text classification, text pair classification or regression, text tagging, and question answering.
英語のヒント
図11.9.2は、感情分析のためのBERTの微調整を示している。Transformerのエンコーダは事前学習したBERTであり、テキストの系列を入力としてとり、「<cls>」の表現(入力の大域的な表現)を追加の全結合層に入れて感情を予測する。微調整のあいだ、感情分析のデータでの予測とラベルとのあいだの交差エントロピー損失を、勾配にもとづくアルゴリズムで最小化する。そこでは、追加の層はゼロから訓練され、BERTの事前学習したパラメータは更新される。BERTは感情分析以上のことをする。2500億の訓練トークンから3億5000万パラメータのBERTが学習した一般的な言語の表現は、単一テキストの分類、テキストの組の分類や回帰、テキストのタグづけ、質問応答のような自然言語の課題で、最先端を押し上げた。
You may note that these downstream tasks include text pair understanding. BERT pretraining has another loss for predicting whether one sentence immediately follows the other. However, this loss was later found to be less useful when pretraining RoBERTa, a BERT variant of the same size, on 2000 billion tokens (Liu et al., 2019). Other derivatives of BERT improved model architectures or pretraining objectives, such as ALBERT (enforcing parameter sharing) (Lan et al., 2019), SpanBERT (representing and predicting spans of text) (Joshi et al., 2020), DistilBERT (lightweight via knowledge distillation) (Sanh et al., 2019), and ELECTRA (replaced token detection) (Clark et al., 2020). Moreover, BERT inspired Transformer pretraining in computer vision, such as with vision Transformers (Dosovitskiy et al., 2021), Swin Transformers (Liu et al., 2021), and MAE (masked autoencoders) (He et al., 2022).
英語のヒント
これらの下流の課題には、テキストの組の理解が含まれることに気づいたかもしれない。BERTの事前学習には、ある文が別の文の直後に続くかどうかを予測するための損失がもう一つある。しかし、この損失は、同じ大きさのBERTの変種であるRoBERTaを2兆トークンで事前学習したときに、あまり役に立たないことが後でわかった(Liu et al., 2019)。BERTのほかの派生は、モデルの構造や事前学習の目的を改良した。たとえば、ALBERT(パラメータの共有を強制する)(Lan et al., 2019)、SpanBERT(テキストの区間を表現し予測する)(Joshi et al., 2020)、DistilBERT(知識蒸留による軽量化)(Sanh et al., 2019)、ELECTRA(置き換えたトークンの検出)(Clark et al., 2020)である。さらに、BERTは、視覚のTransformer(Dosovitskiy et al., 2021)、Swin Transformer(Liu et al., 2021)、MAE(マスクつきオートエンコーダ)(He et al., 2022)のような、コンピュータビジョンでのTransformerの事前学習に着想を与えた。