Neural Machine Translation › Training Objective
Language AI HandbookAll chapters Training is maximum likelihood estimation on parallel data. Given a corpus , minimize:
Everything below is a refinement of this objective or a consequence of it.
The inner sum is over target tokens, and the outer sum is over training examples. Summing gives each token a term, so longer examples contribute more terms. Dividing the batch loss by the number of nonpadding target tokens keeps its scale comparable across batches with different token counts. Averaging each sentence first would instead weight sentences equally.
Label smoothing
With a one-hot target the gradient on the gold logit is and that on each other logit is . For a fixed example, increasing the gold logit's margin pushes its probability toward one, where those gradients approach zero.
Label smoothing (Szegedy et al., 2016; standard in the transformer paper) replaces the one-hot target with a mixture of the true distribution and the uniform distribution. For vocabulary size , smoothing parameter , and gold token :
The loss becomes:
Label smoothing mixes the gold-token and uniform cross-entropy terms. With , the minimum over predictive distributions is , achieved at . The gradient on logit is , so a prediction can be too confident even when it chooses the correct token.
Let's see the numbers.
PythonIn [3]Expand
import torch
import torch.nn.functional as F
# A 5-token target vocabulary. The model is extremely confident on token 0.
logits = torch.tensor([[6.0, 1.0, 0.5, 0.2, 0.1]])
target = torch.tensor([0])
rows = []
for eps in [0.0, 0.05, 0.1, 0.2]:
z = logits.clone().requires_grad_(True)
loss = F.cross_entropy(z, target, label_smoothing=eps)
loss.backward()
rows.append((eps, loss.item(), z.grad[0, 0].item(), z.grad[0, 1].item()))ConsoleOut [4]Expand
eps loss grad[correct] grad[runner-up] 0.00 0.0165 -0.01632 0.00663 0.05 0.2385 0.02368 -0.00337 0.10 0.4605 0.06368 -0.01337 0.20 0.9045 0.14368 -0.03337
For these logits the gold-token gradient is about without smoothing, so gradient descent raises the gold logit. At the gold target probability is , below the model's prediction of about . The gold gradient changes sign to about , and the runner-up gradient becomes about . A descent step lowers the gold logit and raises the runner-up logit.
The figure varies smoothing while holding the logits fixed. The gold-token gradient rises because increasing smoothing lowers the gold target probability. At the model distribution that matches the smoothed target, all logit gradients are zero.
PythonIn [5]Expand
eps_values = [row[0] for row in rows]
grad_correct = [row[2] for row in rows]VisualizationOut [6]Expand

Vaswani et al. reported higher BLEU and worse perplexity with label smoothing in their Transformer setup. Müller, Kornblith, and Hinton reported better calibration in the configurations they studied and less effective distillation from their smoothed teachers. Evaluate perplexity using the unsmoothed reference-token likelihood and report the tokenization and normalization protocol. Vaswani et al. (2017), section 5.4, Müller et al. (2019)
Length bias in the objective
The log-probability of a token prefix cannot increase when another token is appended, since the next log-probability is at most zero. Completed translations include an EOS probability, so two different translations can rank in either order. A decoder can use a length penalty when raw likelihood favors outputs that are too short on the development set.
The next figure isolates the multiplication effect by assigning probability 0.9 to every token. It illustrates one reason to test length normalization. It does not predict the length distribution of a trained translation model.
PythonIn [7]Expand
import numpy as np
lengths = np.arange(1, 21)
per_token_prob = 0.9
log_prob = lengths * np.log(per_token_prob)VisualizationOut [8]Expand

Exposure bias
In teacher forcing the prefix comes from the reference. At inference the prefix can contain generated errors, and changing a token can change later predictions. The Teacher Forcing chapter discusses this exposure mismatch.
Bengio et al. (2015) proposed scheduled sampling, which sometimes feeds a generated token in place of the reference during training. If you compare it with teacher forcing, keep the decoding procedure fixed so the training change can be evaluated separately.
Alternative objectives
The likelihood of the reference and the quality score of a generated translation are different objectives. The following examples change what the learning procedure scores.
- Minimum risk training (MRT). Minimize expected sentence-level risk, . For quality metrics where higher is better, a possible risk is the negative metric. Shen et al. (2016) approximate the expectation with a sampled set of candidate translations.
- Sequence-level policy gradient. A reward such as negative risk weights the sampled sequence's log-probability gradient. A baseline that does not depend on the sampled sequence can be subtracted without changing the expected gradient.
- Quality-based filtering and weighting. Estimate pair quality, then set a filtering threshold or give the pairs different training weights. Keep the held-out set fixed when comparing those choices.
- Contrastive and ranking objectives. A ranking loss can reward higher scores for references than for sampled negatives. Choose negatives that test the errors you want the model to distinguish.
Keep a teacher-forced likelihood baseline and test each change on the same held-out translation set. Report both the translation metric and what changed in training or decoding.
Log-likelihood evaluates the probability assigned to reference text. BLEU and COMET evaluate generated outputs under different scoring rules. Report the metric you use to choose a model, alongside the objective used to train it.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!