Scaled Dot-Product Attention › Weighted Sum of Values
Language AI HandbookAll chapters Scaled Dot-Product Attention › Weighted Sum of ValuesOne section · Sections in this chapter5 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The final step uses the attention weights to aggregate the value vectors. For each query position , we compute a weighted combination of all value vectors. This step produces the enriched representations: it takes information from all positions in the sequence and blends it together, with the blend controlled by the attention weights we just computed.
To understand why this design makes sense, consider the alternative: using the keys directly for the output instead of having a separate value projection. If we used keys as values, the same representation would serve two incompatible purposes: advertising relevance (key role) and contributing information (value role). Keeping them separate gives the model the freedom to have token 's key say "I am relevant to syntactic subject queries" while its value says "here is the rich semantic content I contribute when selected." This decoupling is what allows attention heads to specialize in different linguistic functions.
For each query position , the output is:
where:
- : the attention weight from position to position , giving the fraction of position 's value to include
- : the value vector for position , containing the information that position contributes when selected
- : the attention output for position , a -dimensional context-enriched vector
In matrix form across all positions simultaneously:
where:
- : the attention weight matrix, rows are probability distributions over positions
- : the value matrix, rows are value vectors for each position
- : the output matrix, rows are attention outputs for each position
The output for each position is a soft lookup: a blended summary of information from all positions, weighted by relevance. Positions that received high attention weights contribute most; positions with low weights contribute little. The output has shape , matching the input shape (with instead of the original embedding dimension ). This means each token gets a new representation of the same dimension, enriched by context from the whole sequence.
Why is this better than a hard lookup (selecting the single most relevant position)? Because language rarely has crisp, discrete dependencies. Understanding "it" in "The cat sat on the mat and it purred" requires integrating information from multiple positions: "cat" is most relevant, but the verb tense and surrounding context also contribute. The soft weighted sum captures this gracefully. It is also differentiable everywhere, which means gradients flow smoothly through the output back to both the value vectors and the attention weights, allowing end-to-end training to optimize all components jointly.
# Compute the attention output
output = A @ V # (n, d_v)Output shape: (5, 4) Attention output matrix (each row is one token's enriched representation): [[-0.0954 -0.66 0.6243 -0.1932] [-0.246 -0.6701 0.1977 0.2133] [-0.2605 -0.6365 0.1641 0.26 ] [-0.1539 -0.6517 0.4929 -0.0397] [-0.1038 -0.7851 0.2428 -0.0734]]
Each row of the output is a -dimensional vector: a context-aware representation of that token, blended from value vectors according to attention weights. Tokens that attended broadly have outputs that are smooth blends of many values; tokens that attended sharply to a few positions have outputs dominated by those positions' values. After this step, each token's representation contains information from the entire sequence, weighted by learned relevance, which is precisely what makes attention powerful for understanding language.