← ALL COURSES
INTERMEDIATE / 8 CONNECTED LESSONS

Attention & transformers, built from first principles.

From your first token vector to a transformer you can train. Eight connected lessons, explicit mathematics, and code you can inspect in two libraries.

Start the course
QKᵀVSOFTMAXATTENTION WEIGHTSOUTPUT
YOUR DESTINATION: A WORKING TRANSFORMER
START WITH: FOUNDATIONS OR EQUIVALENTBUILD A SEQUENCE-TO-SEQUENCE MODELPACE: YOUR OWN

You can memorize softmax(Q @ K.T) @ V and still feel lost inside an architecture diagram. This course follows the information instead: what enters a model, how each operation changes it, and why those operations belong together.

Our destination is a small encoder–decoder transformer that learns to copy sequences. It is deliberately modest. A controlled task makes it possible to test causality, padding, shapes, and learning without downloading a dataset or hiding the model behind a high-level transformer wrapper.

What you need before starting

Begin with the four-lesson foundations course if tensor axes, matrix products, weighted means, probability, or gradients are unfamiliar. It uses the same values and typed functions we build on here. The goal is to make these operations recognizable before they appear inside an architecture.

Ready to skip ahead? You should be able to explain why a (3, 4) matrix times a (4, 2) matrix produces (3, 2), why [0.2, 0.3, 0.5] can mix three value vectors, and why a gradient update changes a parameter rather than a training target. If any answer feels uncertain, the foundations are the shortest route to a solid understanding.

Each lesson has a prerequisite, a concrete goal, a visual explanation, implementation details, and a question to check your reasoning. Work through them in order on your first pass. The contents panel jumps within a lesson; site links stay in this tab; external references open a new tab.

How to use the code

Download the complete course code bundle , unzip it, and run the commands in its README.txt. You can also open the individual NumPy forward passes , PyTorch model , training experiment , and verification checks .

bash
python -m pip install numpy torch
python numpy_core.py
python torch_core.py
python verify.py
python train_copy.py --steps 800 --seed 7

The displayed functions come directly from those files. NumPy makes the forward calculations visible. PyTorch performs the same tensor operations and provides automatic differentiation for training. Later snippets reuse functions introduced earlier; run the full files when you want all dependencies together.

The code uses B for batch size, S for source length, T for target length, D for model width, h for head count, and V for vocabulary size. Within an attention equation, the uppercase matrix V instead denotes values; the surrounding shape makes the distinction explicit.

The reading behind this course

The architectural starting point is Attention Is All You Need , especially §§3.1–3.5. We also use Bahdanau et al. for attention’s earlier encoder–decoder context, Layer Normalization , BERT , and the pre-norm analysis . Each lesson links to the sources relevant to its claims and to official API documentation where implementation conventions matter.

Our explanations, numerical examples, diagrams, and code are original teaching material. The final model uses pre-norm, one block per stack, and no dropout. The 2017 paper uses a different normalization order and training recipe. We will make that distinction explicit when we assemble the blocks.

The learning path/ 08

READ → EXPERIMENT → CHECK
01
THE REPRESENTATION

Words become vectors.

Tokens, vocabulary, embeddings, and latent space: establish what the model actually receives before asking how it pays attention.

15 MINNOT STARTED
02
THE POSITION

Give the sequence a sense of order.

Why attention needs positional information, how sinusoidal encoding works, and what changes with learned positions or rotations.

13 MINNOT STARTED
03
THE INTERACTION

Attention, from the ground up.

Derive the weighted mixture, work through actual numbers, and implement the same operation in NumPy and PyTorch.

20 MINNOT STARTED
04
THE INFORMATION FLOW

Who is allowed to see what?

Self-attention, causal masks, padding, and cross-attention are easier to understand when you trace where Q, K, and V come from.

16 MINNOT STARTED
05
THE PARALLEL VIEWS

Several heads, one shared representation.

Project, split, attend, concatenate, and mix: follow every axis through multi-head attention.

15 MINNOT STARTED
06
THE REUSABLE BLOCK

The block around attention.

Residual addition, layer normalization, and the feed-forward network: understand the boxes that architecture diagrams often leave unexplained.

18 MINNOT STARTED
07
THE COMPLETE ARCHITECTURE

Encoder, decoder, or both?

Choose an architecture by following the available information, the output you need, and the objective used to train it.

19 MINNOT STARTED
08
THE WORKING MODEL

Train it. Test what it learned.

Assemble the model, shift the targets, optimize a next-token loss, and evaluate actual generation on held-out sequences.

22 MINNOT STARTED