Stable Sigmoid Derivative

Quick Overview

Implement the derivative of the logistic sigmoid in NumPy so it stays finite and raises no warnings for any finite input. It tests where exp overflows, how a two-branch sigmoid avoids it, and how the formula behaves in the saturated tails.

Stable Sigmoid Derivative

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `stable_sigmoid_derivative(z)`. Given a 1-D `float64` NumPy array `z`, return a new `float64` array of the same shape holding `sigma(z)*(1-sigma(z))`, with no overflow and no `nan` for any finite input. The function must: - Compute `sigma` yourself with a two-branch stable formulation, then form `s*(1-s)`. Do not call `scipy.special.expit`. - Return exactly `0.0` in the saturated tails. - Satisfy `np.isfinite(out).all()` for every finite input. - Emit no warnings, since grading wraps the call in `warnings.simplefilter('error')`. Example (values match to floating-point tolerance): ```python z = np.array([-1000.0, -50.0, -1.0, 0.0, 1.0, 50.0, 1000.0]) out = stable_sigmoid_derivative(z) # out == [0.0, s*(1-s) at -50, 0.19661193324148185, 0.25, # 0.19661193324148185, s*(1-s) at 50, 0.0] assert np.isfinite(out).all() ``` ```hint Find the overflow Work out which inputs make the one-line formula `1/(1+exp(-z))` pass a large positive number to `exp`. For each formula you try instead, check the sign of the exponent over the inputs it will receive. ``` ```hint Look at the tails Ask what `s` rounds to in float64 when `|z|` is large, and what that makes `1 - s` and the product. ``` ### Constraints & Assumptions - `z` is a 1-D `float64` array and every element is finite. - Grading wraps the call in `warnings.simplefilter('error')`, so any NumPy RuntimeWarning (overflow, invalid value) becomes an exception. Under NumPy's default error settings, underflow to `0.0` does not warn. - `scipy.special.expit` may not be used. - The function returns a new array and leaves `z` unchanged. ### What a Strong Answer Covers - Why the textbook sigmoid and derivative formulas overflow or produce `nan` for large `|z|`, and why choosing values with `np.where` does not stop the warning. - A two-branch sigmoid in which `exp` only receives non-positive arguments, each branch evaluated on its own masked elements. - How `s*(1-s)` behaves in the tails: exactly 0.0 at saturation, plus the precision lost in `1 - s` for large positive `z`. - A check of the output against the example, and the time and space cost. ### Follow-up Questions - Why does `np.where(cond, a, b)` still emit an overflow warning even when the bad branch isn't selected? - How can you compute the whole array with a single `exp` evaluation and no branching? - When you train logistic regression with log loss, where does this derivative appear in the gradient, and when does it matter on its own?

Overview: Implement the derivative of the logistic sigmoid in NumPy so it stays finite and raises no warnings for any finite input. It tests where exp overflows, how a two-branch sigmoid avoids it, and how the formula behaves in the saturated tails.

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

Implement stable_sigmoid_derivative(z). Given a 1-D float64 NumPy array z, return a new float64 array of the same shape holding sigma(z)*(1-sigma(z)), with no overflow and no nan for any finite input.

The function must:

  • Compute sigma yourself with a two-branch stable formulation, then form s*(1-s) . Do not call scipy.special.expit .
  • Return exactly 0.0 in the saturated tails.
  • Satisfy np.isfinite(out).all() for every finite input.
  • Emit no warnings, since grading wraps the call in warnings.simplefilter('error') .

Example (values match to floating-point tolerance):

z = np.array([-1000.0, -50.0, -1.0, 0.0, 1.0, 50.0, 1000.0])
out = stable_sigmoid_derivative(z)
# out == [0.0, s*(1-s) at -50, 0.19661193324148185, 0.25,
#         0.19661193324148185, s*(1-s) at 50, 0.0]
assert np.isfinite(out).all()

Constraints & Assumptions

  • z is a 1-D float64 array and every element is finite.
  • Grading wraps the call in warnings.simplefilter('error') , so any NumPy RuntimeWarning (overflow, invalid value) becomes an exception. Under NumPy's default error settings, underflow to 0.0 does not warn.
  • scipy.special.expit may not be used.
  • The function returns a new array and leaves z unchanged.

What a Strong Answer Covers Guidance

  • Why the textbook sigmoid and derivative formulas overflow or produce nan for large |z| , and why choosing values with np.where does not stop the warning.
  • A two-branch sigmoid in which exp only receives non-positive arguments, each branch evaluated on its own masked elements.
  • How s*(1-s) behaves in the tails: exactly 0.0 at saturation, plus the precision lost in 1 - s for large positive z .
  • A check of the output against the example, and the time and space cost.

Follow-up Questions Guidance

  • Why does np.where(cond, a, b) still emit an overflow warning even when the bad branch isn't selected?
  • How can you compute the whole array with a single exp evaluation and no branching?
  • When you train logistic regression with log loss, where does this derivative appear in the gradient, and when does it matter on its own?