PythiloLesson 1 of 12 · progress kept in this browser

Practice in Lab: Weight stable multiclass cross-entropy by observations →

Weight stable multiclass cross-entropy by observations

A production metric must match the training objective and the population it summarizes. Class logits are unnormalized scores; treating them as probabilities or exponentiating large absolute values produces meaningless losses. Shift each row before computing its log normalizer, then weight complete observations. A zero-weight observation contributes nothing, but its schema still needs validation. The example uses an extreme score without overflowing. This is an exact small-batch reference for auditing a vectorized implementation.

import math
row = [1000., 999.]
peak = max(row)
loss = math.log(sum(math.exp(x-peak) for x in row)) + peak-row[0]
print(round(loss, 6))
Your task

Return the weighted mean multiclass negative log likelihood. Subtract each row maximum before exponentiation. Require aligned nonempty batches, rectangular logits with at least two classes, finite values, integer class indices, nonnegative finite weights and a positive total weight. The denominator is total observation weight, not batch length.