PythiloLesson 11 of 15 · progress kept in this browser

Softmax and cross-entropy

A classifier outputs one score per class, called logits. Softmax turns them into probabilities: exponentiate each, divide by the sum. Cross-entropy is the negative log of the probability given to the correct class.

Exponentials overflow: exp(1000) is infinite. Softmax does not change when you subtract the same number from every logit, so subtract the largest and the biggest exponent becomes exp(0) = 1. For the loss, use the log-sum-exp form: -log p[t] equals logsumexp(z) - z[t], which never takes the log of a zero.

import numpy as np

z = np.array([1000.0, 999.0, 0.0])
e = np.exp(z - z.max())
p = e / e.sum()
print(p.round(4))
lse = z.max() + np.log(e.sum())
print(lse - z[1])  # loss for class 1

The gradient of this loss with respect to the logits is the simplest in deep learning: p minus the one-hot vector of the target. It is large where the model is confidently wrong and 0 where it is perfectly right.

Your task

Write softmax(z) for a 1-D array, stable for large logits, cross_entropy(z, target) returning -log softmax(z)[target] as a float through log-sum-exp, and cross_entropy_grad(z, target) returning its gradient with respect to z.

The first run loads NumPy into this browser.