Compute a Numerically Stable log_sigmoid in NumPy With logaddexp

Quick Overview

Implement log(sigmoid(z)) in NumPy so it returns finite, accurate values for inputs as extreme as 1000 in magnitude. The question tests rewriting a formula in log space, floating-point underflow and overflow, and the link to stable binary cross-entropy computed from logits.

Compute a Numerically Stable log_sigmoid in NumPy With logaddexp

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `log_sigmoid(z)`, which computes `log(sigmoid(z))`. Given a 1-D `float64` NumPy array, return a new `float64` array of the same shape with no `-inf` and no overflow for any finite input. Requirements: - Compute `-np.logaddexp(0, -z)` element-wise. Do not materialize the raw sigmoid. - `np.isfinite(out).all()` holds for every finite input, including large-magnitude `z` (large negative `z` returns approximately `z`). Example: ```python z = np.array([-1000.0, -50.0, -1.0, 0.0, 1.0, 50.0, 1000.0]) log_sigmoid(z) # [-1000.0, -50.0 (approx), -1.3132616875182228, -0.6931471805599453, # -0.3132616875182228, -1.9287498479639178e-22, 0.0] # log_sigmoid(0) == -log(2) ``` ```hint Find the failing step Write $\log \sigma(z)$ as $-\log(1 + e^{-z})$, then evaluate the naive version step by step at $z = -1000$ and at $z = 50$ in float64 to see where information is lost. ``` ### Constraints & Assumptions - The input is a 1-D `float64` array of finite values, which can be as large as `±1000` or beyond. - The output has the same shape and dtype, and every entry must be finite. ### What a Strong Answer Covers - The algebra that turns `log(sigmoid(z))` into `-logaddexp(0, -z)`. - Why the naive formula returns `-inf` for very negative `z` and loses the tiny negative values for large positive `z`. - A vectorized `float64` implementation with no loop and no explicit sigmoid. - The behavior at the extremes: about `z` for very negative `z`, about `-exp(-z)` for large positive `z`, and `-log(2)` at zero. ### Follow-up Questions - How would you also compute a stable gradient of `log_sigmoid`? - How does this generalize to a numerically stable binary cross-entropy loss? - Why does `np.log(scipy.special.expit(z))` still fail, even though `expit` itself is stable?

Overview: Implement log(sigmoid(z)) in NumPy so it returns finite, accurate values for inputs as extreme as 1000 in magnitude. The question tests rewriting a formula in log space, floating-point underflow and overflow, and the link to stable binary cross-entropy computed from logits.

General logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement log_sigmoid(z), which computes log(sigmoid(z)). Given a 1-D float64 NumPy array, return a new float64 array of the same shape with no -inf and no overflow for any finite input.

Requirements:

  • Compute -np.logaddexp(0, -z) element-wise. Do not materialize the raw sigmoid.
  • np.isfinite(out).all() holds for every finite input, including large-magnitude z (large negative z returns approximately z ).

Example:

z = np.array([-1000.0, -50.0, -1.0, 0.0, 1.0, 50.0, 1000.0])
log_sigmoid(z)
# [-1000.0, -50.0 (approx), -1.3132616875182228, -0.6931471805599453,
#  -0.3132616875182228, -1.9287498479639178e-22, 0.0]
# log_sigmoid(0) == -log(2)

Constraints & Assumptions

  • The input is a 1-D float64 array of finite values, which can be as large as ±1000 or beyond.
  • The output has the same shape and dtype, and every entry must be finite.

What a Strong Answer Covers Guidance

  • The algebra that turns log(sigmoid(z)) into -logaddexp(0, -z) .
  • Why the naive formula returns -inf for very negative z and loses the tiny negative values for large positive z .
  • A vectorized float64 implementation with no loop and no explicit sigmoid.
  • The behavior at the extremes: about z for very negative z , about -exp(-z) for large positive z , and -log(2) at zero.

Follow-up Questions Guidance

  • How would you also compute a stable gradient of log_sigmoid ?
  • How does this generalize to a numerically stable binary cross-entropy loss?
  • Why does np.log(scipy.special.expit(z)) still fail, even though expit itself is stable?