ELECTRA › The Replaced Token Detection Objective

Michael BrenndoerferUpdated July 25, 20253 min read
Language AI HandbookAll chapters
Part 24 of 67BERT and Variants
Chapter 175ELECTRA › The Replaced Token Detection Objective
Reading section 6 of 18 · The Replaced Token Detection Objective · 3 minSection 6 of 18 · 3 min

Book contents

Section 6 of 18 · 3 min read

ELECTRA › The Replaced Token Detection ObjectiveOne section · Sections in this chapter6 / 18 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The RTD objective trains the discriminator to predict is_replaced for every token. Let's formalize the setup before presenting the loss function.

Problem setup:

  • We start with an original sequence x=[x1,x2,…,xn]\mathbf{x} = [x_1, x_2, \ldots, x_n] containing nn tokens
  • A random subset of positions M⊂{1,…,n}\mathcal{M} \subset \{1, \ldots, n\} is selected for masking (typically 15% of positions)
  • The generator receives the sequence with [MASK] tokens at positions in M\mathcal{M} and produces replacement tokens x~i\tilde{x}_i for each i∈Mi \in \mathcal{M}
  • The corrupted sequence x~\tilde{\mathbf{x}} combines original and generated tokens: x~i=xi\tilde{x}_i = x_i for positions not in M\mathcal{M}, and x~i\tilde{x}_i equals the generator's sampled token for positions in M\mathcal{M}

Note that even at masked positions, the generator might sample the same token as the original. These positions are technically "original" for the discriminator's task, since no replacement occurred.

The discriminator outputs logits D(x~)iD(\tilde{\mathbf{x}})_i for each position. The RTD loss is the standard binary cross-entropy, summed across all positions in the sequence:

LRTD=−∑i=1n[yilog⁡σ(Di)+(1−yi)log⁡(1−σ(Di))]\mathcal{L}_{\text{RTD}} = -\sum_{i=1}^{n} \left[ y_i \log \sigma(D_i) + (1 - y_i) \log (1 - \sigma(D_i)) \right]

where:

  • nn: the total number of tokens in the sequence (e.g., 512)
  • Di=D(x~)iD_i = D(\tilde{\mathbf{x}})_i: the discriminator's raw logit output at position ii, representing how confident the model is that the token was replaced
  • σ(⋅)\sigma(\cdot): the sigmoid function, which converts the logit to a probability between 0 and 1
  • yi=1[x~i≠xi]y_i = \mathbb{1}[\tilde{x}_i \neq x_i]: the ground truth label, equal to 1 if the token at position ii was replaced by the generator, and 0 if it's the original token

The loss decomposes into two cases: when yi=1y_i = 1 (token was replaced), the loss is −log⁡σ(Di)-\log \sigma(D_i), encouraging the model to output a high logit. When yi=0y_i = 0 (token is original), the loss is −log⁡(1−σ(Di))-\log(1 - \sigma(D_i)), encouraging a low logit. The sum runs over all nn positions, including the unmasked ones. This is the main difference from MLM: every token contributes to learning.

Out[17]:
Visualization
Side-by-side comparison showing MLM learning from sparse masked positions versus RTD learning from all positions.
Learning signal comparison between MLM and RTD. MLM learns from 15% of tokens (masked positions only), while RTD learns from 100% of tokens. This 6.7x increase in learning signal per batch explains ELECTRA's sample efficiency.