Masked Language Models
Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9
17 cards in “9.3 Training bidirectional encoders” · whole deck
9.3 Training bidirectional encoders
- Why can’t a bidirectional encoder be trained with next-word prediction?Without the causal mask, the next word is directly visible in the context, so the task is trivial.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 231
- What is the cloze task?A fill-in-the-blank task: given a sequence with one or more elements missing, predict the missing elements (“The ___ of Walden Pond is so beautifully…”).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 231
- What is denoising as a training method?Corrupting the input (masking, substituting, reordering, deleting, inserting words) and training the model to recover the original.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 231
- Describe the BERT masking procedure.A sample of tokens (15% in BERT) is chosen. Of these, 80% are replaced with
[MASK], 10% with a random token sampled by unigram probability, and 10% are left unchanged. The model must predict the original token at each chosen position.“So long and thanks for all the fish”
chosen token model input long [MASK]thanks [MASK]the apricot (random) The loss is computed only at the chosen positions. - Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 231
- Why doesn’t BERT always replace chosen tokens with
[MASK]?[MASK]never appears at fine-tuning or inference time. If the model only predicted at[MASK]positions it would learn to predict only there; random replacements and unchanged tokens make it try to predict every input token.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, pp. 231–232
- How does a masked LM predict the token at a masked position?The final layer’s output \(h^L_i\) for that position goes through the LM head: logits \(u_i = h^L_i E^\top\), probabilities \(y_i = \text{softmax}(u_i)\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 232
- What is the MLM loss?The average negative log probability of the correct tokens over the set \(M\) of chosen positions: \(L_{MLM} = -\frac{1}{|M|}\sum_{i \in M}\log P(x_i \mid h^L_i)\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 233
- In what sense is BERT training inefficient?Only the chosen tokens (about 15%) contribute to the loss; the others play no role in learning. (ELECTRA uses all positions.)
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 233
- What is Next Sentence Prediction (NSP), and what is it for?A second BERT objective: given a pair of sentences, predict whether they are actually adjacent in the corpus or the second was randomly chosen (50/50). It targets sentence-pair relationships: paraphrase, entailment, discourse coherence.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 233
- What special tokens and embeddings does BERT add for sentence pairs?
[CLS]prepended to the input,[SEP]between the sentences and at the end, and segment embeddings (first/second) added to the token and position embeddings.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 233
- How is NSP predicted?From the final-layer vector of the
[CLS]token, with a learned classification head: \(y = \text{softmax}(h^L_{CLS}W_{NSP})\), trained with cross-entropy, combined with the MLM loss.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, pp. 233–234
- Why does the
[CLS]representation tend to capture the whole sequence?Because it is the direct input to the NSP classifier, training pushes it to contain information about the sequence as a whole.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 234
- How does RoBERTa’s training regime differ from BERT’s?It drops next sentence prediction: inputs are simply contiguous sentences (starting with
[CLS]), packed across document boundaries with separators up to the context length, with large batches.- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 234
- How is the training data sampled to build a multilingual tokenizer vocabulary?Not uniformly (which would favor English), but by reweighting each language’s share: \(q_i = \frac{p_i^\alpha}{\sum_j p_j^\alpha}\) with \(p_i\) its proportion of sentences and \(\alpha\) about 0.3, which upweights less-represented languages.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, pp. 234–235
- What are the advantages of a multilingual encoder over many monolingual ones?One model instead of a hundred, and better performance on low-resource languages by leveraging similar higher-resource languages in the training data.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 235
- What is the curse of multilinguality?When the number of languages grows very large, performance on each language degrades compared with a model trained on fewer languages.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 235
- What does it mean that multilingual models “have an accent”?Grammatical structures of higher-resource languages (often English) bleed into the representations of lower-resource languages, making them slightly more English-like.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 9, §9.3, p. 235
Source
- Daniel Jurafsky and James H. Martin. Speech and Language Processing. 3rd edition (draft). https://web.stanford.edu/~jurafsky/slp3/