Why is it numerically safer to compute cross-entropy from logits than from probabilities?

Quick Overview

Asks why deep learning frameworks compute cross-entropy from raw logits and what goes wrong when the loss starts from probabilities. Tests understanding of floating-point overflow and underflow, the log-sum-exp trick, stable binary cross-entropy and gradient behavior.

Why is it numerically safer to compute cross-entropy from logits than from probabilities?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Why is it numerically safer to compute cross-entropy from logits than from probabilities? Cover both the multi-class softmax case and the binary sigmoid case. ```hint Push the inputs to the extremes Try a logit vector with one very large entry, and a confidently wrong prediction. What value does each intermediate quantity take in float32? ``` ```hint Look for an identity Write the log of a softmax output algebraically. Can it be computed without ever forming the probability itself? ``` ### What a Strong Answer Covers - The concrete floating-point failures on the probability path: overflow, underflow to zero, sigmoid saturation - The log-sum-exp identity with the max shift, and why it prevents overflow - A stable form of binary cross-entropy computed from a logit - How gradients behave on each path, including the effect of clipping probabilities with an epsilon - How to call the fused losses correctly in a framework ### Follow-up Questions - Why does subtracting the maximum logit leave the softmax output unchanged? - What changes when training in float16 mixed precision? - If the serving system needs probabilities, how do you keep training stable? - Why can a model trained on epsilon-clipped probabilities stop learning from its worst-misclassified examples?

Overview: Asks why deep learning frameworks compute cross-entropy from raw logits and what goes wrong when the loss starts from probabilities. Tests understanding of floating-point overflow and underflow, the log-sum-exp trick, stable binary cross-entropy and gradient behavior.

Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Why is it numerically safer to compute cross-entropy from logits than from probabilities?

Cover both the multi-class softmax case and the binary sigmoid case.

What a Strong Answer Covers Guidance

  • The concrete floating-point failures on the probability path: overflow, underflow to zero, sigmoid saturation
  • The log-sum-exp identity with the max shift, and why it prevents overflow
  • A stable form of binary cross-entropy computed from a logit
  • How gradients behave on each path, including the effect of clipping probabilities with an epsilon
  • How to call the fused losses correctly in a framework

Follow-up Questions Guidance

  • Why does subtracting the maximum logit leave the softmax output unchanged?
  • What changes when training in float16 mixed precision?
  • If the serving system needs probabilities, how do you keep training stable?
  • Why can a model trained on epsilon-clipped probabilities stop learning from its worst-misclassified examples?