Implementing Self-Attention with Trainable Weights in Python
Self-attention with trainable weights keeps the basic idea of computing context vectors as weighted sums over input vectors, with only slight differences.
Why the Same Question Still Applies
Imagine you already understand the basic self-attention mechanism. For one chosen input element, it produces a context vector by taking a weighted sum over all the input vectors. Each input vector contributes to that context vector, and the contributions are combined according to weights. The version you are about to study keeps that goal. The question stays the same: for this particular input element, how do we blend the whole input together into one useful vector?
What changes is the set of ingredients that go into producing that blend. The trainable version adds trainable weight matrices to the computation. Most of what follows in this article is about why that single addition matters and what it allows the model to do.
What do you think happens?
If the rules that blend the inputs were fixed by the programmer and never changed, could the model improve the context vectors it produces from data?
Reveal answer
No, because a blend built from fixed rules cannot improve on its own
The source explains that a context vector is only as useful as the blend it represents, and a blend built from fixed rules cannot improve on its own. Making the matrices trainable gives the model a way to shift that blend as it learns from data.
What Stays the Same and What Gets Added
The source describes the difference between the two versions as slight. The basic mechanism already computed context vectors as weighted sums over inputs, and the trainable version keeps that structure. The computation pattern stays familiar. What changes is that some of its parameters are no longer fixed by the programmer. They become values the training process is allowed to adjust.
| Aspect | Basic self-attention | Trainable-weight self-attention |
|---|---|---|
| Output for one input element | A context vector built as a weighted sum over all inputs | A context vector built as a weighted sum over all inputs |
| Source of the blending parameters | Fixed by the programmer | Trainable weight matrices that the training process can adjust |
| Role of the weights during training | Not adjusted by training | Updated during model training |
| Overall computation pattern | Weighted sum over inputs | Same weighted-sum pattern, with trainable matrices added |
The trainable version keeps the basic structure and changes where some parameters come from.
The most notable difference is the introduction of weight matrices that are updated during model training. The contrast in the diagram is intentional: the trainable node is the only element that differs from the basic version, and it is the piece that lets the blend be learned rather than specified in advance.
Tracing One Context Vector Step by Step
Take a short sequence of three input elements, called first, second, and third. Suppose we want the context vector for the second element. The mechanism looks at all three input vectors, because a context vector is a weighted sum over the inputs. It is not limited to the chosen element alone. The second element is the focus, which is why we say the computation is specific to a certain input element.
Read the diagram as a flow of information rather than a sequence of code lines. The input vectors enter, the trainable weight matrices govern how they are combined, and the output is a context vector for the chosen element. The only node that carries the word trainable is the weight matrices node, which is the piece that differs from the basic version.
Tracing the Second Element's Context Vector
Given three input elements (first, second, third), describe how the context vector for the second element is produced in the trainable-weight version.
- 1Identify the focus
The chosen input element is the second element. This sets which context vector we are computing.
- 2Gather all inputs
The mechanism looks at the first, second, and third input vectors, not only the second.
- 3Pass the inputs through the trainable weight matrices
The trainable weight matrices govern how the input vectors are combined. These are the values that differ from the basic version.
- 4Combine the contributions
Each input vector contributes to the context vector, and the contributions are combined according to the weights.
- 5Obtain the context vector
The result is one context vector for the second element, built from the blend of the whole input.
The context vector for the second element is a weighted blend of all three input vectors, shaped by the trainable weight matrices.
The Job of the Trainable Weight Matrices
The source states the purpose of the weight matrices directly: they are crucial so that the attention module inside the model can learn to produce good context vectors. The word good carries the weight here. A context vector is only as useful as the blend it represents. Because the matrices are trainable, the model has a way to shift that blend as it learns from data, rather than being stuck with a blend chosen in advance.
In other words, the computation pattern stays familiar, but some of its parameters are values the training process is allowed to adjust. Each context vector still comes from a weighted sum over inputs. The trainable matrices control how that sum is shaped.
Why Training Updates the Weights
The source states that the weight matrices are updated during model training. The reason follows from their purpose: the model should learn to produce good context vectors, and a fixed blend cannot be improved by the model itself. Updating the matrices is how training changes the blend so that later forward passes produce different context vectors than earlier ones.
The loop shows the logic in order. A forward pass produces context vectors. Training adjusts the trainable weight matrices. The next forward pass then runs through the adjusted matrices, so the blend it produces can differ. This source section does not spell out the exact update rule, so the description here stays at the level of what the source states: the matrices are updated during training, and that is what lets the model learn better context vectors.
Mistakes Learners Make with Trainable Weights
Common mistake Thinking the trainable version abandons the weighted-sum idea
What it looks likeAssuming that adding trainable matrices means the context vector is no longer a blend of the inputs.
Why it failsThe source says the computation pattern stays the same. The context vector is still a weighted sum over input vectors.
Do this insteadKeep the weighted-sum structure in mind and treat the trainable matrices as the only new ingredient.
Common mistake Assuming the context vector uses only the chosen element
What it looks likeComputing the context vector for the second element using only the second input vector.
Why it failsA context vector is a weighted sum over the inputs. The second element is the focus, but the mechanism looks at all three input vectors.
Do this insteadGather contributions from every input vector, then let the weights determine how each one blends in.
Common mistake Treating the weight matrices as fixed settings chosen by the programmer
What it looks likeBelieving the matrices keep their starting values throughout the life of the model.
Why it failsThe source states the matrices are updated during model training, and that they are values the training process is allowed to adjust.
Do this insteadRemember that the trainable matrices change over training, which is what lets the model improve its context vectors.
Practice Tracing a Context Vector
Using the three input elements first, second, and third, explain in your own words which vectors feed into the context vector for the third element and what role the trainable weight matrices play. Then state one reason the matrices must be trainable rather than fixed.
Hints
- Start by identifying the chosen input element and the full set of inputs the mechanism considers.
- Follow the flow from the input vectors through the trainable weight matrices to the context vector.
- Recall the source's point that a blend built from fixed rules cannot improve on its own.
Key Takeaways
- Self-attention with trainable weights keeps the basic idea of computing a context vector as a weighted sum over all input vectors, with only slight differences.
- The most notable difference is the introduction of weight matrices that are updated during model training, which govern how the input vectors are combined.
- For one chosen input element, the mechanism considers all input vectors, passes them through the trainable weight matrices, and produces a context vector.
- The trainable matrices matter because they let the attention module learn to produce good context vectors, since a blend built from fixed rules cannot improve on its own.
- Training updates the weight matrices, so later forward passes can produce different and better blends than earlier ones.