Premium problem54. Binary Cross-Entropy from Logits

Medium Locked

Given raw (pre-sigmoid) logits and binary target (0s and 1s), return the mean binary cross-entropy loss: for each element, `-(target * log(p) + (1 - target)

  • log(1 - p))wherep = sigmoid(logit), averaged over all elements. Compute it in a numerically stable way directly from the logits rather than from sigmoid(logits)`.

Input

logits = tensor([ 2.0000, -1.0000,  0.5000, -3.0000])
target = tensor([1., 0., 1., 0.])

Output

tensor(0.2407)

Premium problem

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

Implement solve(...)