Let's break it down · An interactive notebook on Vaswani et al. (2017)

Attention, by hand

This is a hands-on walkthrough of the Transformer from Attention Is All You Need, the 2017 paper by researchers at Google Brain, Google Research and the University of Toronto. You type a sentence, and every number the model computes is worked out in front of you, step by step, with the matrix shapes written next to it. Then you do the same sums yourself.

The model has two halves. We go through them one at a time, starting with the encoder.

Part 1

The encoder

From your sentence to one context-aware vector per word: tokens, embeddings, positional encoding, multi-head attention, add & norm, feed-forward, six layers deep.

Enter
Part 2

The decoder

Masked self-attention, encoder–decoder attention and how the model produces a translation one word at a time.

Enter
The Transformer architecture Hand-drawn redraw of Figure 1 of the paper. The encoder stack on the left and the decoder stack on the right each open their part of the notebook. N× Inputs Input Embedding Positional Encoding Multi-Head Attention Add & Norm Feed Forward Add & Norm click the encoder ↓ or the decoder ↑ N× Outputs (shifted right) Output Embedding Masked Multi-Head Attention Add & Norm Multi-Head Attention Add & Norm Feed Forward Add & Norm Linear Softmax Output Probabilities
Redrawn by hand from Figure 1 of Vaswani et al. (2017). The encoder (left) is Part 1 and the decoder (right) is Part 2.

Who this is for

For people who like to learn architectures from scratch

This notebook is for anyone who enjoys pulling a model apart and rebuilding it with a pencil. You don't need to have trained a neural network before. You only need to be comfortable multiplying and adding numbers, and to be curious about why each step is there.

The goal is simple. Once you've walked through the interactive steps and done the homework on each one, you should carry a clear mental image of how the Transformer works, from a sentence going in to the vectors coming out. With that picture in place, newer variants stop looking like new architectures. They read as small, understandable edits to this one.

Building this tutorial is how I learn. I hope it nudges you to do the same: take the pieces, imagine the forward pass your own way, and make an even more vivid version of it.

How the notebook works

1

Read the note on the left of each step. It says what happens and why the paper does it.

2

Watch the worked example on the right. It computes the step on your own sentence, one number at a time.

3

Do the homework in the “Your turn” sheet under it. Fill in a matrix entry or a shape yourself, then check it. On a phone the sheet opens as a pop-up so the walkthrough isn't interrupted.

4

Track the shapes in the bar at the top. It shows the size of the matrix after every step, so you can see where it grows, shrinks and stays the same.

Is this how ChatGPT works?

Not exactly. Most of today's large language models keep only the decoder half, and they change details such as how positions are encoded and where normalization happens. But the building blocks you'll see here (attention, multiple heads, residual connections, layer norm and the feed-forward network) are still at the heart of every one of them. Once these click, any newer architecture reads like a set of small edits to this one.

Cite the paper

Section and table numbers on every page refer to the arXiv version, arXiv:1706.03762.

Thank you

Andrej Karpathy, whose Neural Networks: Zero to Hero series, and especially Let's build GPT: from scratch, in code, spelled out, inspired this project.

Jay Alammar, whose The Illustrated Transformer was foundational to how I first pictured all of this.

This notebook is my own visualisation of how I understood the paper and the mental framework I built around it.