Skip to content
mlmentorship

Cross-Entropy From Logits

Compute mean multiclass cross-entropy from logits and integer labels.

Published · 3 min read ·Role-specific ·Intermediate

30-second answer map

Visual first · depth when needed

Compute stable log-normalizers per example, subtract the indexed correct logits, then average example losses.

Preparing the visual…

ML breadth · active recall

Practice before you read

8 minutes. Explain the mechanism, why it works, when it fails, and one alternative.

How practice works

ML breadth · closed-book attempt

Cross-Entropy From Logits

Explain the mechanism, why it works, when it fails, and one alternative.

08:00recommended time

Closing or reloading clears the scratchpad. Only score, weak rubric dimensions, attempt count, and retry date can be stored locally.

Compute mean multiclass cross-entropy from logits and integer labels.

Start with the concrete trace below. It shows the state the algorithm must carry as it runs.

Problem trace

Cross-Entropy From Logits: Compute stable log-normalizers per example, subtract the indexed correct logits, then average example losses.

Input and goalCompute mean multiclass cross-entropy from logits and integer labels.
Read logits and integer labelsUse logits [[2,1,0],[0,1,2]] with labels [0,1]. Each label selects one class in its own row.
exampleclass 0class 1class 2label
02100
10121

Recognize it
Use it when multiclass logits and integer labels require negative log likelihood without explicitly forming potentially unstable probabilities.
Keep true
For each row, log_normalizer minus the shifted selected logit equals negative log probability of the label; max shifting cancels from both terms.
Reuse it
For stable classification losses, compute log-normalization in score space, gather the target score by aligned row indices, and reduce only after producing per-example losses.
Read it this way: Use logits [[2,1,0],[0,1,2]] with labels [0,1]. Each label selects one class in its own row. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.

Pattern: Stable log-softmax plus indexed selection.

Simple idea: Subtract each row maximum. Compute the log normalizer for each example. Subtract the correct-class logit, then average.

import numpy as np

def cross_entropy(logits: np.ndarray, labels: np.ndarray) -> float:
   shifted = logits - np.max(logits, axis=1, keepdims=True)
   log_normalizer = np.log(np.sum(np.exp(shifted), axis=1))
   correct_logits = shifted[np.arange(len(labels)), labels]
   return float(np.mean(log_normalizer - correct_logits))

Cost: time and output-sized temporary space.