Notes

Replaced Token Detection

Masked language modelling teaches a model by hiding words and asking it to guess them back. Replaced token detection turns the exercise around: the model reads a sentence in which a few words have been quietly swapped for plausible alternatives, and decides for every position whether the word it sees is the original or an impostor.

Two networks, one game
The objective was introduced with ELECTRA and runs two models together during pre-training:

  • A small generator — itself a masked language model — proposes replacements for a sample of positions, so “the chef cooked the meal” arrives as “the chef ate the meal”.
  • A larger discriminator classifies each token as original or replaced. That discriminator is the model you keep; the generator is thrown away once pre-training ends.
  • Because the replacements are generated rather than drawn at random, the impostors fit the local context well, and spotting them demands real sensitivity to meaning and grammar.

Why it matters
Masked language modelling computes its loss on only the 15% of positions it hid, and it trains the model to expect a [MASK] symbol that never appears in real use. Replaced token detection scores every position in the sentence and shows the model nothing but ordinary text, so each passage yields far more learning signal and the pre-train/fine-tune mismatch disappears.

What it buys in practice
The payoff is efficiency rather than a new capability: an ELECTRA-style encoder reaches GLUE scores comparable to much larger masked models trained at several times the compute. That is what puts a competitive encoder within reach of a single-GPU budget, and the discriminator fine-tunes exactly like any BERT checkpoint in Hugging Face Transformers.

Replaced token detection is a self-supervised pre-training objective in which a small generator substitutes plausible alternatives for some tokens in a passage and the main model classifies every token as original or replaced. Introduced with ELECTRA, it draws signal from all positions rather than a masked subset and never exposes the model to an artificial mask symbol, yielding encoders that match masked language modelling at a fraction of the pre-training compute.

Imagine a friend retyping a page of your novel and sneaking in a handful of swapped words — “ate” where you wrote “cooked”. Nothing looks broken, so finding the changes forces you to read for sense rather than scan for typos.

That is the training exercise behind replaced token detection. Software quietly alters a few words in real sentences, then asks the model to judge each word in turn: mine, or someone else’s? Every single word gets a verdict, so the model learns from the whole sentence instead of a few blanks — and it only ever sees normal text, which is what it will meet in the real world. The result is a model that picks up grammar and meaning faster, and far more cheaply.