Hello! Welcome to the final lesson in our module on Transformers.
Over the past few lessons, we've pieced together the revolutionary mechanisms that make the Transformer so powerful. We started with the core idea of attention, expanded it to multi-head attention to capture diverse relationships, and then solved the "order-blindness" problem with positional encodings.
Today, we will assemble all these parts to see the complete picture. Your learning outcome is to outline the complete architecture of a Transformer, combining self-attention, feed-forward layers, and residual connections. We will see how these components are arranged in the famous encoder-decoder structure and how information flows through the model to perform tasks like machine translation.
This lesson serves as the capstone for the module, bringing together everything we've learned into a coherent whole.
A High-Level View: The Encoder-Decoder Structure
At its core, the Transformer is an encoder-decoder model. You can think of it as a two-part system:
- The Encoder reads the entire input sentence and builds a rich, contextual understanding of it. Its job is comprehension.
- The Decoder generates the output sentence word by word, using the understanding provided by the encoder. Its job is generation.
The original paper stacked six encoders and six decoders. The output of the final encoder is passed to every decoder in the stack, providing the context needed for translation.
Let's start with a high-level visual introduction to this structure.
Jay Alammar's 'The Illustrated Transformer' is a classic resource that excels at providing visual intuition. We'll use it to see the top-down structure of the model.
Read the first two sections, 'A High-Level Look' and the part of 'Bringing The Tensors Into The Picture' that describes the stack of encoders and decoders. Focus on the diagrams showing the model as a black box, then split into encoding/decoding components, and finally as stacks of identical layers.
This gives us the blueprint. Now, let's zoom in on what happens inside each of those blocks.
Inside the Encoder Block
Each encoder block has the same structure, containing two main sub-layers. The input to an encoder (the word embeddings plus positional encodings) flows through these two layers.

-
Multi-Head Self-Attention: This is the engine we've already studied. It processes the input sequence, allowing each word to "look at" all other words in the sequence to build a context-aware representation.
-
Position-wise Feed-Forward Network (FFN): After the attention mechanism, the output for each word's position is passed through a simple, fully connected neural network. This FFN is identical for each position in the sequence (the weights are shared), but it is applied independently. Its purpose is to further process the output of the attention layer, adding more model capacity and non-linearity. The FFN usually consists of two linear layers with a ReLU activation in between.
The Glue: Residual Connections and Layer Normalization
A crucial detail we haven't discussed is that after each of the two sub-layers (attention and FFN), two operations are performed: a residual connection and layer normalization.
-
Residual Connection (or "Skip Connection"): The input to the sub-layer is added directly to the output of that sub-layer. The formula is
LayerNorm(x + Sublayer(x)). This creates a "shortcut" for the data flow. For very deep networks (like a stack of 6 encoders), this is vital. It helps prevent the vanishing gradient problem, allowing the model to train more effectively by ensuring that information and gradients from earlier layers can propagate easily to later ones. -
Layer Normalization: This step normalizes the output of the residual connection. It stabilizes the training process by ensuring that the inputs to the next layer are on a consistent scale, which helps the model learn faster and more reliably.
These two small additions are critical for successfully training deep Transformer models.
Understanding the Transformer Architecture: A Deep Dive
The article 'Understanding the Transformer Architecture' provides a very clear, structured explanation of these components.
Read the sections 'The Encoder Block: A Closer Look', 'Position-wise Feed-Forward Networks', 'Residual Connections (Skip Connections)', and 'The Complete Encoder Architecture'. This will solidify your understanding of how the FFN and Add & Norm layers complete the encoder block.
Inside the Decoder Block
The decoder's job is to generate the output sequence. It has a similar structure to the encoder but with one key addition. Each decoder block has three sub-layers:
-
Masked Multi-Head Self-Attention: The decoder also uses self-attention to build context from the words it has already generated. The crucial difference is the "mask." During training, this mask prevents the model from "cheating" by looking at future words in the target sequence. This enforces the auto-regressive property—generating the next word based only on the previous ones.
-
Encoder-Decoder Attention (or "Cross-Attention"): This is where the encoder and decoder connect. This layer works just like multi-head attention, but with a twist:
- The Query (Q) vectors come from the output of the previous decoder layer (the masked self-attention).
- The Key (K) and Value (V) vectors come from the final output of the encoder stack.
This allows the decoder, at every step, to focus on the most relevant parts of the input sentence to help it generate the next word in the output sentence.
-
Position-wise Feed-Forward Network: This is identical in function to the FFN in the encoder.
Just like in the encoder, each of these three sub-layers has a residual connection and layer normalization around it.
The Final Output
After the input passes through the entire decoder stack, a final vector is produced. How does this vector become a word?
-
Linear Layer: The final vector is passed through a fully connected linear layer that projects it into a very large vector, called the logits vector. The size of this vector is equal to the size of your entire vocabulary (e.g., 30,000 words). Each element in the logits vector corresponds to a score for a specific word.
-
Softmax Layer: A softmax function is applied to the logits vector. This converts all the scores into probabilities that sum to 1.0. The word with the highest probability is then chosen as the output for that time step.
This process repeats, with the newly generated word being fed back into the decoder, until a special <end-of-sentence> token is produced.
Test your understanding!
The Transformer architecture uses three distinct multi-head attention mechanisms. For each of the following, describe where its Query (Q), Key (K), and Value (V) vectors come from:
- Encoder Self-Attention
- Decoder Masked Self-Attention
- Encoder-Decoder (Cross) Attention
Show answer
- Encoder Self-Attention: Q, K, and V all come from the output of the previous encoder layer. This allows the encoder to build context within the input sequence.
- Decoder Masked Self-Attention: Q, K, and V all come from the output of the previous decoder layer. This allows the decoder to build context within the output sequence it has generated so far.
- Encoder-Decoder (Cross) Attention: Q comes from the previous decoder layer, while K and V both come from the final output of the encoder stack. This is how the decoder looks at the input sentence for context.
A Complete Walkthrough
We've covered all the individual pieces. Now, let's watch a video that puts it all together in a continuous narrative, building the Transformer from the ground up with a single, clear example.
Transformer Neural Networks, ChatGPT's foundation, Clearly Explained!!!
The StatQuest video 'Transformer Neural Networks, ChatGPT's foundation, Clearly Explained!!!' provides a fantastic step-by-step walkthrough of the entire process, from input embeddings to the final translated output.
Watch the video from the self-attention part onwards (starting at 12:59). This will serve as a comprehensive review and help you see how all the components interact in a fluid sequence: 12:59 - 23:34: Encoder self-attention and residual connections. 23:34 - 33:34: The decoder, including masked self-attention (implied), encoder-decoder attention, and the final output layers. 33:34 - 35:47: A final summary of the main components and a mention of the feed-forward networks (which the video simplifies but we've covered).
The following diagram also provides an excellent summary of how the components are built upon each other.

Conclusion and Course Wrap-up
Congratulations on completing the final lesson of the course! Today, we've assembled the full Transformer architecture, seeing how its various components work in concert.
Key Takeaways from this Lesson:
- Overall Structure: The Transformer uses an encoder-decoder architecture, typically with a stack of identical blocks for each part.
- Encoder Block: Each encoder uses multi-head self-attention to understand the input sequence and a feed-forward network for further processing.
- Decoder Block: Each decoder uses masked self-attention to handle the output sequence, encoder-decoder attention to consult the input sequence, and a feed-forward network.
- Critical Components: Residual connections and layer normalization are essential for enabling the training of these deep, complex models.
- Final Output: A final linear layer followed by a softmax function converts the decoder's output into word probabilities.
Throughout this course, we have journeyed from the mathematical foundations of derivatives and vectors, through the core principles of probability and statistical inference, and on to building, optimizing, and evaluating a wide range of machine learning models. We started with linear regression and have now arrived at the Transformer, the architecture that powers so much of modern AI.
You set out with the goal of understanding the "why" behind the models you use, and you now have the foundational knowledge to do just that. You can explain how a gradient is used to optimize a loss function, why regularization is needed to prevent overfitting, and how attention allows a model to weigh the importance of different inputs.
This is a significant achievement. The field of AI is constantly evolving, but the statistical and mathematical principles you've learned here will remain relevant. I encourage you to continue building on this foundation, applying these concepts to new problems and exploring the exciting new research that emerges every day. Well done