Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-337

Cross-Entropy Loss (One-Hot)

Problem

Compute the mean cross-entropy loss in nats for one-hot labelled samples. probs is a list of probability rows and labels holds the index of the correct class for each row. Each selected probability is floored at 1e-12, and the function returns 0.0 for empty input.

Hint

Average -ln(p[label]) over all samples.

Test cases

case 1
in: [[[0.8,0.2],[0.1,0.9]],[0,1]]
expected: 0.164252033486018
case 2
in: [[[0.5,0.5],[0.25,0.75]],[1,0]]
expected: 1.0397207708399179
case 3
in: [[[1,0]],[0]]
expected: 0
case 4
in: [[],[]]
expected: 0
Starter codePython
import math
def cross_entropy_one_hot(probs, labels):
    # Your code here
    pass

4 test cases · run your code against them in the editor