プリンキピア

第11章注意機構とTransformer

11.1. Queries, Keys, and Values11.1 クエリ、キー、バリュー

So far all the networks we have reviewed crucially relied on the input being of a well-defined size. For instance, the images in ImageNet are of size 224×224 pixels and CNNs are specifically tuned to this size. Even in natural language processing the input size for RNNs is well defined and fixed. Variable size is addressed by sequentially processing one token at a time, or by specially designed convolution kernels (Kalchbrenner et al., 2014). This approach can lead to significant problems when the input is truly of varying size with varying information content, such as in Section 10.7 in the transformation of text (Sutskever et al., 2014). In particular, for long sequences it becomes quite difficult to keep track of everything that has already been generated or even viewed by the network. Even explicit tracking heuristics such as proposed by Yang et al. (2016) only offer limited benefit.
英語のヒント
これまでに見てきたネットワークはどれも、入力がはっきり決まった大きさであることに決定的に頼っていた。たとえば、ImageNetの画像の大きさは224×224画素であり、CNNはこの大きさに特に合わせて調整されている。自然言語処理でさえ、RNNの入力の大きさははっきり決まっていて固定されている。可変の大きさには、一度に一つのトークンずつ順に処理するか、特別に設計した畳み込みのカーネル(Kalchbrenner et al., 2014)で対処する。このやり方は、10.7節のテキストの変換(Sutskever et al., 2014)のように、入力の大きさが本当にまちまちで、含まれる情報の量もまちまちな場合には、大きな問題につながりうる。特に、長い系列では、ネットワークがすでに生成した、あるいは見たものすべてを追い続けるのはかなり難しくなる。Yang et al. (2016)が提案したような明示的な追跡の経験則でさえ、限られた利点しか与えない。
Compare this to databases. In their simplest form they are collections of keys (k) and values (v). For instance, our database 𝒟 might consist of tuples {(“Zhang”, “Aston”), (“Lipton”, “Zachary”), (“Li”, “Mu”), (“Smola”, “Alex”), (“Hu”, “Rachel”), (“Werness”, “Brent”)} with the last name being the key and the first name being the value. We can operate on 𝒟, for instance with the exact query (q) for “Li” which would return the value “Mu”. If (“Li”, “Mu”) was not a record in 𝒟, there would be no valid answer. If we also allowed for approximate matches, we would retrieve (“Lipton”, “Zachary”) instead. This quite simple and trivial example nonetheless teaches us a number of useful things:
英語のヒント
これをデータベースと比べてみよう。最も単純な形では、データベースはキー(k)とバリュー(v)の集まりである。たとえば、データベース𝒟は、姓をキー、名をバリューとする組{(“Zhang”, “Aston”), (“Lipton”, “Zachary”), (“Li”, “Mu”), (“Smola”, “Alex”), (“Hu”, “Rachel”), (“Werness”, “Brent”)}からなるかもしれない。𝒟に対して操作を行える。たとえば、「Li」に対する完全一致のクエリ(q)は、バリュー「Mu」を返す。(“Li”, “Mu”)が𝒟の記録になければ、有効な答えはない。近似的な一致も許すなら、代わりに(“Lipton”, “Zachary”)を取り出すことになる。このごく単純で取るに足らない例でも、役に立つことをいくつも教えてくれる。
  • We can design queries q that operate on (k,v) pairs in such a manner as to be valid regardless of the database size.
    英語のヒント
    データベースの大きさにかかわらず有効になるように、(k, v)の組に働くクエリqを設計できる。
  • The same query can receive different answers, according to the contents of the database.
    英語のヒント
    同じクエリでも、データベースの中身によって違う答えを受け取りうる。
  • The “code” being executed for operating on a large state space (the database) can be quite simple (e.g., exact match, approximate match, top-k).
    英語のヒント
    大きな状態空間(データベース)に対して操作するために実行される「コード」は、かなり単純でありうる(たとえば完全一致、近似的な一致、上位k件)。
  • There is no need to compress or simplify the database to make the operations effective.
    英語のヒント
    操作を効果的にするために、データベースを圧縮したり単純にしたりする必要はない。
Clearly we would not have introduced a simple database here if it wasn’t for the purpose of explaining deep learning. Indeed, this leads to one of the most exciting concepts introduced in deep learning in the past decade: the attention mechanism (Bahdanau et al., 2014). We will cover the specifics of its application to machine translation later. For now, simply consider the following: denote by 𝒟=def{(𝐤1,𝐯1),…(𝐤m,𝐯m)} a database of m tuples of keys and values. Moreover, denote by 𝐪 a query. Then we can define the attention over 𝒟 as
英語のヒント
明らかに、深層学習を説明する目的がなければ、ここで単純なデータベースを紹介はしなかっただろう。実際、これは、過去10年に深層学習で導入された最もわくわくする概念の一つ、注意機構(Bahdanau et al., 2014)につながる。機械翻訳へのその応用の詳細は後で扱う。今のところは、単に次のことを考えよう。キーとバリューのm個の組からなるデータベースを𝒟=def{(𝐤1,𝐯1),…(𝐤m,𝐯m)}と表す。さらに、クエリを𝐪と表す。すると、𝒟上の注意を次のように定義できる。
Attention(𝐪,𝒟)=def∑i=1mα(𝐪,𝐤i)𝐯i,(11.1.1)
where α(𝐪,𝐤i)∈ℝ (i=1,…,m) are scalar attention weights. The operation itself is typically referred to as attention pooling. The name attention derives from the fact that the operation pays particular attention to the terms for which the weight α is significant (i.e., large). As such, the attention over 𝒟 generates a linear combination of values contained in the database. In fact, this contains the above example as a special case where all but one weight is zero. We have a number of special cases:
英語のヒント
ここで、α(𝐪,𝐤i)∈ℝ(i=1,…,m)はスカラーの注意の重みである。この演算そのものは、ふつう注意プーリングと呼ばれる。注意という名前は、この演算が、重みαが大きい(つまり、値が大きい)項に特に注意を払うことに由来する。したがって、𝒟上の注意は、データベースに含まれるバリューの線形結合を生成する。実際、これは上の例を、一つを除くすべての重みが0である特別な場合として含んでいる。特別な場合がいくつかある。
  • The weights α(𝐪,𝐤i) are nonnegative. In this case the output of the attention mechanism is contained in the convex cone spanned by the values 𝐯i.
    英語のヒント
    重みα(𝐪,𝐤i)が非負である。この場合、注意機構の出力は、バリュー𝐯iが張る凸錐に含まれる。
  • The weights α(𝐪,𝐤i) form a convex combination, i.e., ∑iα(𝐪,𝐤i)=1 and α(𝐪,𝐤i)≥0 for all i. This is the most common setting in deep learning.
    英語のヒント
    重みα(𝐪,𝐤i)が凸結合をなす。つまり、すべてのiについて∑iα(𝐪,𝐤i)=1かつα(𝐪,𝐤i)≥0である。これは深層学習で最もよくある設定である。
  • Exactly one of the weights α(𝐪,𝐤i) is 1, while all others are 0. This is akin to a traditional database query.
    英語のヒント
    重みα(𝐪,𝐤i)のうちちょうど一つが1で、ほかはすべて0である。これは伝統的なデータベースのクエリに近い。
  • All weights are equal, i.e., α(𝐪,𝐤i)=1m for all i. This amounts to averaging across the entire database, also called average pooling in deep learning.
    英語のヒント
    すべての重みが等しい。つまり、すべてのiについてα(𝐪,𝐤i)=1mである。これはデータベース全体で平均をとることにあたり、深層学習では平均プーリングとも呼ばれる。
A common strategy for ensuring that the weights sum up to 1 is to normalize them via
英語のヒント
重みの和が1になることを保証するよくある戦略は、次によって重みを正規化することである。
α(𝐪,𝐤i)=α(𝐪,𝐤i)∑jα(𝐪,𝐤j).(11.1.2)
In particular, to ensure that the weights are also nonnegative, one can resort to exponentiation. This means that we can now pick any function a(𝐪,𝐤) and then apply the softmax operation used for multinomial models to it via
英語のヒント
特に、重みが非負でもあることを保証するため、指数関数を使うことができる。つまり、任意の関数a(𝐪,𝐤)を選び、それに多項モデルで使うソフトマックス演算を次のように当てはめればよい。
α(𝐪,𝐤i)=exp⁡(a(𝐪,𝐤i))∑jexp⁡(a(𝐪,𝐤j)).(11.1.3)
This operation is readily available in all deep learning frameworks. It is differentiable and its gradient never vanishes, all of which are desirable properties in a model. Note though, the attention mechanism introduced above is not the only option. For instance, we can design a non-differentiable attention model that can be trained using reinforcement learning methods (Mnih et al., 2014). As one would expect, training such a model is quite complex. Consequently the bulk of modern attention research follows the framework outlined in Fig. 11.1.1. We thus focus our exposition on this family of differentiable mechanisms.
英語のヒント
この演算は、どの深層学習のフレームワークでもすぐに使える。微分可能で、その勾配は決して消えない。これらはどれもモデルにとって望ましい性質である。ただし、上で紹介した注意機構だけが選択肢ではないことに注意してほしい。たとえば、強化学習の手法で訓練できる、微分できない注意のモデルを設計することもできる(Mnih et al., 2014)。予想されるとおり、そうしたモデルの訓練はかなり複雑である。そのため、現代の注意の研究の大部分は、図11.1.1に示した枠組みに従っている。そこで、説明はこの微分可能な仕組みの一族に絞る。
Fig. 11.1.1図11.1.1
The attention mechanism computes a linear combination over values 𝐯𝑖 via attention pooling, where weights are derived according to the compatibility between a query 𝐪 and keys 𝐤𝑖.
英語のヒント
注意機構は、注意プーリングによってバリュー𝐯𝑖の線形結合を計算する。その重みは、クエリ𝐪とキー𝐤𝑖の適合度に応じて導かれる。
What is quite remarkable is that the actual “code” for executing on the set of keys and values, namely the query, can be quite concise, even though the space to operate on is significant. This is a desirable property for a network layer as it does not require too many parameters to learn. Just as convenient is the fact that attention can operate on arbitrarily large databases without the need to change the way the attention pooling operation is performed.
英語のヒント
かなり注目すべきなのは、キーとバリューの集合に対して実行する実際の「コード」、つまりクエリが、操作する空間が大きくても、かなり簡潔でありうることである。これは、学習するパラメータがそれほど多く必要ないので、ネットワークの層にとって望ましい性質である。同じくらい便利なのは、注意プーリングの演算のやり方を変えなくても、注意が任意に大きなデータベースに対して働けるという事実である。
import torch
from d2l import torch as d2l