LeNet-5's Loss: From the Correct Class Penalty to the MAP Criterion
LeNet-5's output layer gives a penalty yi≥0 for every class (a squared distance, so smaller is better). Compute the two training criteria the paper writes for these penalties, and the gradient of the second one.
Implement lenet_losses(penalties, labels, j).
Inputs
penalties: array of shape (P,C), the penalty yi(Zp) of class i for pattern p.
labels: integer array of shape (P,), the correct class Dp of each pattern.
j: a positive float, the constant of the "rubbish" class.
Output: tuple (e_mse, e_map, grad):
e_mse, the mean penalty of the correct class:
EMSE=P1∑p=1PyDp(Zp).
e_map, the MAP criterion:
EMAP=P1∑p=1P(yDp(Zp)+log(e−j+∑i=1Ce−yi(Zp))).
grad: array (P,C), ∂EMAP/∂yi(Zp).
Notes
- The sum over i includes the correct class.
- Penalties can be in the hundreds or thousands; the result must stay finite.