Numerically Stable Sigmoid From Scratch

Quick Overview

Write a vectorized NumPy sigmoid that never overflows in exp, even for inputs of -1000 and 1000, without calling scipy. It tests floating-point overflow and underflow, choosing equivalent algebraic forms by sign, and how NumPy evaluates branches when you vectorize them.

Numerically Stable Sigmoid From Scratch

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `stable_sigmoid(z)` for a 1-D `float64` array `z`. Return a new `float64` `np.ndarray` of the same shape holding `1/(1+exp(-z))`, with no `exp` overflow for any finite input. Requirements: - Branch per element by sign: `z >= 0` uses `1/(1+exp(-z))`, `z < 0` uses `exp(z)/(1+exp(z))`. - `exp` must never receive a large positive argument. Underflow to `0.0` is correct, overflow is not. - Do not import or call `scipy.special.expit`. For example: ```python z = np.array([-1000.0, -50.0, -1.0, 0.0, 1.0, 50.0, 1000.0]) stable_sigmoid(z) # [0.0, expit(-50), expit(-1), 0.5, expit(1), expit(50), 1.0] # extreme entries are exactly 0.0 and 1.0 ``` ```hint Vectorizing the branch If you select between the two forms with one vectorized call, check which expressions NumPy evaluates, and on which elements, before it does the selecting. ``` ### Constraints & Assumptions - `z` is a 1-D `float64` array of finite values, which can be as large in magnitude as the `-1000.0` and `1000.0` in the example. - The grader runs with `warnings.simplefilter('error')`, so any overflow warning from `exp` fails the test. ### What a Strong Answer Covers - Which inputs make the naive formula overflow, and why. - The two algebraically equivalent forms and why each keeps the `exp` argument at or below 0 on the elements it handles. - A vectorized implementation that never evaluates an unsafe expression on any element, including the trap of evaluating both branches on the whole array. - The exact outputs at the extremes and at 0, and the time and space complexity. ### Follow-up Questions - Why does naive `1/(1+np.exp(-z))` fail under `warnings.simplefilter('error')`? - How would you extend this to a 2-D input without a Python loop? - Logistic regression's loss needs `log(sigmoid(z))`. How would you compute it stably for very negative `z`?

Overview: Write a vectorized NumPy sigmoid that never overflows in exp, even for inputs of -1000 and 1000, without calling scipy. It tests floating-point overflow and underflow, choosing equivalent algebraic forms by sign, and how NumPy evaluates branches when you vectorize them.

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

Implement stable_sigmoid(z) for a 1-D float64 array z. Return a new float64 np.ndarray of the same shape holding 1/(1+exp(-z)), with no exp overflow for any finite input.

Requirements:

  • Branch per element by sign: z >= 0 uses 1/(1+exp(-z)) , z < 0 uses exp(z)/(1+exp(z)) .
  • exp must never receive a large positive argument. Underflow to 0.0 is correct, overflow is not.
  • Do not import or call scipy.special.expit .

For example:

z = np.array([-1000.0, -50.0, -1.0, 0.0, 1.0, 50.0, 1000.0])
stable_sigmoid(z)
# [0.0, expit(-50), expit(-1), 0.5, expit(1), expit(50), 1.0]
# extreme entries are exactly 0.0 and 1.0

Constraints & Assumptions

  • z is a 1-D float64 array of finite values, which can be as large in magnitude as the -1000.0 and 1000.0 in the example.
  • The grader runs with warnings.simplefilter('error') , so any overflow warning from exp fails the test.

What a Strong Answer Covers Guidance

  • Which inputs make the naive formula overflow, and why.
  • The two algebraically equivalent forms and why each keeps the exp argument at or below 0 on the elements it handles.
  • A vectorized implementation that never evaluates an unsafe expression on any element, including the trap of evaluating both branches on the whole array.
  • The exact outputs at the extremes and at 0, and the time and space complexity.

Follow-up Questions Guidance

  • Why does naive 1/(1+np.exp(-z)) fail under warnings.simplefilter('error') ?
  • How would you extend this to a 2-D input without a Python loop?
  • Logistic regression's loss needs log(sigmoid(z)) . How would you compute it stably for very negative z ?