第11章注意機構とTransformer
11.4. The Bahdanau Attention Mechanism11.4 Bahdanauの注意機構
When we encountered machine translation in Section 10.7, we designed an encoder–decoder architecture for sequence-to-sequence learning based on two RNNs (Sutskever et al., 2014). Specifically, the RNN encoder transforms a variable-length sequence into a fixed-shape context variable. Then, the RNN decoder generates the output (target) sequence token by token based on the generated tokens and the context variable.
英語のヒント
10.7節で機械翻訳に出会ったとき、二つのRNNにもとづく系列から系列への学習のためのエンコーダ・デコーダ構造を設計した(Sutskever et al., 2014)。具体的には、RNNのエンコーダが可変長の系列を固定された形状の文脈変数に変換する。次に、RNNのデコーダが、生成したトークンと文脈変数にもとづいて、出力(目標)系列をトークンごとに生成する。
Recall Fig. 10.7.2 which we repeat (Fig. 11.4.1) with some additional detail. Conventionally, in an RNN all relevant information about a source sequence is translated into some internal fixed-dimensional state representation by the encoder. It is this very state that is used by the decoder as the complete and exclusive source of information for generating the translated sequence. In other words, the sequence-to-sequence mechanism treats the intermediate state as a sufficient statistic of whatever string might have served as input.
英語のヒント
Fig. 11.4.1図11.4.1
Sequence-to-sequence model. The state, as generated by the encoder, is the only piece of information shared between the encoder and the decoder.
英語のヒント
系列から系列へのモデル。エンコーダが生成する状態が、エンコーダとデコーダのあいだで共有される唯一の情報である。
While this is quite reasonable for short sequences, it is clear that it is infeasible for long ones, such as a book chapter or even just a very long sentence. After all, before too long there will simply not be enough “space” in the intermediate representation to store all that is important in the source sequence. Consequently the decoder will fail to translate long and complex sentences. One of the first to encounter this was Graves (2013) who tried to design an RNN to generate handwritten text. Since the source text has arbitrary length they designed a differentiable attention model to align text characters with the much longer pen trace, where the alignment moves only in one direction. This, in turn, draws on decoding algorithms in speech recognition, e.g., hidden Markov models (Rabiner and Juang, 1993).
英語のヒント
これは短い系列ならかなり理にかなっているが、本の一章や、ごく長い一文でさえ、長い系列には実行できないことは明らかである。結局のところ、遠からず、中間の表現には、原文の系列で重要なことをすべて保存するだけの「空間」がなくなる。その結果、デコーダは長く複雑な文を翻訳できなくなる。これに最初に出会った一人がGraves (2013)で、手書きのテキストを生成するRNNを設計しようとした。原文のテキストは任意の長さをもつので、テキストの文字をずっと長いペンの軌跡にそろえる微分可能な注意のモデルを設計した。そこでは、そろえ方は一方向にしか動かない。これは、たとえば隠れマルコフモデル(Rabiner and Juang, 1993)のような、音声認識の復号のアルゴリズムを土台にしている。
Inspired by the idea of learning to align, Bahdanau et al. (2014) proposed a differentiable attention model without the unidirectional alignment limitation. When predicting a token, if not all the input tokens are relevant, the model aligns (or attends) only to parts of the input sequence that are deemed relevant to the current prediction. This is then used to update the current state before generating the next token. While quite innocuous in its description, this Bahdanau attention mechanism has arguably turned into one of the most influential ideas of the past decade in deep learning, giving rise to Transformers (Vaswani et al., 2017) and many related new architectures.
英語のヒント
そろえ方を学習するという考えに着想を得て、Bahdanau et al. (2014)は、一方向のそろえ方という制限のない、微分可能な注意のモデルを提案した。トークンを予測するとき、入力のトークンのすべてが関係するわけではない場合、モデルは、現在の予測に関係すると判断した入力系列の部分にだけそろえる(つまり注意を向ける)。そして、これを使って次のトークンを生成する前に現在の状態を更新する。その記述はかなり無害に見えるが、このBahdanauの注意機構は、おそらく深層学習の過去10年で最も影響力のある考えの一つになり、Transformer(Vaswani et al., 2017)や多くの関連する新しい構造を生んだ。
import torch
from torch import nn
from d2l import torch as d2l