第11章注意機構とTransformer
11.6.2. Comparing CNNs, RNNs, and Self-Attention11.6.2 CNN、RNN、自己注意を比べる
Let’s compare architectures for mapping a sequence of tokens to another one of equal length, where each input or output token is represented by a -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).
英語のヒント
個のトークンの系列を同じ長さの別の系列に写す構造を比べよう。ここで、入力や出力の各トークンは次元のベクトルで表される。具体的には、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 -grams in text. Given a sequence of length , consider a convolutional layer whose kernel size is , and whose numbers of input and output channels are both . The computational complexity of the convolutional layer is . As Fig. 11.6.1 shows, CNNs are hierarchical, so there are sequential operations and the maximum path length is . For example, and are within the receptive field of a two-layer CNN with kernel size 3 in Fig. 11.6.1.
英語のヒント
When updating the hidden state of RNNs, multiplication of the weight matrix and the -dimensional hidden state has a computational complexity of . Since the sequence length is , the computational complexity of the recurrent layer is . According to Fig. 11.6.1, there are sequential operations that cannot be parallelized and the maximum path length is also .
英語のヒント
RNNの隠れ状態を更新するとき、の重み行列と次元の隠れ状態の掛け算の計算量はである。系列の長さはなので、再帰的な層の計算量はである。図11.6.1によれば、並列化できない逐次的な演算があり、最大の経路長もである。
In self-attention, the queries, keys, and values are all matrices. Consider the scaled dot product attention in (11.3.6), where an matrix is multiplied by a matrix, then the output matrix is multiplied by an matrix. As a result, the self-attention has a 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 sequential operations and the maximum path length is also .
英語のヒント
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乗であるため、自己注意はとても長い系列では法外に遅くなる。