Hello! Welcome to the next lesson in our journey through Modern Sequence Modeling with Transformers.
In our last lesson, we identified a critical weakness in the traditional encoder-decoder architecture: the information bottleneck. We saw how forcing the encoder to compress an entire input sequence into a single, fixed-size context vector leads to information loss and a drop in performance, especially for long sequences. This limitation set the stage for a revolutionary new idea.
Today, we'll explore that idea. Your learning outcome is to explain the concept of attention as a mechanism to dynamically focus on relevant input parts. This is one of the most important concepts in modern machine learning, as it's the core engine that powers Transformer models like GPT.
1. The Core Idea: Overcoming the Bottleneck
The problem with the old model was its memory. It had to read an entire sentence and condense it into one summary vector before it could start translating. A human translator doesn't work this way. When translating, they might focus on the subject of the sentence first, then the verb, then the object, constantly looking back at different parts of the original text as needed.
The attention mechanism gives our model this same ability. Instead of relying on a single, static context vector, attention allows the decoder to look at the entire input sequence at every single step of the output generation process and decide which parts are most important right now.
Let's start with a short video that introduces this solution conceptually.
Attention for Neural Networks, Clearly Explained!!!
In the last lesson, we saw how the fixed-size context vector was a problem. This clip from StatQuest with Josh Starmer picks up right there and introduces attention as the solution: adding direct pathways from the decoder back to every part of the encoded input.
Watch from 01:10 to 03:56 and then from 04:37 to 05:30. Focus on the main idea that attention adds 'a bunch of new paths' so the decoder can 'directly access input values' at each step, overcoming the single-vector bottleneck.
As the video explained, the central idea is to create a direct connection between each output step and all the input steps. This way, if the model is translating a long sentence, it doesn't have to rely on a faded memory of the first word. It can simply "look back" and pay attention to it directly.
2. Why Contextual Focus Matters
Before we dive into the mechanics, let's solidify our intuition about why this dynamic focus is so powerful. A word's meaning is often determined by the words around it. The initial embedding for a word is generic, but its true meaning is contextual.
Consider these sentences:
- "A bug was found in the code." (An error)
- "A ladybug landed on the flower." (An insect)
The word "bug" has different meanings. An attention mechanism helps the model refine the generic "bug" vector by paying attention to surrounding words like "code" or "ladybug" to derive the correct contextual meaning.
Attention in transformers, step-by-step | Deep Learning Chapter 6
This video from 3Blue1Brown provides a brilliant illustration of why this contextual refinement is so important. It uses the example of the word 'mole' to show how context changes meaning.
Watch from 01:32 to 04:35. Pay attention to how the meaning of 'mole' or 'tower' changes based on context, and how attention's goal is to update the initial word vector to a more specific, contextually-aware one.
This ability to dynamically update a word's representation based on its relationship with other words in the sequence is what makes attention so effective. It's not just about overcoming the memory bottleneck; it's about building a richer, more nuanced understanding of the input data.
3. The Mechanics of Attention: A Fuzzy Dictionary
So, how does the model decide what to "pay attention" to? The mechanism is often described using an analogy of a dictionary or a key-value database, but one that is "fuzzy" and learnable.
Let's break down the three key components: Queries, Keys, and Values.
- Query (Q): Represents the current interest. It's a question from the decoder, asking, "Given the word I'm about to produce, what information from the input should I focus on?"
- Key (K): Represents a label or an "advertisement" for a piece of information. Each word in the input sequence has a key, which says, "This is the type of information I hold."
- Value (V): Represents the actual content or substance of the information. Each word in the input sequence also has a value, which is the information that will be passed on if its key matches the query.
The process works like this: The Query from the current decoder state is compared against all the Keys from the encoder states. The more a Key matches the Query, the more "attention" we pay to its corresponding Value. The final output for this step is a weighted sum of all the Values, where the weights are determined by the query-key similarities.
This reading develops this idea starting from a simple dictionary lookup.
This blog post, 'An Intuition for Attention', provides a great step-by-step build-up of the attention concept. It starts with a simple dictionary and gradually introduces the ideas of weighted sums and vector representations.
Please read the first two main sections: 'A Fuzzy Dictionary' and 'Word Vectors and Similarity'. Focus on the core idea of moving from an exact key-match to a 'weighted sum' of values based on semantic similarity.
This concept of a "fuzzy" lookup where a query can match multiple keys to different degrees is the essence of attention. It allows the model to blend information from multiple sources.
The following image provides a high-level visual summary of this flow.

4. The Calculation Steps
Let's get a bit more concrete. The process of calculating attention for one output step can be broken down into three main stages.
Stage 1: Calculate Similarity Scores
To determine how much attention to pay to each input word, we first need to score the similarity between the decoder's current Query and each of the encoder's Keys. The most common way to do this is with the dot product. Since queries and keys are vectors, their dot product gives us a simple, effective measure of their similarity. A large positive dot product means high similarity.
Stage 2: Normalize Scores into Weights (Softmax)
The raw dot product scores can be any real number. To turn them into something more useful, like percentages, we apply a softmax function. The softmax function takes all the scores and converts them into a set of positive numbers that all add up to 1. These are our attention weights. A weight of 0.7 for the third input word means that for the current output step, the model will pay 70% of its attention to that word.
Stage 3: Compute the Weighted Sum of Values
Finally, we use these attention weights to create a weighted sum of the Value vectors. Each Value vector (from the encoder) is multiplied by its corresponding attention weight. The results are then summed up to produce a single vector. This final vector is the context vector for the current time step. It's a custom-built vector, dynamically created by focusing on the most relevant parts of the input.
Let's watch a clip that walks through these calculation steps with a clear example.
Attention for Neural Networks, Clearly Explained!!!
The StatQuest video we saw earlier also does a great job of explaining the calculation. It walks through the dot product, the softmax, and the final weighted sum in a very accessible way.
Watch the segment from 06:29 to 13:28. This section details how similarity scores are calculated (using dot product), how softmax turns them into weights, and how these weights are used to scale the encoded inputs to create the final attention values.
The image below contrasts this "global attention" mechanism, where the output connects to all inputs, with older methods like convolutions that only look at a fixed local window.

Test your understanding!
Imagine a model is translating the English sentence "The cat sat on the mat" to French. When generating the French word "chat" (cat), which English word do you think would receive the highest attention weight? What about when generating the French word "tapis" (mat)? Explain your reasoning in terms of the attention mechanism.
Show answer
When generating "chat", the English word "cat" would receive the highest attention weight. The query from the decoder, representing its intent to produce a noun that is the subject of the sentence, would have the highest similarity (dot product) with the key of the word "cat".
Similarly, when generating "tapis", the English word "mat" would receive the highest attention weight. The decoder's query would now be focused on finding the object of the preposition "on", and its query vector would align most closely with the key vector of "mat". This demonstrates how attention dynamically shifts focus based on the part of the output being generated.
Conclusion
Today, we've unpacked the core concept of the attention mechanism, the innovation that solved the information bottleneck and revolutionized sequence modeling.
Key Takeaways:
- Dynamic Focus: Attention allows a model to dynamically look at all parts of an input sequence and decide which are most relevant at each step of generating an output.
- Contextual Understanding: This mechanism helps the model build a richer, contextual understanding of words by allowing information to flow between different parts of the sequence.
- The Q, K, V Framework: Attention works by using Queries (what am I looking for?), Keys (what information do I have?), and Values (what is my actual content?).
- The Calculation Process: The mechanism calculates similarity scores between queries and keys (often via dot product), normalizes them into attention weights using softmax, and computes a weighted sum of the Values to create a dynamic context vector.
Preview of the Next Lesson:
We've established the intuition and the conceptual framework for attention. In the next lesson, we will formalize this process. Your learning outcome will be to calculate attention scores using the query, key, and value formulation. We will look at the specific matrix operations that make this process computationally efficient and see the famous "scaled dot-product attention" equation from the original "Attention Is All You Need" paper.