Premium problem55. Cross-Entropy Loss (Multi-Class)

Medium Locked

logits is a (batch, classes) tensor of raw scores and target is a 1-D tensor of integer class indices. Return the mean cross-entropy loss: convert each row of logits to a probability distribution, then average the negative log-probability assigned to the true class across the batch.

Input

logits =
tensor([[ 2.0000,  0.5000, -1.0000],
        [ 0.1000,  1.5000,  0.2000]])
target = tensor([0, 1])

Output

tensor(0.3297)

Premium problem

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

Implement solve(...)