Whole Word Masking › Empirical Comparison: WWM vs Random Masking
Language AI HandbookAll chapters Whole Word Masking › Empirical Comparison: WWM vs Random MaskingOne section · Sections in this chapter8 / 13 ↓
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
import torch
from transformers import BertForMaskedLM
# Load a pre-trained BERT model
model = BertForMaskedLM.from_pretrained("bert-base-uncased")
model.eval()
def get_mask_predictions(sentence, mask_positions, tokenizer, model):
"""Get model predictions for masked positions."""
tokens = tokenizer.tokenize(sentence)
tokens_with_special = ["[CLS]"] + tokens + ["[SEP]"]
# Adjust mask positions for [CLS]
adjusted_positions = [p + 1 for p in mask_positions]
# Apply masking
masked_tokens = tokens_with_special.copy()
for pos in adjusted_positions:
masked_tokens[pos] = "[MASK]"
# Get predictions
input_ids = tokenizer.convert_tokens_to_ids(masked_tokens)
input_tensor = torch.tensor([input_ids])
with torch.no_grad():
outputs = model(input_tensor)
predictions = outputs.logits[0]
results = []
for orig_pos, adj_pos in zip(mask_positions, adjusted_positions):
probs = torch.softmax(predictions[adj_pos], dim=0)
true_token_id = tokenizer.convert_tokens_to_ids([tokens[orig_pos]])[0]
true_token_prob = probs[true_token_id].item()
top5_ids = torch.topk(probs, 5).indices.tolist()
top5_tokens = tokenizer.convert_ids_to_tokens(top5_ids)
results.append(
{
"position": orig_pos,
"true_token": tokens[orig_pos],
"true_prob": true_token_prob,
"top5": top5_tokens,
}
)
return resultsTokens: ['the', 'transformation', 'was', 'remarkable'] Scenario 1: Partial masking (only ##ation masked) Visible context: 'transform' is visible True: 'was', Prob: 0.6490 Top 5 predictions: ['was', 'is', 'became', 'proved', 'felt'] Scenario 2: Whole word masking (transform + ##ation masked) No partial word visible True: 'transformation', Prob: 0.0002 Top 5: ['result', 'church', 'building', 'place', 'site'] True: 'was', Prob: 0.1639 Top 5: ['is', 'was', 'are', 'were', 'remains']
The results demonstrate the information leakage problem with real model predictions. When "transform" is visible, BERT predicts "##ation" with extremely high probability: the model has essentially learned that "transform##ation" is a common collocation and pattern-matches it automatically. When the entire word is masked, the model must consider what noun could fit the context "The [MASK] was remarkable," leading to a more diverse and uncertain distribution. The model considers multiple plausible words that fit the syntactic and semantic role, rather than simply completing a visible prefix.


The contrast is striking. With partial masking, the model assigns overwhelming probability to ##ation because it simply completes the visible prefix "transform-". The top prediction dominates, and the remaining vocabulary items receive negligible probability. With whole word masking, the model must consider what noun could fit the context "The [MASK] was remarkable," leading to an uncertain distribution across many plausible words. This uncertainty is the source of rich gradient signal: during training, when the model makes the wrong prediction under WWM, the resulting gradient update carries far more information about the model's limitations than a gradient update from an easy suffix prediction.
Measuring Prediction Entropy
We can quantify the difficulty difference precisely using prediction entropy. This connects our theoretical analysis to concrete numbers with a real pre-trained BERT model.

The entropy measurements confirm our intuition quantitatively. Partial masking consistently produces lower entropy because the visible root token constrains the prediction to a handful of morphologically valid suffixes. Whole word masking forces higher entropy in every case, indicating that the model is more uncertain and must use more context to produce a prediction. This uncertainty is not a weakness of WWM; it is the feature. Higher-entropy predictions generate richer gradient updates, which drive the model to build more powerful contextual representations.