Binary Cross-Entropy with Logits

medium
Unit 14 · Losses & CNNs0/3 to milestone

Computing sigmoid then log loses precision and can produce log(0). The fused, stable form for logit zz and target yy is

ℓ=max⁡(z,0)−zy+log⁡(1+e−∣z∣)\ell = \max(z, 0) - z y + \log(1 + e^{-|z|})

Implement bce_with_logits(z, y, pos_weight=1.0) returning the mean loss, where pos_weight multiplies the positive term (as in PyTorch): ℓ=−[w ylog⁡σ(z)+(1−y)log⁡(1−σ(z))]\ell = -[w\,y\log\sigma(z) + (1-y)\log(1-\sigma(z))]. Also implement bce_with_logits_grad(z, y, pos_weight=1.0), the gradient of the mean loss with respect to z.

#losses#numerics2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Binary Cross-Entropy with Logits · TechGradify