Recommended resources
The freely available resources this lesson will direct you to.
Introduction
In our last lesson, we explored how LSTMs and GRUs use sophisticated gating mechanisms to create an "information highway," allowing them to overcome the vanishing gradient problem and learn long-range dependencies. However, they still process information sequentially, one token at a time. This inherent sequentiality is a significant bottleneck, preventing parallelization over the time dimension and limiting performance, a constraint that is particularly relevant given your interest in high-performance systems.
This lesson introduces a paradigm shift away from sequential processing. What if a model could look at an entire sequence at once and dynamically calculate the relevance of every word to every other word? This is the core idea behind the attention mechanism. We will deconstruct its fundamental building block: scaled dot-product attention. You will learn to view this mechanism not as a sequential process, but as a highly parallelizable, differentiable query-key-value lookup system that forms the foundation of modern architectures like the Transformer.
The Problem: Context is Everything
To begin, let's build an intuition for the problem attention is designed to solve. A word's meaning is heavily influenced by its context. The challenge for a model is to selectively draw information from the surrounding words to refine the meaning of a specific word.
Attention in transformers, step-by-step | Deep Learning Chapter 6
The 3Blue1Brown channel offers a superb explanation of the conceptual problem that attention addresses. This segment will establish why context-dependent representations are necessary.
Please watch from the beginning of this section. Pay close attention to the examples of the word "mole" and the phrase "Eiffel tower." The video illustrates how a word's initial embedding needs to be updated based on the other words in the sequence to capture its true contextual meaning.
A Differentiable Dictionary: The Query, Key, Value Framework
The attention mechanism formalizes this process of contextual information retrieval using a powerful analogy: a dictionary or database lookup. In a typical lookup, you have:
- A Query: The question you're asking or the information you're looking for.
- A set of Keys: The indices or labels you can search through.
- A set of Values: The content associated with each key.
You use your query to find the single best-matching key and retrieve its corresponding value. Attention implements a "soft" version of this. Instead of a single, hard retrieval, it computes a weighted average of all values, where the weights are determined by how well the query matches each key.
To implement this in a neural network, we represent queries, keys, and values as vectors. For each input token (e.g., its embedding vector), we generate three distinct vectors:
- Query vector (): Represents what this token is looking for.
- Key vector (): Represents what information this token offers or what it's "about."
- Value vector (): Represents the actual content or information this token will communicate if attended to.
These vectors are generated by multiplying the input token's embedding by three separate, learnable weight matrices: , , and .

Let's watch a brief segment that introduces this Q, K, V concept.
Attention in transformers, step-by-step | Deep Learning Chapter 6
This next segment from the same 3Blue1Brown video introduces the query, key, and value vectors and the learnable matrices used to create them.
Watch from this section. The video explains how query and key matrices transform the initial embeddings into vectors that can be compared for relevance.
The Core Mechanism: Scaled Dot-Product Attention
Now, let's assemble these pieces into a precise mathematical and computational framework.
Step 1: Score - Measuring Similarity with the Dot Product
Given a query vector , we need a way to measure its similarity to every key vector in the sequence.
Socratic Question: From our first module on linear algebra, we know the dot product is related to the cosine of the angle between the vectors: . How can this property be used to define a continuous measure of "similarity" or "relevance" between a query and a key? What would a large positive dot product signify compared to a value near zero?
Answer
The dot product directly measures the alignment between two vectors. A large positive dot product indicates that the vectors point in similar directions (high alignment/similarity). A dot product near zero indicates they are orthogonal (no alignment/low similarity). A large negative value indicates they point in opposite directions. This makes the dot product an excellent, and computationally efficient, way to score the match between a query and a key.So, the first step is to compute the dot product of our query with every key . This gives us a set of raw affinity scores.

Step 2: Scale - Stabilizing the Gradients
The original "Attention Is All You Need" paper introduced a crucial, yet simple, modification. They scaled the dot product scores before applying the softmax function.
The reasoning is statistical. If the components of the query and key vectors are random variables with a mean of 0 and variance of 1, their dot product will have a mean of 0 but a variance of , where is the dimension of the key vectors. For large , this means the dot product scores can have very large magnitudes.
This is a problem for the subsequent softmax function. Large input values to softmax result in extremely small gradients, effectively stalling learning. To counteract this, we scale the scores by dividing by . This ensures the variance of the scores remains around 1, leading to more stable and reliable training.
11.3. Attention Scoring Functions
The "Dive into Deep Learning" textbook provides an excellent mathematical justification for this scaling factor.
Read the section titled "Dot Product Attention". Focus on the paragraph explaining why the dot product is rescaled by 1/\sqrt{d}.
Step 3: Normalize - Calculating Attention Weights with Softmax
The scaled scores are just raw numbers. To convert them into a useful distribution of weights, we apply the softmax function. This normalizes the scores so they are all positive and sum to 1. The output, , is the vector of attention weights.
Step 4: Aggregate - The Weighted Sum of Values
The final step is to compute the output of the attention layer. This is simply the weighted sum of all the value vectors , using the attention weights we just calculated.
The resulting vector is a blend of all the values in the sequence, where the values whose corresponding keys were most similar to the query are given the highest weight.
From Vectors to Matrices: Efficient Implementation
Performing these steps for each token one-by-one is inefficient. The true power of attention, especially relevant to your interest in performance, comes from its formulation using matrix operations. We can process an entire sequence of tokens at once.
If we stack our query, key, and value vectors into matrices , , and , the entire scaled dot-product attention operation can be expressed in a single, highly parallelizable equation:
- computes all dot products between all queries and all keys in one go.
- The division by is a simple scalar operation.
softmaxis applied row-wise to the resulting score matrix.- The final matrix multiplication aggregates the value vectors according to the attention weights.
This formulation maps beautifully to GPUs, enabling massive parallelization that was impossible with RNNs. Andrej Karpathy's "Let's build GPT" provides one of the best explanations available, building the entire self-attention head from first principles.
Let's build GPT: from scratch, in code, spelled out.
This video is a masterclass in building attention from scratch. Karpathy starts by developing a "mathematical trick" for weighted aggregation, which provides deep intuition for the final matrix form.
First, watch the segment from "a mathematical trick". This section is crucial. He shows how a simple averaging of past tokens can be efficiently implemented with a matrix multiplication involving a lower-triangular matrix. This builds the intuition for using matrix multiplication for weighted aggregation. Next, watch the core implementation of scaled dot-product attention. Here, he replaces the fixed averaging weights with data-dependent affinities calculated via the QK^T dot product. He explains the roles of Q, K, and V, the importance of scaling, and how it all fits into a single self-attention Head module. Finally, review his summary and additional notes on attention from this segment. He clarifies concepts like self-attention vs. cross-attention and encoder vs. decoder blocks.
Masking: Preventing the Future from Leaking
In some applications, like generating text word-by-word (an autoregressive task), we must prevent the model from "cheating" by attending to future tokens. For example, when predicting the 5th word, the model should only have access to words 1 through 4.
This is accomplished with masking. Before the softmax step, we add to the attention scores that correspond to future positions. After applying softmax, these scores become zero, effectively nullifying any contribution from future value vectors. Karpathy's video touched upon this, and the "Dive into Deep Learning" resource provides a formal explanation.
11.3. Attention Scoring Functions
This resource explains how masking is implemented to handle variable-length sequences or to prevent look-ahead in autoregressive models.
Read the section titled "Masked Softmax Operation". This will show you the practical implementation of setting certain scores to a very large negative number before the softmax.
Conclusion
In this lesson, we have defined the fundamental mechanism of scaled dot-product attention. You've moved from the sequential world of RNNs to the parallel, query-based world of attention.
Key Takeaways:
- Query-Key-Value Model: Attention functions as a "soft" database lookup, retrieving a weighted combination of Values based on the similarity between a Query and a set of Keys.
- Dot Product as Similarity: The dot product between a query and a key vector provides a computationally efficient score for their relevance or alignment.
- Scaling for Stability: Dividing the scores by is a critical step to prevent the softmax function from saturating, which would kill gradients and destabilize training.
- Matrix Formulation: The entire mechanism can be expressed as a single matrix equation, , making it highly parallelizable on modern hardware.
- Masking: In autoregressive settings, masking is applied to prevent tokens from attending to future positions in the sequence.
Preview of the Next Lesson:
We have defined the what and how of scaled dot-product attention. But what happens when the queries, keys, and values all come from the same input sequence? In our next lesson, we will explore this powerful idea, self-attention, which allows a sequence to look at itself to build rich, contextual representations of its own tokens, all in parallel. This is the mechanism that truly unlocks the power of the Transformer architecture.