Chapter 5 of 5

BERT

Everything so far has been attention in the abstract. This chapter opens a real checkpoint, bert-base-uncased, and shows you all 144 of its attention heads at once. Most of them are doing something dull. A few are doing something sharp. Finding out which is which is the work.

About 14 minutesNotebook Part 4

The idea in plain words

BERT is a stack of the block from the last chapter, twelve of them, each with twelve attention heads. Twelve times twelve is 144 heads, and every one of them is looking at the whole sentence at once, in both directions. That last part is the name: bidirectional encoder representations from transformers.

It was trained by hiding words and making the model guess them. No labels, no annotation, just a great deal of text with holes punched in it. The surprise of 2018 was how much that one exercise teaches a model about grammar, about which word a pronoun refers to, and about the difference between a river bank and a bank that holds money.

The counts are measured, not quoted: the pipeline loads the real checkpoint and finds 109,482,240 parameters in bert-base-uncased. What follows lets you look at what those parameters learned to do.

All 144 heads

Open a real checkpoint and look at every head it has, one sentence at a time.

Run this yourself: notebook Part 4

1 means a head that spreads evenly over every token. Near 0 means a head that picks one.

View

Loading the weights for this sentence.

What to notice

  • Start on the first sentence with the grid coloured by the marked pair. One square is far darker than the rest. That is layer 3, head 10, and it puts 0.87 of what the word "it" has to spend onto "animal". The notebook found it by searching all 144, which is the honest way to find it.
  • Now keep that head and change the sentence. On the trophy and the students it is the strongest of all 144 again, and on the teacher it is second. On the committee it is 36th, with 0.09, while layer 8 head 10 takes it with 0.65. One head is not the coreference head, even when it looks like one twice running.
  • Colour by the previous token. The clearest is layer 3, head 5, which puts 0.65 of every row one step back, and five others manage more than 0.4. That is six heads out of 144 doing a job you could describe in four words, which is roughly how many turn out to have a describable job at all.
  • Try the delimiters. 69 of the 144 heads put more than half of their weight on [CLS] and [SEP], and layer 1 head 6 puts 0.96 there. A row that empties itself onto the two tokens BERT added is a row that has found nothing it wants, which is the usual reading of these.
  • Colour by spread and look at the range rather than the pattern. The flattest head sits at 0.96, the sharpest at 0.006, and the middle of the 144 is 0.55. Heads are not all the same kind of thing, and most of them are somewhere unremarkable in between.
Go deeper: the equations, the shapes and the code

What you are looking at is one tensor per sentence of shape 12 by 12 by n by n, where n is the token count including the [CLS] and [SEP] that BERT adds. Each row of each head is a softmax, so it sums to 1: a row is one token spreading a fixed budget over the tokens it can see. Reading down a column is a different question, how much a token was attended to, and a column sums to nothing in particular.

The weights on disk are quantised to 16 bits over the unit interval, which costs about eight parts in a million. The unit tests recompute the entropy of all 144 heads in the browser from the decoded bytes and require it to match what Python measured, because a wrong decode does not look wrong on screen, it just looks like a different model.

On the claim that layer 3, head 10 is "the coreference head". Across the five sentences here that mark a pronoun and its noun, it is the strongest of the 144 on three, second on one, and 36th on the fifth. So it is a head that often does this, on sentences shaped like these, and calling it the coreference head is a convenience rather than a finding. Attention is also not the only path information takes through the model: residual connections carry a token's own vector forward whatever the attention does, so a head showing no weight is not a head showing no influence.

Source: bert-base-uncased. Generated 2026-12-05 by pipeline/bert_attention.py.5.4 KB, checksum 461600e0b91d.

How the training data is made

Run BERT's own masking rule over a sentence of your own and see what it would have been asked.

Run this yourself: notebook Part 5

, ,

15%, which is what BERT uses

Draw

Seed 42. The same seed always gives this exact answer.

What the model is shown

[CLS]theanimal[MASK]notcrossthestreetbecauseitwastoo[MASK].[SEP]

2 of 13 tokens selected, at 15%. [CLS] and [SEP] are never candidates, so they are not in that count.

What it has to predict

PositionShownTreatmentAnswer
3[MASK]replaced with [MASK]did
12[MASK]replaced with [MASK]tired

The answer is always the original word, including on the rows where nothing visible changed. That is the point of the last 10%: the model cannot tell from the input alone which positions it is being asked about, so it has to keep a usable representation of every token.

The 80, 10, 10 split

[MASK]
79.5%

aiming at 80%

Random word
9.8%

aiming at 10%

Unchanged
10.7%

aiming at 10%

Measured over 4,000 selections from 2,000 draws on your sentence, because one draw cannot show a split: with 2 tokens selected the only shares available in a single run are coarse ones.

Loading the word list for the random replacements.

What to notice

  • Press "try another draw" a few times without changing anything. The selected words move, but how many are selected never does. The 15% is a count taken from a shuffle, not a coin flipped at each token, so a sentence of this length always gets the same number of holes.
  • Watch the [CLS] and [SEP] tokens through every draw. They are never picked. A model that spent part of its training predicting the two tokens that appear in every single input would be learning nothing from that part.
  • Keep drawing until a row says the word was left as it is. Nothing changed in the input, and the model is still asked for that word. That is the 10% that stops it from only bothering to understand positions that obviously have a hole in them.
  • The random replacement is the other 10%, and it is why the model cannot simply trust the word in front of it. Some of the words it reads during training are wrong.
  • Drag the share up to 50%. The sentence stops having enough left to go on, which is the practical argument for keeping the number low.
Go deeper: the equations, the shapes and the code

The recipe here follows BERT's own pretraining data script rather than the one line summary of it. That script shuffles the candidate positions and takes the first round(0.15 times n) of them, then rolls once per selected token against the 80, 10, 10 split. Doing it the other way round, deciding the treatments first and then placing them, gives the same long run shares and a different answer for any single sentence.

Why hide words at all. A model that reads left to right can be trained to predict the next word, and the text supplies the answer for free. An encoder reads in both directions at once, so next word prediction is trivial: the answer is already in the input. Hiding a word in the middle puts the answer back out of reach while leaving the context on both sides, which is the whole trick, and the reason the paper calls it a cloze task rather than language modelling.

Two things on this panel are approximations, and the panel says so in its own footer rather than here: the sentence is split on spaces and punctuation rather than into WordPiece pieces, and the random replacements come from the GloVe word list this site already ships rather than from BERT's 30,522 entry vocabulary. Both are honest stand ins for parts that belong to chapter 2.

Computed in your browser while you watch, by src/lib/masking.ts. No model is involved and no number is stored.

One honest limit: the sentence is split on spaces and punctuation, which is the first half of what BERT does. The WordPiece step that breaks a rare word into pieces such as work ##shop needs the 30,522 entry vocabulary, which this site does not ship yet.

Guess what it will say

Hide a word, write down what you expect, then see what the model actually predicts.

Run this yourself: notebook Part 5

Loading the predictions.

What to notice

  • Write a guess before you reveal, even a bad one. The whole value of this explorer is the gap between what you expected and what came back, and that gap disappears the moment you have seen the answer.
  • Start with the capital of India. The model ranks Mumbai above Delhi, at 0.177 against 0.153. It is not looking anything up. It is completing a pattern, and Mumbai appears in more sentences shaped like this one.
  • Now try the three grammar prompts. "Neither the teacher nor the students [MASK] ready" gives "were" at 0.879. The model is far more certain about agreement than about facts, which is a fair summary of what masked pretraining actually teaches.
  • Watch the line under the bars that says what the five add up to. On the workshop prompt they hold 0.381 between them, so most of the probability is spread across tokens you will never see. Five bars can look like the whole answer when they are a fifth of it.
  • Then the two job probes, side by side with the comparison picker. The same sentence with one word changed gives carpenter, waiter, barber on one side and nurse, waitress, maid on the other. Nothing about that is a bug being reported: it is the pretraining text, measured.
Go deeper: the equations, the shapes and the code

What a score is. The model produces a distribution over its whole 30,522 token vocabulary at the masked position, and these are the five largest. They do not sum to 1 and are not meant to: the remainder is spread over everything else, and the panel says how much. A prompt where the top five hold 0.98 and one where they hold 0.22 are different kinds of answer, and showing only the ranking would hide that difference completely.

On the probes. They are in the data because a model trained on a large amount of human text reproduces the associations in it, and the only way to discuss that is to look at it. The prompts are marked, their notes are shown above the numbers rather than below, and nothing here is offered as a correct answer. The pipeline marks them in the file itself, and a unit test fails if a probe ever arrives without its note.

One thing this cannot show you: whether a different phrasing would change the answer. The prompts are fixed, because every number on this site comes from a recorded run rather than from a model in your browser. Running the notebook is how you ask your own questions, and Part 5 is set up for exactly that.

Source: bert-base-uncased. Generated 2026-12-05 by pipeline/fill_mask.py.12.3 KB, checksum 8b583fdff827.

Base against large

Put two measured checkpoints side by side and find out where their size actually goes.

Run this yourself: notebook Part 2

Loading the measured parameter counts.

What to notice

  • Look at the two bars before reading the table. Base and large are drawn to the same scale, and large is 3.06 times the parameters, though it has only twice the layers. Depth is not the whole story.
  • Compare the feed forward row with the attention row, on either side. The feed forward holds almost exactly twice as much: 51.8% against 25.9% on base. The chapter on attention spends its whole length on the part that holds a quarter of the weights.
  • Watch the embeddings row as you go from base to large. It falls from 21.8% to 9.5%, because the vocabulary table grows only with the width while the layer stack grows with both the width and the depth. In the distilled model it is 35.6%, over a third.
  • Change "stored as" from float32 to int8. Base goes from 437.9 MB to 109.5 MB, one byte per parameter. That is the number that decides whether a model fits where you want to put it, and it is the same model either way.
  • Put base against the distilled checkpoint. Half the layers, the same width, 1.64 times fewer parameters, and a classifier head of 592,130 that the pretrained models do not carry.
Go deeper: the equations, the shapes and the code

Why the feed forward is twice attention, every time. Attention has four square projections per layer, so four times the square of the hidden size. The feed forward has two rectangles of hidden by intermediate, and in this family the intermediate width is four times the hidden size, so eight times the square of it. Eight against four, and the biases are too small to move it: the measured ratio is 1.999 on all three checkpoints.

Why base to large is 3.06 and not 2. Twice the layers, and each layer 1.78 times heavier because its terms go with the square of the width. That is 3.55 on the layer stack alone. The embeddings grow by only 1.33, the width ratio, and they are a fifth of base, which pulls the total back down to 3.06. The panel shows this arithmetic rather than asserting it.

On the megabytes. They are weights only, computed here from the measured parameter count and the bytes per parameter, and the unit tests require them to match what the pipeline wrote. Serving a model also needs room for the activations, which grow with the sequence length and the batch size, and training needs room for the gradients and the optimiser state as well. Those depend on how it is being run, so this panel does not put a figure on them rather than offering one it cannot source.

The counts themselves come from loading each checkpoint and walking its module tree: 199 tensors in base, 391 in large, 104 in the distilled model. The notebook prints 109.5M for base in Part 2, and the exact figure is 109,482,240, which is where the 109.48M on this page comes from.

Source: bert-base-uncased, bert-large-uncased, distilbert-base-uncased-finetuned-sst-2-english. Generated 2026-12-05 by pipeline/param_counts.py.5.3 KB, checksum 806ad5d44018.

Go deeper

BERT is an encoder. It reads a whole sequence and produces a vector per token, which makes it good at classifying, tagging and answering from a passage, and unable to write you a paragraph. The models that write are decoders, trained to predict the next word rather than a hidden one. The block inside them is the same block.

The phrase to be careful with is "the model is paying attention to". A head with a high weight from one token to another has shown you where information could flow, on this sentence, in this head. It has not shown you what the model concluded, and it has not shown you why.

Three questions before you move on

Nothing is scored or saved. Pick an answer and the reason appears either way.

  1. BERT masks 15% of the tokens. How is it decided which ones?

  2. Layer 3, head 10 puts 0.87 of what "it" has to spend onto "animal". What does that entitle you to say?

  3. The top five predictions for "The capital of India is [MASK]" hold 0.599 between them. What is the other 0.401?