← Master Index
Vol. 11 Module 11.2 Lecture

ELECTRA

BERT Family

How This Lesson Fits the Module & Volume

MLM only supervises a subset of tokens (~15%), so compute per token of learning signal is low. ELECTRA (Clark et al., 2020) replaces MLM with a discriminative task: detect which tokens a small generator replaced. The discriminator sees every position—sample-efficient pretraining that often matches larger MLM models at lower cost.

It closes the “better objectives / cheaper training” arc before Sentence-BERT specializes embeddings for similarity.

Learning Objectives

By the end of this lesson, students should be able to:

  • Contrast generative MLM with ELECTRA’s replaced-token detection (RTD).
  • Describe the generator–discriminator setup.
  • Explain why RTD yields denser learning signals.
  • Load an ELECTRA model in Hugging Face for classification.
  • Relate ELECTRA’s discriminator to downstream fine-tuning (generator discarded).
  • Compare compute efficiency vs. BERT/RoBERTa pretraining.
Definition

ELECTRA (Efficiently Learning an Encoder that Classifies Token Replacements Accurately) trains a small MLM generator to propose token replacements and a discriminator to predict, for every position, whether the token is original or replaced. After pretraining, typically only the discriminator is fine-tuned.

Replaced Token Detection

Corrupt

Mask some tokens like MLM.

Generate

Small MLM fills proposals.

Replace

Swap masks with sampled tokens.

Detect

Discriminator labels real vs. fake per position.

AspectBERT MLMELECTRA RTD
Task typeGenerative (vocab softmax)Binary per position
Supervised positions~15% maskedAll tokens
Auxiliary modelNoneSmall generator
Deployed weightsFull encoderDiscriminator encoder

Why It Works

Binary detection is cheaper than full-vocab softmax at every masked spot, and the learning signal covers the entire sequence. Plausible generator replacements make the discriminator solve a hard, language-sensitive problem—not a trivial “spot [MASK]” cue.

from transformers import ElectraTokenizer, ElectraForSequenceClassification tok = ElectraTokenizer.from_pretrained("google/electra-base-discriminator") model = ElectraForSequenceClassification.from_pretrained( "google/electra-base-discriminator", num_labels=2 ) x = tok("ELECTRA detects replaced tokens.", return_tensors="pt") print(model(**x).logits.shape)

Strengths and Tradeoffs

Strengths

  • Strong results at smaller compute budgets.
  • Dense supervision on every token.
  • Clear efficiency story for pretraining.

Tradeoffs

  • Two networks during pretraining (complexity).
  • Generator quality affects difficulty.
  • Less “fill-in-the-blank” generative use than MLM.
Common Misconception

“ELECTRA is a GAN.” The paper discusses adversarial ideas, but training is not a classic minimax GAN loop. Think: generator proposes corruptions; discriminator does efficient discriminative LM pretraining.

Knowledge Check

  1. Short Answer: What does RTD stand for? Answer: Replaced Token Detection.
  2. True/False: ELECTRA’s discriminator is supervised on every token position. Answer: True.
  3. Multiple Choice: After pretraining you typically fine-tune: (a) only the generator, (b) the discriminator, (c) a diffusion U-Net. Answer: (b).
  4. Short Answer: Why is MLM sample-inefficient? Answer: Loss applies mainly to the masked minority of tokens.
  5. True/False: ELECTRA’s generator is usually smaller than the discriminator. Answer: True (common setup).
  6. Multiple Choice: Discriminator output per token is essentially: (a) binary real/replaced, (b) full vocab softmax always, (c) a pixel. Answer: (a).
  7. Short Answer: Name one efficiency benefit vs. BERT. Answer: Better accuracy per pretraining FLOP / denser loss.
  8. Short Answer: Is ELECTRA typically encoder-only? Answer: Yes.
  9. Multiple Choice: ELECTRA is closest in goal to: (a) chat RLHF, (b) efficient NLU pretraining, (c) speech codecs. Answer: (b).
  10. True/False: ELECTRA training is identical to training StyleGAN. Answer: False.

Key Takeaways

  • ELECTRA learns by detecting generator replacements on all positions.
  • Denser loss than MLM improves compute efficiency.
  • Fine-tune the discriminator encoder for downstream tasks.
  • Not a classic GAN despite generator/discriminator naming.
  • Next: Sentence-BERT for sentence embeddings.
Trainer’s Guide

Hands-on idea: Sketch a 5-token toy sequence; mark which positions get RTD labels vs. MLM labels.

Discussion prompt: When is a discriminative pretraining objective preferable to generative MLM?

Recap: ELECTRA makes pretraining denser and cheaper. Continue with Sentence-BERT.