Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

ML coding · Numerical stability

Cross-entropy loss and its gradient from logits

Medium · Target 15 minutes · Python and NumPy

You are given a batch of logits logits of shape (N, C) and integer class labels labels of length N, each in 0…C−1. Return a list [loss, grad] where loss is the mean over the batch of −log softmax(logitsn)labelsn, and grad is the N × C matrix ∂loss/∂logits.

The function must stay finite for logits of any size.

Examples

Input logits = [[2, 1, 0]], labels = [0]

Output [0.4076, [[-0.3348, 0.2447, 0.09]]]

p = [0.665, 0.245, 0.090]; the gradient is p minus the one-hot label.

Input logits = [[0, 0], [0, 0]], labels = [0, 1]

Output [0.6931, [[-0.25, 0.25], [0.25, -0.25]]]

Uniform predictions: the loss is log 2, and the 1/N shows in the gradient.

Input logits = [[1000, 0]], labels = [1]

Output [1000, [[1, -1]]]

Confidently wrong. The loss is large and finite.

Constraints

  • 1 ≤ N ≤ 512, 2 ≤ C ≤ 1000
  • |logits[n][c]| ≤ 10⁴
  • Checked to a relative tolerance of 10⁻⁹

Hints

Hint 1

Compute log-probabilities, not probabilities. log softmax(z)ᵢ = zᵢ − logsumexp(z).

Hint 2

Use fancy indexing to pick each row's correct class: logp[np.arange(N), labels].

Hint 3

The gradient of the mean loss with respect to the logits is (softmax − one_hot) / N. You already have softmax as exp(logp).

Where candidates go wrong

  • Taking np.log of the softmax output. For a confidently wrong prediction the probability underflows to 0 and the loss becomes inf.
  • Forgetting the 1/N in the gradient while using the mean in the loss.
  • Building the one-hot matrix with a Python loop. Subtract 1 at [np.arange(N), labels] instead.
  • Modifying the caller's array in place when shifting by the row maximum.
Console⌘/Ctrl + Enter runs

Python runs in your browser with NumPy. The first run downloads the runtime, which takes a few seconds.


Why interviewers ask it

Every classifier is trained on this loss. The question tests three things at once: whether you work in log space, whether you can vectorise over a batch without a loop, and whether you know the gradient by heart. Interviewers often ask for the gradient first and the code second.

The theory behind it is in the lesson Numerically stable softmax and log-sum-exp.


Keep going