Exponentiating large logits overflows (inf) and tiny ones underflow to zero, producing NaN losses.
Subtracting the maximum logit before exponentiating leaves the result unchanged mathematically but keeps values in a safe range.
Similarly, compute log-softmax directly (log-sum-exp trick) instead of taking log of softmax.
Loading…