Before A Fix for Softmax and Cross-Entropy, two papers had noticed that cross-entropy misbehaves at large logit margins and proposed workarounds. Prieto et al. replace the exponential inside softmax with a function that grows only linearly (StableMax), so the target probability approaches one slowly and the gradient never rounds away. Liu et al. keep the standard gradient but subtract its mean so it sums to zero (zero-sum projection). The paper compares both against the fixed cross-entropy. Implement them so the comparison can be reproduced.
stablemax(z)
z is a float array of shape (B, K). Apply for and for elementwise and normalise each row:(B, K) whose rows sum to one; emit no numpy warnings for any finite input.stablemax_cross_entropy(z, t)
t is an integer array of shape (B,) of target indices. Return the per-example losses as a float array of shape (B,) (no averaging).zero_sum_projection(g)
g is a float array of shape (B, K) of per-example logit gradients. Subtract each row's mean from that row and return the result, shape (B, K), so every row sums to zero. Do not modify the input.numpy.where evaluates both branches, so write the negative branch's denominator so that it cannot be zero or negative on the positive side.