ELECTRA pre-training proceeds as follows for a sentence x=[x1,…,xn].
- Randomly select k positions (typically 15% of the total, k=⌈0.15n⌉). Call the set of selected positions m.
- Replace the words at the selected positions with
[MASK] to obtain xmasked.
- The generator outputs word probabilities pG at each masked position and samples a word x^i from them.
- Replace the masked positions with x^i to form the corrupted sentence xcorrupt.
- The discriminator predicts, for every position in xcorrupt, whether the word matches the original sentence.
- The generator loss and the discriminator loss are summed, and both networks are updated together.
The probability that the generator outputs word xt at masked position t is given below, using the token embedding e and the generator output hG.
pG(xt∣x)=∑x′exp(e(x′)⊤hG(x)t)exp(e(xt)⊤hG(x)t)
The probability that the discriminator judges the word at position t to be "original" is:
D(x,t)=sigmoid(w⊤hD(x)t)
The generator loss is the standard masked language modeling loss of predicting the original words at the masked positions.
LMLM=E(i∈m∑−logpG(xi∣xmasked))
The discriminator loss is the binary cross-entropy over all positions.
LDisc=E(t=1∑n−1(xtcorrupt=xt)logD(xcorrupt,t)−1(xtcorrupt=xt)log(1−D(xcorrupt,t)))
The combined loss is minimized over a large text corpus X.
θG,θDminx∈X∑LMLM(x,θG)+λLDisc(x,θD)
The paper uses λ=50. Because the discriminator loss is binary classification, it tends to be much smaller than the generator loss, which is classification over a 30,000-word vocabulary, so a large weight balances the two.
Positions where the generator happens to pick the original word are treated as "original," not "replaced." In the figure's example, the generator put the same word "chef" back into the masked "chef" position, so the correct label at that position is "original." The paper reports that this treatment slightly improves downstream results.
The pre-training settings in the paper are as follows (nearly the same as BERT).
| Item | Small | Base | Large |
|---|
| Data | Wikipedia and BooksCorpus (3.3B tokens) | Same as Small | Same data as XLNet (33B tokens) |
| Mask ratio | 15% | 15% | 25% |
| Batch size | 128 | 256 | 2,048 |
| Steps | 1M | 766K | 400K (1.75M for the longer-trained model) |
| Learning rate | 5×10−4 | 2×10−4 | 2×10−4 |
The optimizer is Adam (β1=0.9, β2=0.999, ϵ=10−6) with weight decay 0.01, a warmup over the first 10,000 steps, and linear decay afterward. Dropout is 0.1. Following RoBERTa, masks are re-sampled for every input, and next sentence prediction is not used. The mask ratio for Large was raised to 25% because at 15% the generator was so accurate that very few words were actually replaced.