Introlanguage modelstransformersdeep learning

BERT

Pre-training deep bidirectional transformers for language understanding

Before BERT, the strongest language models read text in one direction — left to right — so when understanding a word they could only use the words before it. BERT's insight was to read in both directions at once, letting every word draw on its full context, left and right. To train a model that way, the authors invented a clever fill-in-the-blank game: randomly hide 15% of the words and have the model guess them from everything around them (the Masked Language Model), plus a second task of judging whether one sentence naturally follows another. After this self-supervised “pre-training” on 3.3 billion words of books and Wikipedia — no human labels needed — the same model can be lightly “fine-tuned” for almost any language task by adding just one small output layer. This one recipe set new records on eleven benchmarks at once, pushing the GLUE score to 80.5% and SQuAD question-answering to a 93.2 F1.

15 min readNAACL 2019, 2018Original paper

Jacob Devlin, Ming-Wei Chang, Kenton Lee, Kristina Toutanova

80.5%

GLUE score

A new state of the art across the GLUE benchmark suite — a 7.7-point absolute jump over the previous best.

93.2

SQuAD v1.1 F1

Question-answering test F1 — a +1.5-point improvement, and above the human performance reported for the task.

83.1

SQuAD v2.0 F1

The harder QA benchmark (some questions have no answer) — a +5.1-point absolute improvement.

86.7%

MultiNLI accuracy

Natural-language inference — a +4.6-point absolute improvement.

15%

Tokens masked

The Masked LM randomly hides 15% of WordPiece tokens; of those, 80% become [MASK], 10% a random token, 10% are left unchanged.

110M / 340M

Parameters (Base / Large)

BERT-Base: 12 layers, 768 hidden, 12 heads. BERT-Large: 24 layers, 1024 hidden, 16 heads.

3.3B

Pre-training words

BooksCorpus (800M words) + English Wikipedia (2,500M words) — all unlabeled, no human annotation.

11

Benchmarks topped

BERT set new state-of-the-art results on eleven NLP tasks with the same pre-trained model.

The one-line version

One model, pre-trained once, good at (almost) everything

In 2018, getting a machine to do a new language task usually meant building a new model for it. BERT changed that. It's a single model pre-trained on a mountain of raw text, that you can then fine-tune — with one small added layer — to state-of-the-art performance on question answering, sentiment, inference, and much more.

Two ideas made it work: reading text bidirectionally (both left and right context at once), and a self-supervised training trick that makes bidirectionality possible. Let's build up to both.

The problem

Reading in only one direction leaves information on the table

The strong language models before BERT (like GPT) were unidirectional: they read left to right, so when representing a word they could only use the words that came before it. That's a real limitation — to understand a word, the words after it often matter just as much.

Take “bank” in “I made a deposit at the ____ ” versus “I sat on the river ____ ”. You need the right-hand context to know which “bank” is meant. A left-to-right model reading up to the blank is missing exactly that.

Left-to-right vs. bidirectional — pick a word

Understanding bank — which words can the model use?

BERT uses every other word — both sides at once. Seeing “river” and “sunset” together makes the meaning of bank unambiguous.
Tap a word to see which other words a model can use to understand it. A left-to-right model only sees the words before it; BERT sees both sides at once — the whole sentence.

Think of it like…

A fill-in-the-blank quiz vs. reading a sentence with a hand covering everything to the right. With the full sentence visible, guessing the missing word is far easier — you use clues from both sides. BERT gets to read the whole sentence; the older models had a hand over the right half.

The key trick

The Masked Language Model: a fill-in-the-blank game

Here's the catch: you can't just train a deep bidirectional model the normal way. If every word could see every other word (including itself), predicting the next word becomes trivial — the model would just “see” the answer. BERT's solution is the Masked Language Model (MLM).

Randomly hide 15% of the words in a sentence, then train the model to guess them from all the surrounding context. Because the target words are hidden, the model can safely look in both directions. There's one subtlety: the special [MASK] token used in training never appears when you later fine-tune. So of the chosen 15%, the model replaces them with [MASK] 80% of the time, a random word 10% of the time, and leaves them unchanged 10% of the time — keeping training and real use aligned.

Play the masking game

thecatsatonthesoftwarmmatbythefireplace

Press “Mask ~15%” to hide some tokens the way BERT does during pre-training.

Press “Mask” to hide ~15% of the tokens the way BERT does — mostly [MASK], sometimes a random swap, sometimes left intact — then reveal the model's guesses. The 80/10/10 split keeps pre-training aligned with fine-tuning.

Why the weird 80/10/10 split?

If the model only ever saw [MASK], it would over-rely on that token and get confused at fine-tuning time (when [MASK] never appears). Mixing in random and unchanged words forces it to build a good representation of every token, just in case it needs to be predicted.

The second task

Next Sentence Prediction: understanding how sentences relate

Many tasks — question answering, natural-language inference — hinge on the relationship between two sentences, which word-level prediction alone doesn't capture. So BERT adds a second, simple pre-training task: Next Sentence Prediction (NSP).

Give the model two sentences, A and B. Half the time B really is the sentence that followed A (labeled IsNext); half the time B is a random sentence from the corpus (NotNext). The model learns to tell which. It's trivially easy to generate this data from any text, and the final model reaches 97–98% accuracy on it.

Feeding it in

How BERT reads its input

BERT packs its input as a sequence that can hold one or two sentences. A special [CLS] token starts every sequence — its final hidden state becomes the summary used for classification. A [SEP] token separates the two sentences.

Each token's input is the sum of three embeddings: the token itself (from a 30,000-word WordPiece vocabulary), a segment embedding (is this sentence A or B?), and a position embedding (where in the sequence). Adding them lets one vector carry what the word is, which sentence it's in, and where it sits.

Build BERT's input, layer by layer

Token
[CLS]helikesplay##ing[SEP]mydogiscute[SEP]
+
Segment
E_AE_AE_AE_AE_AE_AE_BE_BE_BE_BE_B
+
Position
E_0E_1E_2E_3E_4E_5E_6E_7E_8E_9E_10
Input =Σ
[CLS]helikesplay##ing[SEP]mydogiscute[SEP]
Token: What word it is (WordPiece, 30k vocab)Segment: Sentence A or BPosition: Where in the sequence (0, 1, 2…)

Each token's input vector is the sumof these three embeddings. Toggle them off to see what information disappears: without segment, BERT can't tell sentence A from B; without position, it loses word order. [CLS] summarizes the sequence; [SEP] separates the sentences.

Every token's input vector is the sum of its token, segment (sentence A/B), and position embeddings. Toggle each layer to see how [CLS] and [SEP] structure the sequence.

The recipe

Pre-train once, fine-tune for anything

This is the paradigm BERT popularized. Pre-training is expensive and done once: the model learns MLM + NSP on 3.3 billion words of unlabeled text (BooksCorpus + Wikipedia), absorbing general language understanding. Fine-tuning is cheap and done per task: start from the pre-trained weights, add a single output layer, and train briefly on the labeled task data.

The beauty is how little changes between tasks — the same pre-trained model becomes a question-answerer, a sentiment classifier, or an entailment judge just by swapping the tiny top layer and fine-tuning.

From raw text to any task

Done once

Pre-training

Learn MLM + NSP on 3.3B words of unlabeled text (BooksCorpus + Wikipedia). Expensive, but only happens once.

Pre-trained BERT
+ 1 layer

Per task · fast

Fine-tuning

Add one output head (Span classifier) and train briefly on the labeled task data.

Question answering

Pick a downstream task:

Same pre-trained model, every time. “When was BERT released?” → highlights the answer span in a passage. Only the small output head changes between tasks — the heart of the pretrain-then-fine-tune recipe.

Watch the same pre-trained BERT get adapted to different downstream tasks by adding just one output layer. Pre-training is done once on unlabeled text; fine-tuning is fast and task-specific.

Think of it like…

A broad university education, then a short on-the-job training. BERT's pre-training is the general degree — expensive, once, on huge amounts of material. Fine-tuning is the quick role-specific onboarding: a few days to specialize a well-educated generalist into a specialist.

The evidence

New records on eleven tasks at once

With the same pre-trained model and minimal per-task changes, BERT set new state-of-the-art results across the board. It pushed the GLUE score to 80.5% (a 7.7-point jump), MultiNLI to 86.7%, SQuAD v1.1 question-answering to a 93.2 test F1, and the harder SQuAD v2.0 to 83.1 F1. Larger was better: BERT-Large (340M parameters) consistently beat BERT-Base (110M), even on small datasets.

By the numbers

80.5%

GLUE score

A new state of the art across the GLUE benchmark suite — a 7.7-point absolute jump over the previous best.

93.2

SQuAD v1.1 F1

Question-answering test F1 — a +1.5-point improvement, and above the human performance reported for the task.

83.1

SQuAD v2.0 F1

The harder QA benchmark (some questions have no answer) — a +5.1-point absolute improvement.

86.7%

MultiNLI accuracy

Natural-language inference — a +4.6-point absolute improvement.

Explore the GLUE results

OpenAI GPT
75.1
BERT-Base
79.6
BERT-Large
82.1

Average: Overall GLUE score (WNLI excluded). BERT beats the previous best (GPT) on every task, and BERT-Large beats BERT-Base — bigger helps.

Per-task GLUE scores comparing the previous best (OpenAI GPT) with BERT-Base and BERT-Large. Switch tasks to see the consistent gains — and that bigger BERT is better.

The lasting idea

BERT showed that one general model, pre-trained on raw text with a bidirectional fill-in-the-blank objective, could be cheaply specialized for almost any language task — and beat purpose-built systems. That pretrain-then-fine-tune template, and the emphasis on scale and self-supervision, is the direct ancestor of every large language model that followed.

Figure 1 in the original paper

The paper's diagram of BERT's two stages — pre-training on unlabeled text, then fine-tuning the same architecture for each downstream task.

Devlin et al., 2018 · view on arXiv

Key takeaways

  • BERT reads text bidirectionally — using both left and right context for every word — unlike earlier left-to-right models.
  • The Masked Language Model hides 15% of tokens (80% [MASK] / 10% random / 10% unchanged) so a deep bidirectional model can be trained without “seeing the answer.”
  • Next Sentence Prediction teaches the model relationships between sentence pairs, helping QA and inference tasks.
  • Each input token is the sum of token + segment + position embeddings, with [CLS] summarizing the sequence and [SEP] separating sentences.
  • Pre-train once on 3.3B words of unlabeled text, then fine-tune per task by adding a single output layer.
  • One recipe set new records on 11 tasks — GLUE 80.5%, SQuAD v1.1 F1 93.2 — and made pretrain-then-fine-tune the standard.
Share X LinkedIn

Keep exploring

IntermediateFeatured

Chain-of-Thought Prompting Elicits Reasoning in Large Language Models

Ask a big model to show its work — and it starts to reason

Big language models were surprisingly bad at multi-step problems — grade-school math, logic puzzles — often blurting a wrong final answer. This paper found a shockingly simple fix that requires no training at all: in the few examples you show the model (the “prompt”), don't just show it question-and-answer pairs — show it the *reasoning in between*. Write out the step-by-step thinking that leads to each answer. The model then imitates that pattern on new problems, producing its own “chain of thought” before answering — and accuracy leaps. The headline result: prompting a 540-billion-parameter model with just eight worked examples hit 56.9% on the GSM8K math benchmark — up from 17.9% with ordinary prompting — beating the previous best (a model specially fine-tuned for the task and paired with a separate verifier, at 55%). The twist: this only works in *large* models. In small ones, showing the steps does little or even hurts. Reasoning-by-prompting is an ability that *emerges* with scale — a discovery that reshaped how people use LLMs.

chain-of-thoughtpromptingreasoning
14 min4 interactiveExplore
IntroFeatured

QLoRA: Efficient Finetuning of Quantized LLMs

Fine-tune a 65-billion-parameter model on one gaming GPU

LoRA already made fine-tuning cheaper by training tiny add-on matrices instead of the whole model. But there was still a wall: you had to fit the giant frozen base model in GPU memory just to run it — and a 65-billion-parameter model needs over 780 GB, far beyond any single GPU. QLoRA smashes that wall by squashing the frozen base model down to 4 bits per number (instead of the usual 16), roughly a 4× shrink, so the whole thing fits in under 48 GB — a single high-end GPU. It does this without hurting quality, using three clever tricks: a new 4-bit number format (NF4) tuned to how weights are actually distributed, “double quantization” that even compresses the compression bookkeeping, and “paged optimizers” that spill memory to the CPU during brief spikes instead of crashing. The tiny LoRA add-ons still train in full precision on top. To prove it works, they fine-tuned a chatbot called Guanaco in 24 hours on one GPU — and it reached 99.3% of ChatGPT's score on a popular benchmark. QLoRA is why hobbyists and small labs can now fine-tune enormous models at home.

QLoRAquantization4-bit
15 min4 interactiveExplore
IntroFeatured

Retrieval-Augmented Generation for Large Language Models: A Survey

RAG — giving a language model an open book to read from

A language model only knows what it saw during training. So it can confidently make things up (“hallucinate”), it can't know anything recent, and it can't see your private documents. Retrieval-Augmented Generation (RAG) fixes this with a simple, powerful idea: before the model answers, go fetch relevant text from an external library and hand it to the model along with the question — like turning a closed-book exam into an open-book one. The basic recipe has three steps: (1) Indexing — chop your documents into chunks and store them so they're searchable by meaning; (2) Retrieval — when a question comes in, pull the few most relevant chunks; (3) Generation — feed those chunks plus the question to the model so its answer is grounded in real sources. This survey organizes the whole field into three generations of increasingly sophisticated RAG — Naive, Advanced, and Modular — and breaks every system into three parts: what you retrieve, how you use it, and how the model generates. RAG is now one of the most widely used techniques in real-world AI, because it makes answers more accurate, up-to-date, and traceable to a source.

RAGretrievalhallucination
15 min6 interactiveExplore