プリンキピア

第11章注意機構とTransformer

11.6.1. Self-Attention11.6.1 自己注意

Given a sequence of input tokens 𝐱1,…,𝐱n where any 𝐱i∈ℝd (1≤i≤n), its self-attention outputs a sequence of the same length 𝐲1,…,𝐲n, where
英語のヒント
任意の𝐱i∈ℝd(1≤i≤n)である入力トークンの系列𝐱1,…,𝐱nが与えられると、その自己注意は同じ長さの系列𝐲1,…,𝐲nを出力する。ここで、各出力は次で与えられる。
𝐲i=f(𝐱i,(𝐱1,𝐱1),…,(𝐱n,𝐱n))∈ℝd(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, d). The output tensor has the same shape.
英語のヒント
これは(11.1.1)の注意プーリングの定義による。マルチヘッド注意を使って、以下のコード片は、(バッチサイズ、時間ステップの数あるいはトークン単位の系列の長さ、d)という形状のテンソルの自己注意を計算する。出力のテンソルも同じ形状をもつ。
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))