プリンキピア

第11章注意機構とTransformer

11.2.3. Adapting Attention Pooling11.2.3 注意プーリングを調整する

We could replace the Gaussian kernel with one of a different width. That is, we could use α(𝐪,𝐤)=exp⁡(−12σ2∥𝐪−𝐤∥2) where σ2 determines the width of the kernel. Let’s see whether this affects the outcomes.
英語のヒント
ガウスカーネルを、幅の異なるものに置き換えることもできる。つまり、σ2がカーネルの幅を決めるα(𝐪,𝐤)=exp⁡(−12σ2∥𝐪−𝐤∥2)を使える。これが結果に影響するか見てみよう。
sigmas = (0.1, 0.2, 0.5, 1)
names = ['Sigma ' + str(sigma) for sigma in sigmas]

def gaussian_with_width(sigma):
    return (lambda x: torch.exp(-x**2 / (2*sigma**2)))

kernels = [gaussian_with_width(sigma) for sigma in sigmas]
plot(x_train, y_train, x_val, y_val, kernels, names)
Clearly, the narrower the kernel, the less smooth the estimate. At the same time, it adapts better to the local variations. Let’s look at the corresponding attention weights.
英語のヒント
明らかに、カーネルが狭いほど、推定値はなめらかでなくなる。同時に、局所的な変動によりよく適応する。対応する注意の重みを見てみよう。
plot(x_train, y_train, x_val, y_val, kernels, names, attention=True)
As we would expect, the narrower the kernel, the narrower the range of large attention weights. It is also clear that picking the same width might not be ideal. In fact, Silverman (1986) proposed a heuristic that depends on the local density. Many more such “tricks” have been proposed. For instance, Norelli et al. (2022) used a similar nearest-neighbor interpolation technique for designing cross-modal image and text representations.
英語のヒント
予想どおり、カーネルが狭いほど、大きな注意の重みの範囲も狭くなる。同じ幅を選ぶのが理想的でないかもしれないことも明らかである。実際、Silverman (1986)は局所的な密度に依存する経験則を提案した。こうした「工夫」はほかにも数多く提案されてきた。たとえば、Norelli et al. (2022)は、異なる種類のデータをまたぐ画像とテキストの表現を設計するのに、似た最近傍補間の手法を使った。
The astute reader might wonder why we are providing this deep dive for a method that is over half a century old. First, it is one of the earliest precursors of modern attention mechanisms. Second, it is great for visualization. Third, and just as importantly, it demonstrates the limits of hand-crafted attention mechanisms. A much better strategy is to learn the mechanism, by learning the representations for queries and keys. This is what we will embark on in the following sections.
英語のヒント
注意深い読者は、半世紀以上前の手法をなぜこれほど深掘りするのか不思議に思うかもしれない。第一に、これは現代の注意機構の最も初期の先駆けの一つである。第二に、可視化にとても向いている。第三に、同じくらい重要なことに、手作りの注意機構の限界を示している。もっとずっとよい戦略は、クエリとキーの表現を学習することで、仕組みそのものを学習することである。以下の節では、これに取りかかる。