FunctionRacerProblemsRace

Cross-Entropy

MediumPyTorchPython

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More PyTorch problems