第11章注意機構とTransformer
11.6.1. Self-Attention11.6.1 自己注意
Given a sequence of input tokens where any (), its self-attention outputs a sequence of the same length , where
英語のヒント
任意の()である入力トークンの系列が与えられると、その自己注意は同じ長さの系列を出力する。ここで、各出力は次で与えられる。
(11.6.1)
according to the definition of attention pooling in (11.1.1). Using multi-head attention, the following code snippet computes the self-attention of a tensor with shape (batch size, number of time steps or sequence length in tokens, ). The output tensor has the same shape.
英語のヒント
これは(11.1.1)の注意プーリングの定義による。マルチヘッド注意を使って、以下のコード片は、(バッチサイズ、時間ステップの数あるいはトークン単位の系列の長さ、)という形状のテンソルの自己注意を計算する。出力のテンソルも同じ形状をもつ。
num_hiddens, num_heads = 100, 5
attention = d2l.MultiHeadAttention(num_hiddens, num_heads, 0.5)
batch_size, num_queries, valid_lens = 2, 4, torch.tensor([3, 2])
X = torch.ones((batch_size, num_queries, num_hiddens))
d2l.check_shape(attention(X, X, X, valid_lens),
(batch_size, num_queries, num_hiddens))