Softmax Cross-Entropy & Its Gradient

medium
Unit 16 · Object Detection0/2 to milestone

Implement cross_entropy(logits, targets) returning (loss, dlogits):

  • logits: shape (n, C)
  • targets: integer class indices, shape (n,)
  • loss: the mean negative log-likelihood (a Python float)
  • dlogits: gradient of the mean loss w.r.t. logits, shape (n, C)

The beautiful result: ∂L∂z=1n(softmax(z)−onehot(y))\frac{\partial L}{\partial z} = \frac{1}{n}(\text{softmax}(z) - \text{onehot}(y)).

Use log-sum-exp for numerical stability.

#loss#classification#backprop2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Softmax Cross-Entropy & Its Gradient · TechGradify