Week 08 · Pretraining objectives
Compare predicting the next token with reconstructing a token from both sides.
← Week 7 · Subject contents · Week 9 →
Compare predicting the next token with reconstructing a token from both sides.
Work through the problem
Next-token prediction estimates P(wₜ|w before t). Masked-token prediction estimates missing content given the visible context, potentially on both sides. Both can use cross-entropy but they expose different evidence to the learner.
For a toy corpus containing equal numbers of “the red fox” and “the blue jay,” a next-token predictor at “the” cannot distinguish red from blue. A masked-token predictor with both “the” and “fox” visible can. Train frequency tables for both to see the conditional probabilities directly.
Run the Python lab
from collections import Counter,defaultdict
sentences = ["the red fox","the blue jay"]*12
left, both = defaultdict(Counter),defaultdict(Counter)
for a,b,c in map(str.split,sentences):
left[a][b] += 1
both[a,c][b] += 1
def prob(table,context,token):
counts = table[context]
return counts[token]/counts.total()
print("Next token P(red | the):",prob(left,"the","red"))
print("Masked P(red | the, fox):",prob(both,("the","fox"),"red"))
assert prob(left,"the","red")==.5 and prob(both,("the","fox"),"red")==1
Output from the lab
Next token P(red | the): 0.5
Masked P(red | the, fox): 1.0
Practice
- Could the masked model score a natural left-to-right generation task directly?
Check your reasoning
- Its training probabilities use a different conditioning pattern. A generation procedure must supply a compatible order or strategy.
← Week 7 · Subject contents · Week 9 →
Further reading · Jurafsky and Martin · Speech and Language Processing