Autoregressive and masked language modelling
What it is
Autoregressive language models factor a sequence probability into next-token predictions: each token is predicted from the tokens to its left. Training shifts the target sequence by one position and applies a causal mask; generation repeatedly samples or selects one token, appends it to context, and runs the next step. Masked language models instead hide selected tokens and use context on both sides, which is useful for representation learning but does not directly define left-to-right generation. Both commonly use cross-entropy, yet their visible context and inference procedures are fundamentally different.
Why it matters
It explains why a decoder-only model generates left to right and why likelihood, perplexity, sampling temperature, and top-p are tied to the same token distribution. Sampling settings are a decision policy over logits: temperature rescales confidence, while top-p truncates the candidate set; neither makes an unsupported answer more truthful or a weakly trained distribution more knowledgeable.
In the real world: Chatbots predict the next word; search models fill in missing words to understand meaning.
Visual example
Deep learningThe same token sequence supplies both the input and, shifted by one, the target.
import torch
import torch.nn as nn
ids = torch.tensor([[1045, 2293, 2204, 4157]]) # (batch, seq)
inputs, targets = ids[:, :-1], ids[:, 1:] # shift by one
logits = model(inputs) # (batch, seq-1, vocab)
loss = nn.CrossEntropyLoss()(
logits.reshape(-1, logits.size(-1)),
targets.reshape(-1),
)Using low training perplexity as proof of helpfulness or safety. It measures predictive fit to text, not factuality, instruction following, fairness, or downstream task value.