14/32
Part 3 of 6 · The loss and its gradientLearn and do

Take the log of a sum, never the log of a probability

Subtracting each row's max keeps np.exp finite. The log has a trap of its own.

z = np.array([0.0, -40000.0, 3.0])
z = z - z.max()
probs = np.exp(z) / np.exp(z).sum()
print(probs)
print(-np.log(probs[1]))
log_probs = z - np.log(np.exp(z).sum())
print(-log_probs[1])
Predict

The label is class 1, whose logit sits 40,003 below the top. Its probability rounds to 0.0. What do -np.log(probs[1]) and the log-space line give?

Pick one to see what it prints.
Pick an answer above to see the rest