Premium problem90. Negative Log-Likelihood

Medium Locked

log_probs is (B, C) and targets is (B,) of class indices. Return the mean negative log-likelihood: pick each row's entry at its target class, negate, and average.

Do not call nn.NLLLoss or F.nll_loss. The whole operation is a gather followed by a mean.

Input

log_probs =
tensor([[-0.1000, -2.3000, -3.0000],
        [-1.2000, -0.5000, -2.0000]])
targets = tensor([0, 1])

Output

tensor(0.3000)

Premium problem

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

Implement solve(...)