BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Losses1 of 12 in topic

228. Binary cross-entropy straight from logits

EasyLosses
Asked atMicrosoft

Applying sigmoid and then log loss in two steps is unstable. Frameworks fuse them into one stable formula that works directly on logits:

loss = max(z, 0) - z * y + log(1 + exp(-|z|))

Write bce_with_logits(z, y) that returns the mean loss over all elements as a float. z are logits and y are 0/1 targets.

Functiondef bce_with_logits(z, y)
Play with it

Binary cross-entropy on a logit

With label 1 the loss is softplus(−z); with label 0 it is softplus(z). Both grow linearly when the logit is confidently wrong.

0246-6-4-20246logit z
logit z
0.00
BCE, y = 1
0.6931
BCE, y = 0
0.6931
slope of BCE, y = 1
-0.5
BCE, y = 1BCE, y = 0

Example 1

Input: z = [0.0, 2.0, -1.0], y = [1, 1, 0]

Output: 0.377779

Example 2

Input: z = [800.0, -800.0], y = [0, 1]

Output: 800.0

Example 3

Input: z = [50.0], y = [1]

Output: 1.92875e-22

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 3 tests to be accepted.
Hint 1

Never compute the sigmoid first: the given formula works straight on the logits and avoids overflow.

Hint 2

Convert z and y to float arrays, compute max(z, 0) - z*y + log(1 + exp(-|z|)) elementwise, and return the mean.

Hint 3

Use np.log1p(np.exp(-np.abs(z))) for the last term, because exp(-|z|) is always at most 1, then wrap the mean in float(...).

Understand the idea

Binary cross-entropy measures how wrong a probability is for a 0/1 target. Doing sigmoid first and then log can produce log(0) and return inf or nan for confident logits. The fused form computes the same number straight from the logit z and never passes a large positive number to exp.

Worked example

z = 2, y = 1. Fused: max(2, 0) - 2 x 1 + log(1 + exp(-2)) = 0 + log(1.135) = 0.127, which equals -log(sigmoid(2)) = -log(0.881). For z = -2, y = 1: 0 + 2 + 0.127 = 2.127, a big loss for a confident wrong answer. For z = 1000, y = 0, the two-step version breaks, but the fused one gives 1000 - 0 + log(1 + ~0) = 1000.

loss = max(z, 0) - z y + log(1 + exp(-|z|)), averaged over all elements

PyTorch's BCEWithLogitsLoss uses this form, and it trains binary classifiers and reward heads.

Topics
Losses
Companies1
  • Microsoft the topic appears in interview reports Microsoft Interview Prep: Questions, Process & Study Plan

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Cross-entropy with label smoothing from raw logitsEasy
  • Compute the multiclass hinge lossEasy
  • Compute the soft Dice lossEasy
  • Compute smooth L1 box regression loss over positive anchorsEasy
All Losses problems
Report
228 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def bce_with_logits(z, y):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 3 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS