AI State of the Art
Fundamentals/NLP: From Classical to Modern/23 min
Section 41

BERT: Bidirectional Pre-training, MLM, NSP

Listen as a podcast →

The thunderclap: October 2018

Google shows up with BERT, Bidirectional Encoder Representations from Transformers, and takes the top of every leaderboard in natural language processing (NLP). The paper reports new records on eleven tasks at once. This is the moment everything pivots: no more task-specific models, make way for massive pre-training plus small adaptation, the paradigm that governs AI today.

Here are the figures the paper's abstract gives, to size the jump:

BenchmarkBERT scoreGain over the previous best
GLUE, General Language Understanding Evaluation: the average over nine understanding tasks80.5%+7.7 points
MultiNLI, inference between two sentences: does the second follow from the first?86.7%+4.6 points
SQuAD v1.1, Stanford Question Answering Dataset: locate the answer inside a paragraph. Test F193.2+1.5 point
SQuAD v2.0, same test, except some questions deliberately have no answer83.1+5.1 points

The F1 score folds precision and recall into a single number; see the basic metrics. The headline is that gaining 7.7 points on a leaderboard where teams were fighting over tenths was unheard of.

BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding

The classic problem: limited attention vs full vision

Before BERT, there were two schools:

Unidirectional models (GPT-1, for Generative Pre-trained Transformer, June 2018, yes, GPT is contemporaneous!):

  • Read left to right only
  • To predict the next word, only see previous tokens
  • Excellent for generation (natural: you generate word by word)
  • Catastrophic for comprehension: miss future context

Example: "The lawyer argued before the [???]". The missing word is on the right. A unidirectional reading can't see it. The model must guess blindly.

BERT's central idea: What if we masked random words and asked the model to guess them by looking at all available context, before and after the missing word?

That's called masked language modeling, abbreviated MLM.

StepText
Original text"The black cat sleeps"
Mask"The [MASK] cat sleeps"
ObjectivePredict "black" by seeing ["The", "cat", "sleeps"]

What's revolutionary here: the model can exploit the full context (words before and after) without cheating. Since it can't see the real token (replaced by [MASK]), there's no information leakage. But it has enough context to make an informed prediction. Result: BERT learns representations that integrate meaning from both directions, something no autoregressive model can do naturally.

Bidirectionality without the trick

If BERT saw the masked word, it could "cheat": learn that [MASK] = what you need to predict. Instead, BERT asks itself "What is this masked token?" and has to look for clues around it. Result: it learns a rich representation of the bidirectional context.

The architecture: just an encoder

BERT uses the encoder of a standard transformer. No decoder (unlike T5). Why? Because BERT isn't made to generate text, it's for understanding. On the encoder/decoder split, see encoder-decoder.

Structure, as reported in the paper:

  • Each token looks at all other tokens via multi-head attention
  • 12 stacked layers (BERT-base) or 24 (BERT-large)
  • internal vectors of 768 numbers (base) or 1024 (large)
  • 110 million (base) or 340 million (large) learned parameters, numbers frozen once training ends, identical for every text you later feed the model

The text itself is split into tokens by WordPiece, a cousin of BPE (byte pair encoding), see tokenizers beyond BPE.

Special tokens: three vocabulary entries that correspond to no word, inserted by the program to signal something to the model.

  • [CLS], for classification: always first in the sequence. Its vector out of the last layer acts as a summary of the whole sentence, that is a computed value for the text at hand, not a learned parameter
  • [SEP], for separator: separates two sentences, when the task is about a sentence pair
  • [MASK]: replaces masked words during training

Technical corner: the MLM objective and its subtleties

MLM is BERT's main training objective.

Process:

  1. Take 15% of tokens at random
  2. For each one, three options:
    • 80% of the time: replace with the [MASK] token (the hole)
    • 10% of the time: replace with a random token from the vocabulary (noise)
    • 10% of the time: keep unchanged (no change)
VariantText
Initial input"The cat sleeps on the couch"
Masked (80%)"The [MASK] sleeps on the [MASK]"
With noise (10%)"The telephone sleeps on the hamburger"
Unchanged (10%)"The cat sleeps on the couch"

Objective: predict "cat" and "couch" despite the perturbations.

Why this weird 80-10-10 ratio?

Intuition: if you always put [MASK] where the word to predict goes, BERT would take a shortcut, "wherever [MASK] appears there is something to guess; everywhere else I can coast." But when you adapt the model to a real task (fine-tuning, further training on labelled data), [MASK] tokens are gone from the text. The shortcut becomes useless, and the model is left helpless on words it never had to represent properly.

Hence the two additions. The 10% replaced by a random word force BERT to ask, for every token, whether the one it is reading is plausible: it must therefore represent all tokens well, not just the masked ones. The 10% left unchanged close the last escape route: the model cannot conclude "this token isn't masked, so it must be correct."

Result: a rich, general internal representation that doesn't depend on the [MASK] symbol being present.

MLM vs next-token prediction (GPT)

GPT predicts the next token, which only lets it look left. BERT predicts a masked token in the middle, which lets it look both ways. MLM therefore forces bidirectional understanding, richer for analysing a text, but unusable for producing one, since at generation time there is nothing to the right. That is the whole reason BERT excels at classification and extraction and not at generation.

The second objective: NSP (and why it failed)

BERT adds a second objective: NSP (next sentence prediction).

You give the model two sentences and it must say: "Does the second sentence logically follow the first?"

TypeSentence ASentence BLabel
Positive (consecutive)"Paris is the capital""It is beautiful"1
Negative (random)"Cats eat fish""The Moon orbits Mars"0

The verdict: Later, researchers discovered that NSP barely helped. RoBERTa (2019) showed that removing NSP and focusing solely on MLM gave the same performance, often better.

Why does NSP fail? The task is too easy to solve in the wrong way. Negative sentences are randomly drawn from other documents, so they're often thematically incompatible with the anchor sentence. The model learns to detect superficial thematic coherence, not fine discursive logic. That's not the useful skill. MLM, on the other hand, truly forces the model to understand the meaning of each word in its complete context, that's much richer.

A plausible idea is not a correct one

NSP looked logical. Measurement ruled against it. That is the normal cycle: hypothesis, implementation, then experiment beating intuition. Hence the importance of ablation, retraining the model with one component removed at a time to see what it was actually contributing.

Training data and resources

The figures below are the ones reported in the paper:

  • BooksCorpus: 800 million words, from books, long, continuous prose
  • English Wikipedia: 2.5 billion words

Total: about 3.3 billion words. Sequences of 512 tokens, batches of 256 sequences, one million training steps. You can sanity-check that by hand: 256 x 512 is 131,072 tokens per step, so about 131 billion tokens over a million steps; against a 3.3-billion-word corpus, that is roughly 40 passes over the whole data set.

Hardware: the paper reports Google's own accelerator chips, TPUs (tensor processing units, the in-house counterpart to a graphics processing unit), for 4 days, 16 chips for BERT-base, 64 for BERT-large. The paper puts no dollar figure on it, and later published estimates vary by orders of magnitude depending on the pricing assumed: better to remember "four days on specialised hardware" than any particular sum of money.

Result: a pre-trained model released for free that anyone can pick up and adapt.

The secret: efficient fine-tuning

Here's the magic of BERT pre-training:

Phase 1 (once, in the lab): pre-train BERT on 3.3 billion words. Expensive, time-consuming, done once by whoever can afford it.

Phase 2 (you, right now): take the pre-trained BERT and adapt it to your task:

  1. Remove the output layer that predicted masked words, replace it with a small layer suited to your task
  2. Retrain the whole thing on your labelled data, typically a few thousand examples
  3. Only a few epochs, the paper recommends 2 to 4 passes over your data

On what happens during that second phase, see fine-tuning.

Fine-tuning examples:

  • Classification (sentiment, spam, topic): attach a classification layer to the [CLS] token's vector, which serves as the sentence summary
  • Answer extraction (question answering): the model predicts the position of the first and last word of the answer inside the passage you supply
  • NER (named entity recognition: spotting names of people, places, organisations): classify each token individually
  • Part-of-speech tagging (POS: noun, verb, adjective...): same principle, one label per token
  • Similarity: compare two sentence vectors by the cosine of the angle between them, see norms and distances

The point isn't a precise number of hours or examples, but the order of magnitude of the shift: where you previously had to assemble a large annotated data set and train a model from scratch for every task, a few thousand examples and a few hours of compute now suffice, because everything about the language itself has already been learned.

Pre-training as a shared cost

That is the whole economics of it: one heavy training run, paid once by an actor who can afford it, then thousands of teams starting from that model for a tiny fraction of the cost. Compute spent once is amortised across every later use. It is the business model of every foundation model that followed.

The impact: NLP changed shape

BERT revolutionized the field:

  • Democratization: before BERT, reaching state of the art on a language task took a large annotated data set and a specialised team. After, one engineer with a few thousand examples gets there.
  • Benchmarks: on GLUE and SQuAD, both introduced at the top of this page, the leaderboards were overturned at a stroke. On what such benchmarks are worth in general, see MMLU and its limits.
  • Ecosystem: RoBERTa (same architecture, better training recipe), DistilBERT (compressed via distillation), ALBERT (A Lite BERT), DeBERTa (Decoding-enhanced BERT, refined attention) were born directly from the BERT idea
  • Real deployment: Google announced in 2019 that it was using BERT in its search engine. That affects the results you see.
  • Transition: encoder-decoder models (T5, BART) and decoder-only models (GPT) progressed in parallel. In 2026, decoder-only dominates by a wide margin.

Why this matters

BERT showed that reading in both directions, through masking, produces qualitatively superior representations. The 7.7 points gained on GLUE give the measure of the jump, on a leaderboard where teams fought over tenths.

The massive pre-training then lightweight fine-tuning paradigm became the industry standard, and that is arguably its main legacy: every foundation model today works that way.

Where BERT stands in 2026

Two uses need separating. As an encoder (turning a text into a vector to classify it, compare it, or index it in a search engine) the BERT family is still heavily used in production: fast, light, and good enough. But BERT itself is no longer the state of the art there; the embedding models that succeeded it do better, see text embeddings. As a generative model, on the other hand, it was never in the running and never will be: masking trains it to fill gaps, not to produce text. Dialogue, free-form summarization, writing: that is the domain of decoder-only models (GPT, Claude, Mistral). So read this lesson to understand the encoder architecture and pre-training, not to pick a generative model.

For a guided reading of the paper itself, see the BERT paper.

Check your understanding0 / 4

1. How does masking let BERT read in both directions without cheating?

2. Why not always replace the token to predict with [MASK], hence the 80-10-10 ratio?

3. What did the RoBERTa experiment reveal about the NSP objective (next sentence prediction)?

4. Why was BERT never in the running for text generation?