Premium problem127. Cross-Entropy Loss Straight from Logits

Medium Locked

Return the scalar mean categorical cross-entropy:

  • Z holds logits of shape (n, k)
  • y holds integer labels of shape (n,)
  • compute it via log-sum-exp, so it stays finite when a correct-class probability underflows to 0

Input

Z =
[[1. 2. 3.]
 [3. 1. 0.]]
y = [2 0]

Output

0.2887259920003331

Premium problem

This one's part of Premium. Unlock the full NumPy track plus every other premium problem on the site.

Implement solve(...)