The biggest difference with trainable weights compared with the previous version is that the weight matrices are updated during model training.
First: Compute Attention Weights

CleanShot 2024-10-23 at 8 .04.16@2x.png
In the first step of self-attention with trainable weight matrices, we compute query (q), key (k), and value (v) vectors for input element x. As in the previous section, we designate the second input as the query input. The query vector is obtained by matrix multiplication between the input and the weight matrix . Similarly, we obtain the key and value vectors through matrix multiplication involving the weight matrices and .
Although our immediate goal is only to compute one context vector , we still need the key and value vectors for all input elements, because they are used to compute the attention weights relative to .
Weight Parameters vs. Attention Weights
In a weight matrix W, the word “weight” is short for “weight parameter” and refers to values in a neural network that are optimized during training. This is not the same as attention weights. As we saw earlier, attention weights determine how much the context vector depends on different parts of the input—that is, how much the network attends to different parts of the input.
In short, weight parameters are the basic learned coefficients that define the network’s connections, while attention weights are dynamic, context-specific values.
Second: Compute Attention Scores

CleanShot 2024-10-23 at 8 .15.34@2x.png
The new feature here is that we do not compute dot products directly between input elements; instead, we use the queries and keys obtained after transforming the inputs with the corresponding weight matrices.
keys_2 = keys[1] #1 第二个元素
attn_score_22 = query_2.dot(keys_2)
print(attn_score_22) # w22
Compute all attention scores for the second element
attn_scores_2 = query_2 @ keys.T #1
print(attn_scores_2)
The attention scores are as follows
tensor([1.2705, 1.8524, 1.8111, 1.0795, 0.5577, 1.5440])
Third: Normalize Attention Scores

CleanShot 2024-10-23 at 8 .22.21@2x.png
The final attention weights are as follows
tensor([0.1500, 0.2264, 0.2199, 0.1311, 0.0906, 0.1820])
Fourth: Compute the Context Vector

CleanShot 2024-10-28 at 11 .19.26@2x.png
Now the context vector is computed as a weighted sum of the value vectors. Here, the attention weights act as weighting factors that balance the relative importance of each value vector.
context_vec_2 = attn_weights_2 @ values
print(context_vec_2)
So far, we have computed only . Next, we need to generate all context vectors from through .
Query, Key, and Value
In the context of attention mechanisms, the terms “key,” “query,” and “value” are borrowed from information retrieval and databases, where similar concepts are used to store, search for, and retrieve information.
A query is like a search query in a database. It represents the current item the model is focusing on or trying to understand (for example, a word or token in a sentence). The query is used to probe other parts of the input sequence to determine how much attention to give them.
A key is like a database key used for indexing and searching. In an attention mechanism, each item in the input sequence (for example, each word in a sentence) has an associated key. These keys are used to match against queries.
In this context, a value is like the value in a database key-value pair. It represents the actual content or representation of an input item. Once the model determines which keys—and therefore which parts of the input—are most relevant to the query (the item currently in focus), it retrieves the corresponding values.