Recommended resources
The freely available resources this lesson will direct you to.
- L19.4.2 Self-Attention and Scaled Dot-Product Attentionwww.youtube.com
- Why Scaling by the Square Root of Dimensions Matters in Attention | Transformers in Deep Learningwww.youtube.com
- Scaled Dot-Product Attention: The Core Transformer Mechanism ...mbrenndoerfer.com
- Why is Self Attention divided by √dₖ: The Secret Behind ...medium.com
Introduction
In our previous lesson, we established the conceptual foundation of the attention mechanism. We saw it as a powerful solution to the information bottleneck in traditional sequence-to-sequence models, enabling a model to selectively focus on relevant parts of the input at each step of output generation. This is achieved by replacing the static context vector with a dynamic one, computed as a weighted sum of all input representations.
Now, we will transition from the why to the how. This lesson delves into the mathematical core of the most prevalent attention mechanism: scaled dot-product attention. Our goal is to derive its formula step-by-step, understanding the theoretical justification for each component. By dissecting this elegant equation, you will gain a foundational understanding of the engine that powers modern Transformer architectures.
The Query, Key, and Value Framework
In our conceptual discussion, we talked about the decoder attending to the encoder's hidden states. The actual mechanism, as introduced in the "Attention Is All You Need" paper, is more general and powerful. It uses an analogy from information retrieval systems, framing attention in terms of Queries, Keys, and Values.
Instead of using raw input embeddings or hidden states directly, the model learns three distinct linear projections for each input token. This creates three new representations for each token, each with a specific role:
- Query (Q): A representation of the current token's state, essentially asking a question or "querying" the other tokens. For example, a pronoun's query might ask, "To whom or what do I refer?"
- Key (K): A representation that acts like a label or an "advertisement" for what a token contains. A noun's key might say, "I am a potential antecedent." The query will be matched against these keys.
- Value (V): A representation of the token's actual content or information. If a token is "selected" via its key, its value is what gets passed along to the output.
This separation of roles is crucial. It allows a token to advertise its relevance (Key) in a different way than it contributes its content (Value).
Mathematically, given an input matrix (where each row is a token's embedding), we generate the Q, K, and V matrices by multiplying with three distinct weight matrices, , , and , which are learned during training:
The following video provides a clear walkthrough of this initial projection step.
L19.4.2 Self-Attention and Scaled Dot-Product Attention
This video by Sebastian Raschka introduces the concept of projecting inputs into Query, Key, and Value representations.
Watch from the beginning to this point. The video first recaps the basic attention idea and then introduces the learnable weight matrices W_Q, W_K, W_V to create the Q, K, and V vectors. Pay attention to how the dot product is now computed between a query and all keys to determine attention, and how the values are used to form the final context-aware embedding.
With this Q, K, V framework established, we can now build the full attention formula.

The Four Steps of Scaled Dot-Product Attention
The computation can be broken down into four distinct, sequential operations. We will examine each one and the mathematical reasoning behind it.
Step 1: Compute Similarity Scores with the Dot Product
The first step is to measure how much each query "matches" each key. An effective and computationally efficient way to do this is with the dot product. Given the geometric interpretation of the dot product , it measures the alignment between two vectors. A large positive dot product implies the vectors point in similar directions, signaling high similarity.
We compute the dot product between every query vector and every key vector. This can be done for the entire sequence in a single, highly optimized operation: matrix multiplication.
The resulting Scores matrix contains the raw, unnormalized similarity scores. The element at position in this matrix represents the similarity between the query of the -th token and the key of the -th token.
Scaled Dot-Product Attention: The Core Transformer Mechanism ...
This article provides an excellent, in-depth explanation of each component of the formula. This first reading focuses on the choice of the dot product for measuring similarity.
Please read the section titled Dot Product Similarity. The author provides a clear justification for using the dot product over alternatives like Euclidean distance or cosine similarity and explains how the entire set of pairwise scores can be computed efficiently with the matrix multiplication QK^T.
Step 2: Scale the Scores
This step is the "scaled" in scaled dot-product attention and is critical for stable model training.

The Problem: Softmax Saturation
To understand the need for scaling, we must first consider the statistical properties of the dot product. Assume the components of your query and key vectors are independent random variables with a mean of 0 and variance of 1. The dot product of two such vectors, and , of dimension is .
From the properties of variance, the variance of this sum is the sum of the variances. Since , the variance of the dot product is:
The standard deviation is therefore . This means that as the dimension of the keys and queries increases, the variance of the dot products also increases, leading to scores with larger magnitudes.
These large scores are then fed into a softmax function (our next step). The softmax function, , is sensitive to large inputs. If one score is significantly larger than others, the softmax output will be extremely "peaky"—approaching a one-hot distribution where one token gets an attention weight near 1 and all others get weights near 0. This is called softmax saturation.
In this saturated regime, the gradients become vanishingly small for all non-maximal inputs. The model effectively stops learning, as there is no gradient signal to update the weights.
The Solution: Variance Normalization
To counteract this, the original authors proposed a simple and elegant solution: divide the scores by the standard deviation, .
This step normalizes the scores to have a variance of 1, regardless of the dimension . This keeps the inputs to the softmax function in a well-behaved range, preventing saturation and ensuring that gradients can flow properly during training.
The following resources provide a detailed statistical derivation and intuition for this crucial step.
Why is Self Attention divided by √dₖ: The Secret Behind ...
This article focuses specifically on the mathematical justification for the scaling factor.
Please read the section that starts with the heading But why is the scaling factor √dₖ?. It walks through the statistical derivation, showing that for independent random variables with mean 0 and variance 1, the variance of the dot product is d_k, and thus the standard deviation is \sqrt{d_k}.
Why Scaling by the Square Root of Dimensions Matters in Attention | Transformers in Deep Learning
This video provides an excellent intuitive and mathematical explanation for the scaling factor.
Please watch the following segments: This first part explains the core mathematical property: the variance of the matrix multiplication result is proportional to the dimension d_k. Next, this segment provides an intuitive example showing how variance increases as vector dimension grows. The third part explains why high variance is a problem, connecting it to softmax saturation and the vanishing gradient problem. Finally, the last segment provides the mathematical justification for dividing by \sqrt{d_k} to normalize the variance.
Step 3: Compute Attention Weights with Softmax
With the scores appropriately scaled, we apply a row-wise softmax function to convert them into a probability distribution.
The resulting matrix is the attention weight matrix. Each row is a probability distribution that sums to 1. The value represents the amount of attention the -th token should pay to the -th token. The softmax sharpens the distribution, assigning exponentially higher weights to tokens with higher similarity scores.
Step 4: Compute the Output as a Weighted Sum of Values
The final step is to use the attention weights to create a weighted sum of the Value vectors.
The output for the -th token is a new vector computed by summing all value vectors , weighted by their corresponding attention scores . This new vector is a context-aware representation of the token, containing information blended from across the entire sequence, precisely controlled by the learned attention weights.
The Complete Formula
By combining these four steps, we arrive at the complete formula for scaled dot-product attention:
This single, compact equation elegantly captures the entire process: measuring similarity, stabilizing the computation, converting scores to probabilistic weights, and aggregating information. Crucially, every operation within this formula—matrix multiplication, scaling, and softmax—is differentiable. This allows gradients from a downstream loss function to flow back through the entire mechanism, enabling the weight matrices to be optimized end-to-end via standard backpropagation.
Scaled Dot-Product Attention: The Core Transformer Mechanism ...
To consolidate your understanding, let's revisit the same article and tie all the pieces together.
Please read the following sections: Start with the introduction to get a high-level overview. Then, read about the scaling fix, the softmax application, and the final weighted sum. Finally, review the section The Full Attention Formula, which presents the complete equation and a concise Python implementation that mirrors the mathematical steps.
Conclusion
In this lesson, we have mathematically deconstructed scaled dot-product attention, the fundamental building block of the Transformer. We moved beyond the high-level concept of attention and derived its specific implementation, understanding the theoretical necessity of each component.
The key takeaways are:
- Q, K, V Framework: Inputs are projected into three distinct roles—Query, Key, and Value—allowing for a flexible and powerful attention mechanism.
- Similarity Scores: The dot product provides an efficient and effective measure of similarity between queries and keys.
- The Scaling Factor: Dividing by is a crucial step for numerical stability. It normalizes the variance of the dot products, preventing the softmax function from saturating and causing vanishing gradients, thus enabling the training of deep models.
- The Final Output: The attention weights, derived from the softmax of the scaled scores, are used to compute a weighted sum of the Value vectors, producing a context-enriched representation for each token.
- Differentiability: The entire mechanism is fully differentiable, allowing for end-to-end training with gradient-based optimization.
You now understand how attention is calculated for a single "head." However, a single attention mechanism might be forced to average its focus. What if we want the model to pay attention to different things simultaneously—for instance, attending to syntactic dependencies in one way and semantic relationships in another? This is the motivation for our next topic: multi-head attention.