Repository navigation
Module 14
Level: Advanced
Status: Introductory technical chapter with supporting lessons and learning resources
This module is a self-contained learning unit within the larger Deep Learning curriculum. Its purpose is to build understanding from motivation and prerequisites through mathematical reasoning, implementation and critical evaluation.
Transformers replace recurrent sequence processing with attention and position-wise feedforward transformations. Self-attention computes interactions among tokens in parallel, while multi-head attention learns several representation subspaces. Residual connections and normalization support deep stacks.
Start with token embeddings and query, key and value projections. Derive scaled dot-product attention, masking and multi-head concatenation. Trace tensor shapes through an encoder block and decoder block, including residual and feedforward paths.
By the end of this module, a reader should be able to define its central objects, explain the motivation behind its main methods, interpret the governing equations, work through a small example, and identify assumptions and limitations. The goal is understanding the mechanism—not memorizing a list of terms.
- Derive scaled dot-product attention.
- Track dimensions across heads.
- Explain masks, residuals and feed-forward layers.
Modules 01–03, 05–09 provide the linear algebra, calculus, probability, nonlinearities and optimization background.
Useful background
Where these ideas are used next
- Module-14-Lesson-Map
- Module-14-Important-Lessons
- Module-14-Numericals-and-Exercises
- Module-14-Implementation-Roadmap
- Module-14-Research-Extensions
- Module-14-transformer-architecture-attention-mechanisms
- Module-14-Lesson-01
- Module-14-Lesson-02
- Module-14-Lesson-03
- Module-14-Lesson-04
- Module-14-Lesson-05
- Module-14-Lesson-06
- Module-14-Lesson-07
- Module-14-Lesson-08
- Module-14-Lesson-09
- Module-14-Lesson-10
- Module-14-Lesson-11
- Study the lessons in prerequisite order.
- Write a notation sheet and derive central equations.
- Solve at least one hand-worked example per major mechanism.
- Implement core operations where appropriate.
- Verify outputs and gradients.
- Complete conceptual and coding exercises.
- Record limitations and research extensions.
- Conceptual understanding
- Mathematical derivations
- Worked numerical examples
- NumPy implementation (where applicable)
- PyTorch implementation (where applicable)
- Tests and numerical verification
- Experiments and interpretation
- Revision and self-assessment
The Transformer replaces recurrent sequence transitions with attention and position-wise feedforward layers. Multi-head self-attention lets each token aggregate information from other tokens. Residual connections, normalization and positional information make the stack trainable and order-aware.
Scaled dot-product attention: $$ \operatorname{Attention}(Q,K,V)= \operatorname{softmax}\left(\frac{QK^\top}{\sqrt{d_k}}+M\right)V. $$
For one head,
With token representations X of shape (B,T,d_model), projections produce Q,K,V of shape (B,h,T,d_k), where
flowchart TD
A[Token embeddings + positions] --> B[Multi-head self-attention]
B --> C[Residual + normalization]
C --> D[Position-wise feedforward network]
D --> E[Residual + normalization]
E --> F[Next Transformer block]
Build a single attention head with explicit tensor shapes before using nn.MultiheadAttention. Test causal masking, padding masks, attention dimensions and residual shape compatibility.
Self-attention is permutation equivariant without positional information. The standard attention matrix costs O(T^2) memory and compute in sequence length. Encoder self-attention, decoder causal self-attention and cross-attention are distinct.
Study pre-LN versus post-LN, rotary embeddings, grouped-query attention, multi-query attention, FlashAttention, sparse attention and mixture-of-experts layers.
- Reproduce the smallest mathematical example by hand.
- Implement the central operation using NumPy.
- Implement the same operation using PyTorch.
- Compare outputs and gradients with numerical tolerances.
- Change one assumption or hyperparameter and document what changes.
- Record environment, random seed, data split, compute budget and limitations.
- Explain the motivation and assumptions without notes.
- Derive the principal equations and define every symbol.
- Complete at least one worked numerical example.
- Trace tensor shapes through the computation.
- Implement and test the central mechanism.
- Explain at least two failure modes.
- Read one primary paper and summarize its contribution and limitations.
Content status: This page now contains a technical chapter and a practical learning specification. The linked lesson pages provide focused subtopic treatment; experiments are not claimed complete until their result logs are added.
🏠 Home · 🗺️ Curriculum · 🧭 Learning paths · ❓ Question bank · 📖 Glossary · 📚 References · ✏️ Contributing
🏠 Home · 🗺️ Curriculum · 🧭 Learning paths
❓ Question bank · 📖 Glossary · 📚 References
Jump to module:
01 02 03 04 05 06 07 08 09 10 11 12
13 14 15 16 17 18 19 20 21 22 23 24