liveBook · Manning

Cross entropy loss

As explained in Reinforcement Learning from Human Feedback

Cross-entropy is the expected negative log probability assigned by one distribution to outcomes drawn from another distribution.

Cross-entropy measures how much probability a model assigns to outcomes specified by a target distribution. Minimizing it penalizes low model probability on outcomes the target considers likely. The target can be a one-hot label or a soft distribution, such as a teacher model’s next-token probabilities. section 12.3.1

Equation 12.3
\[\label{eq:kd_cross_entropy} H(q,p) = -\sum_z q(z)\log p(z).\]

For target distribution q and model distribution p, cross-entropy averages the negative log probability under p over outcomes weighted by q. section 12.3.1

For a binary correctness label r and predicted probability p of correctness, the loss is −[r log p + (1 − r) log(1 − p)]. A correct label selects the first term; an incorrect label selects the second. The training objective averages this loss over the data. section 5.4

The supervision determines where and against what cross-entropy is computed:

  • Outcome reward modeling: copy a completion’s correct or incorrect label to every completion token. Mask prompt tokens so they do not contribute to the loss. section 5.4
  • Process reward modeling: supervise reasoning steps rather than the whole outcome. A binary objective uses each step’s correctness label; a three-class implementation predicts incorrect, neutral, or correct at step boundaries and masks other tokens. section 5.5
  • Knowledge distillation: use the teacher’s distribution over possible next tokens as a soft target. The student is penalized for assigning low probability to tokens the teacher considers likely. section 12.3.1
hidden = model.lm(
    **inputs,
    output_hidden_states=True,
).hidden_states[-1]       1
logits_per_token = model.head(hidden).squeeze(-1)  # (batch, seq_len)

mask = labels != -100                 2
loss = F.binary_cross_entropy_with_logits(
    logits_per_token[mask], labels[mask].float()
)

A PyTorch outcome-reward loss selects unmasked token logits and labels before applying binary cross-entropy with logits. section 5.4

hidden = model.lm(
    **inputs,
    output_hidden_states=True,
).hidden_states[-1]
logits = model.head(hidden)  # (batch, seq_len, 3)

mask = labels != -100                         1
loss = F.cross_entropy(logits[mask], labels[mask])

For three-class process supervision, cross-entropy is applied to logits and labels at unmasked step boundaries. section 5.5

Cross-entropy also connects likelihood training to divergence minimization: H(q,p) = H(q) + KL(q‖p). When the target distribution q is fixed, its entropy does not change with model parameters, so minimizing cross-entropy and forward KL has the same gradients and minimum. In supervised fine-tuning, the negative log-likelihood over the dataset is the cross-entropy term. section 12.3.1 section 15.2.2

How Reinforcement Learning from Human Feedback covers it

How deep it goes
Defines general and binary objectives and demonstrates masked token-level and step-level loss implementations.
Examples
Uses completion correctness labels, reasoning-step labels, and teacher next-token distributions as targets.
What you can do after
Apply it via PyTorch to masked binary token predictions and multiclass step-boundary predictions.
How it connects
Connects cross-entropy to negative log-likelihood, entropy, and forward KL divergence.

Also explained in

Compare how 19 books explain cross entropy loss →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Reinforcement Learning from Human Feedback. The section links point to the parts of the book it draws on.