For a one-hot target the cross-entropy depends on the logits only through the margin : the loss is and its gradient is a scaled vector of the non-target exponentials with the target entry set to minus their sum. Barron et al. (2026) implement it exactly that way so that neither the loss nor the target gradient can round to zero. Implement the batched loss and gradient.
fixed_cross_entropy(z, targets)
z is a float array of shape (N, K) (K >= 2) of logits; targets is an integer array of shape (N,) with the target class of each row (which need not be the row's largest logit).targets[n], compute the margin over the other entries of that row and(loss, grad): loss is the mean of over the batch as a Python float; grad is a float64 array of shape (N, K) whose row is (the per-example gradient, not divided by N).grad must sum to zero to within 1e-12, and loss must be strictly positive whenever is representable, even where the standard cross-entropy returns exactly 0 (for the paper's example row [9, -9, -9] with target 0 the loss is about 3.046e-8).log(1 + exp(-m)) returns 0 for in float64; np.log1p does not.