プリンキピア

第11章注意機構とTransformer

11.6.2. Comparing CNNs, RNNs, and Self-Attention11.6.2 CNN、RNN、自己注意を比べる

Let’s compare architectures for mapping a sequence of n tokens to another one of equal length, where each input or output token is represented by a d-dimensional vector. Specifically, we will consider CNNs, RNNs, and self-attention. We will compare their computational complexity, sequential operations, and maximum path lengths. Note that sequential operations prevent parallel computation, while a shorter path between any combination of sequence positions makes it easier to learn long-range dependencies within the sequence (Hochreiter et al., 2001).
英語のヒント
n個のトークンの系列を同じ長さの別の系列に写す構造を比べよう。ここで、入力や出力の各トークンはd次元のベクトルで表される。具体的には、CNN、RNN、自己注意を考える。それらの計算量、逐次的な演算、最大の経路長を比べる。逐次的な演算は並列計算を妨げ、一方、系列の位置のどの組み合わせのあいだでも経路が短いほど、系列の中の長距離の依存関係を学習しやすくなることに注意してほしい(Hochreiter et al., 2001)。
Fig. 11.6.1図11.6.1
Comparing CNN (padding tokens are omitted), RNN, and self-attention architectures.
英語のヒント
CNN(詰め物のトークンは省略)、RNN、自己注意の構造の比較。
Let’s regard any text sequence as a “one-dimensional image”. Similarly, one-dimensional CNNs can process local features such as n-grams in text. Given a sequence of length n, consider a convolutional layer whose kernel size is k, and whose numbers of input and output channels are both d. The computational complexity of the convolutional layer is 𝒪(knd2). As Fig. 11.6.1 shows, CNNs are hierarchical, so there are 𝒪(1) sequential operations and the maximum path length is 𝒪(n/k). For example, 𝐱1 and 𝐱5 are within the receptive field of a two-layer CNN with kernel size 3 in Fig. 11.6.1.
英語のヒント
どんなテキストの系列も「1次元の画像」とみなそう。同じように、1次元のCNNは、テキストのnグラムのような局所的な特徴量を処理できる。長さnの系列が与えられたとき、カーネルの大きさがkで、入力と出力のチャネルの数がどちらもdの畳み込み層を考えよう。畳み込み層の計算量は𝒪(knd2)である。図11.6.1が示すように、CNNは階層的なので、逐次的な演算は𝒪(1)で、最大の経路長は𝒪(n/k)である。たとえば、図11.6.1では、𝐱1と𝐱5は、カーネルの大きさが3の2層のCNNの受容野の中にある。
When updating the hidden state of RNNs, multiplication of the d×d weight matrix and the d-dimensional hidden state has a computational complexity of 𝒪(d2). Since the sequence length is n, the computational complexity of the recurrent layer is 𝒪(nd2). According to Fig. 11.6.1, there are 𝒪(n) sequential operations that cannot be parallelized and the maximum path length is also 𝒪(n).
英語のヒント
RNNの隠れ状態を更新するとき、d×dの重み行列とd次元の隠れ状態の掛け算の計算量は𝒪(d2)である。系列の長さはnなので、再帰的な層の計算量は𝒪(nd2)である。図11.6.1によれば、並列化できない逐次的な演算が𝒪(n)あり、最大の経路長も𝒪(n)である。
In self-attention, the queries, keys, and values are all n×d matrices. Consider the scaled dot product attention in (11.3.6), where an n×d matrix is multiplied by a d×n matrix, then the output n×n matrix is multiplied by an n×d matrix. As a result, the self-attention has a 𝒪(n2d) computational complexity. As we can see from Fig. 11.6.1, each token is directly connected to any other token via self-attention. Therefore, computation can be parallel with 𝒪(1) sequential operations and the maximum path length is also 𝒪(1).
英語のヒント
自己注意では、クエリ、キー、バリューはすべてn×dの行列である。(11.3.6)のスケール化内積注意を考えよう。そこでは、n×dの行列にd×nの行列が掛けられ、その後、出力のn×nの行列にn×dの行列が掛けられる。その結果、自己注意の計算量は𝒪(n2d)になる。図11.6.1からわかるように、各トークンは自己注意を通じてほかのどのトークンとも直接つながっている。したがって、計算は𝒪(1)の逐次的な演算で並列にでき、最大の経路長も𝒪(1)である。
All in all, both CNNs and self-attention enjoy parallel computation and self-attention has the shortest maximum path length. However, the quadratic computational complexity with respect to the sequence length makes self-attention prohibitively slow for very long sequences.
英語のヒント
全体として、CNNと自己注意はどちらも並列計算の恩恵を受け、自己注意は最大の経路長が最も短い。しかし、系列の長さについて計算量が2乗であるため、自己注意はとても長い系列では法外に遅くなる。