プリンキピア

第11章注意機構とTransformer

11.3.1. Dot Product Attention11.3.1 内積注意

Let’s review the attention function (without exponentiation) from the Gaussian kernel for a moment:
英語のヒント
ガウスカーネルからの注意の関数(指数関数をとる前のもの)を少し見直そう。
a(𝐪,𝐤i)=−12∥𝐪−𝐤i∥2=𝐪⊤𝐤i−12∥𝐤i∥2−12∥𝐪∥2.(11.3.1)
First, note that the final term depends on 𝐪 only. As such it is identical for all (𝐪,𝐤i) pairs. Normalizing the attention weights to 1, as is done in (11.1.3), ensures that this term disappears entirely. Second, note that both batch and layer normalization (to be discussed later) lead to activations that have well-bounded, and often constant, norms ∥𝐤i∥. This is the case, for instance, whenever the keys 𝐤i were generated by a layer norm. As such, we can drop it from the definition of a without any major change in the outcome.
英語のヒント
まず、最後の項は𝐪だけに依存することに注意してほしい。そのため、それはすべての(𝐪,𝐤i)の組について同じである。(11.1.3)で行うように注意の重みを1に正規化すると、この項は完全に消える。第二に、バッチ正規化と層正規化(後で論じる)はどちらも、ノルム∥𝐤i∥がよく抑えられた、しばしば一定の活性値をもたらすことに注意してほしい。たとえば、キー𝐤iが層正規化で生成された場合はいつもそうである。そのため、結果を大きく変えずに、それをaの定義から落とせる。
Last, we need to keep the order of magnitude of the arguments in the exponential function under control. Assume that all the elements of the query 𝐪∈ℝd and the key 𝐤i∈ℝd are independent and identically drawn random variables with zero mean and unit variance. The dot product between both vectors has zero mean and a variance of d. To ensure that the variance of the dot product still remains 1 regardless of vector length, we use the scaled dot product attention scoring function. That is, we rescale the dot product by 1/d. We thus arrive at the first commonly used attention function that is used, e.g., in Transformers (Vaswani et al., 2017):
英語のヒント
最後に、指数関数の引数の桁を制御下に置いておく必要がある。クエリ𝐪∈ℝdとキー𝐤i∈ℝdのすべての要素が、平均0、分散1の独立同分布の確率変数だとする。二つのベクトルの内積は、平均が0で分散がdになる。ベクトルの長さにかかわらず内積の分散が1のままであるように、スケール化内積注意のスコア関数を使う。つまり、内積を1/dでスケールし直す。こうして、たとえばTransformer(Vaswani et al., 2017)で使われる、最初のよく使われる注意の関数にたどり着く。
a(𝐪,𝐤i)=𝐪⊤𝐤i/d.(11.3.2)
Note that attention weights α still need normalizing. We can simplify this further via (11.1.3) by using the softmax operation:
英語のヒント
注意の重みαはまだ正規化する必要があることに注意してほしい。ソフトマックス演算を使えば、(11.1.3)によってこれをさらに単純にできる。
α(𝐪,𝐤i)=softmax(a(𝐪,𝐤i))=exp⁡(𝐪⊤𝐤i/d)∑j=1exp⁡(𝐪⊤𝐤j/d).(11.3.3)
As it turns out, all popular attention mechanisms use the softmax, hence we will limit ourselves to that in the remainder of this chapter.
英語のヒント
実は、人気のある注意機構はすべてソフトマックスを使うので、この章の残りではそれに限ることにする。