ELECTRA › The Replaced Token Detection Objective
Language AI HandbookAll chapters 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 containing tokens
- A random subset of positions is selected for masking (typically 15% of positions)
- The generator receives the sequence with
[MASK]tokens at positions in and produces replacement tokens for each - The corrupted sequence combines original and generated tokens: for positions not in , and equals the generator's sampled token for positions in
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 for each position. The RTD loss is the standard binary cross-entropy, summed across all positions in the sequence:
where:
- : the total number of tokens in the sequence (e.g., 512)
- : the discriminator's raw logit output at position , representing how confident the model is that the token was replaced
- : the sigmoid function, which converts the logit to a probability between 0 and 1
- : the ground truth label, equal to 1 if the token at position was replaced by the generator, and 0 if it's the original token
The loss decomposes into two cases: when (token was replaced), the loss is , encouraging the model to output a high logit. When (token is original), the loss is , encouraging a low logit. The sum runs over all positions, including the unmasked ones. This is the main difference from MLM: every token contributes to learning.
