Cross-Entropy
logits has one row per example and one column per class, and labels holds the right class of each example. Return the cross-entropy loss: take the softmax of each row, then minus the logarithm of the share it gives the right class, and return the mean of that over the examples.
Example 1
logits [[0.0,0.0]]
labels [0]
returns 0.693147
Two equal scores give each class a share of one half, and minus the logarithm of one half is 0.693.
Constraints
- 1 to 5 examples, 2 to 4 classes
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More PyTorch problems
- Fit a LineMedium
- How Often RightMedium
- In BatchesMedium
- Many OutputsMedium
- Mean Squared ErrorMedium
- One Step DownhillMedium
- Two LayersMedium
- Two ParametersMedium