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

BERT, by hand

This is a hands-on walkthrough of BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding, the paper by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova at Google AI Language. You type two sentences, and every number BERT 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.

If you've done Attention, by hand, you already know most of BERT. It is the Transformer's encoder with the decoder taken away, a slightly different input, and two small heads on top that give it a new job. Part 1 matches every step to the encoder you know. Part 2 is the part that's new.

Star on GitHubFound it useful? A star helps other learners find it.
Part 1

The encoder

From two sentences to one context-aware vector per token: WordPiece, masking, token + segment + position embeddings, twelve layers of attention and a GELU feed-forward network.

Enter
Part 2

Two heads, one loss

The masked LM head, the next sentence head, how their two losses are added during pre-training, and how BERT is actually used once it's trained.

Enter
BERT pre-training and fine-tuning Hand-drawn redraw of Figure 1 of the paper. Left, pre-training: a masked sentence pair goes through BERT and its outputs feed the NSP and Mask LM heads. Right, faded, fine-tuning: the same BERT with a task-specific output. The BERT box opens Part 1 and the heads open Part 2. [CLS] E[CLS] C Tok 1 E1 T1 … … … Tok N EN TN [SEP] E[SEP] T[SEP] Tok 1 E′1 T′1 … … … Tok M E′M T′M Masked Sentence AMasked Sentence B Unlabeled Sentence A and B Pair BERT 12 encoder layers · click for Part 1 NSP Mask LM Mask LM the two heads ↓ Part 2 Pre-training [CLS] E C Tok E T … … … [SEP] E T Tok E′ T′ … … … QuestionParagraph Question Answer Pair BERTsame weights, then tuned Start/End Span MNLI · NER · SQuAD Fine-tuning
Redrawn by hand from Figure 1 of Devlin et al. (2018). The BERT box is Part 1; the heads on top, and the fine-tuning side, are 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 BERT: the Transformer encoder you already know, fed two sentences at once, trained by filling in blanks and judging whether one sentence follows another. With that picture in place, the many BERT variants read as small edits to it.

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, why the paper does it, and whether it's the same as the Transformer or changed.

2

Watch the worked example on the right. It computes the step on your own sentences, 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 BERT a chatbot?

No. BERT reads; it doesn't write. Chat models such as ChatGPT keep the Transformer's decoder and predict the next word from the words on its left. BERT keeps the encoder and fills in blanks using the words on both sides. That makes it good at understanding text: classifying it, finding answers in it, turning it into vectors for search. Encoders in BERT's style still do that work today. Later models changed its training recipe (RoBERTa dropped next sentence prediction, ELECTRA replaced masking with spotting swapped tokens) but kept the picture you'll build here.

Cite the paper

First posted in October 2018 and published at NAACL 2019. Section, table and figure numbers on every page refer to the arXiv version, arXiv:1810.04805v2. Details the paper leaves out come from the authors' released code and are marked code.

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 and The Illustrated BERT, ELMo, and co. were 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.