Implement a numerically stable sigmoid that preserves float32 and float64 dtypes
Quick Overview
Write a NumPy sigmoid that never overflows in exp and returns exactly the input's float32 or float64 dtype. It tests numerically stable formulas, floating-point limits, and the NumPy 2 type promotion rules for scalars.
Implement a numerically stable sigmoid that preserves float32 and float64 dtypes
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `stable_sigmoid_preserve_dtype(z)`. Given a `float32` or `float64` array, return a new array of the same shape holding the sigmoid, with `dtype` exactly equal to the input's. Computation runs inside `np.errstate(over='raise')`.
The function must follow these rules:
- Use the sign-branched form: `z >= 0` uses `1 / (1 + exp(-z))`, `z < 0` uses `exp(z) / (1 + exp(z))` (exponent always `<= 0`).
- No `exp` overflow for either dtype (`float32` overflows near 88).
- No upcasting: an explicit `np.float64(1.0)` upcasts `float32`, while Python `1.0` or `np.float32(1.0)` preserves it.
- Do not use `scipy.special.expit`.
Example:
```python
z = np.array([-1000.0, 0.0, 1000.0], dtype=np.float32)
out = stable_sigmoid_preserve_dtype(z)
out # array([0. , 0.5, 1. ], dtype=float32)
out.dtype # float32
z64 = z.astype(np.float64)
stable_sigmoid_preserve_dtype(z64).dtype # float64
```
```hint Scalar promotion
Look at how NumPy 2 promotion treats a Python float compared with a NumPy `float64` scalar when either one meets a `float32` array.
```
```hint Both branches at once
If you evaluate both branches on every element, for example with `np.where`, make sure neither branch can overflow on the elements it does not end up selecting.
```
### Constraints & Assumptions
- NumPy 2 (NEP 50 promotion) is assumed.
- Input dtypes are `float32` or `float64` only; input validation beyond those two is out of scope.
- The output is a new array; the input must not be modified.
### What a Strong Answer Covers
- Why the chosen formula keeps every `exp` argument at or below zero for both dtypes, so `over='raise'` never fires.
- Where the output dtype comes from: allocation, intermediates and the NEP 50 rules for scalars.
- A vectorized implementation with no Python-level loop, checked against the example.
- Behavior at the extremes and for special values such as `inf`, `-inf` and NaN.
### Follow-up Questions
- Why use the sign-branched form when you could clip `z` before calling `exp`?
- How would you implement the branch without a Python-level `if` so it stays vectorized?
- How would you compute `log(sigmoid(z))` stably for a log-loss?
Overview: Write a NumPy sigmoid that never overflows in exp and returns exactly the input's float32 or float64 dtype. It tests numerically stable formulas, floating-point limits, and the NumPy 2 type promotion rules for scalars.
Implement a numerically stable sigmoid that preserves float32 and float64 dtypes
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0
Implement stable_sigmoid_preserve_dtype(z). Given a float32 or float64 array, return a new array of the same shape holding the sigmoid, with dtype exactly equal to the input's. Computation runs inside np.errstate(over='raise').
The function must follow these rules:
Use the sign-branched form:
z >= 0
uses
1 / (1 + exp(-z))
,
z < 0
uses
exp(z) / (1 + exp(z))
(exponent always
<= 0
).
No
exp
overflow for either dtype (
float32
overflows near 88).
No upcasting: an explicit
np.float64(1.0)
upcasts
float32
, while Python
1.0
or
np.float32(1.0)
preserves it.
Do not use
scipy.special.expit
.
Example:
z = np.array([-1000.0, 0.0, 1000.0], dtype=np.float32)
out = stable_sigmoid_preserve_dtype(z)
out # array([0. , 0.5, 1. ], dtype=float32)
out.dtype # float32
z64 = z.astype(np.float64)
stable_sigmoid_preserve_dtype(z64).dtype # float64
Constraints & Assumptions
NumPy 2 (NEP 50 promotion) is assumed.
Input dtypes are
float32
or
float64
only; input validation beyond those two is out of scope.
The output is a new array; the input must not be modified.
What a Strong Answer Covers Guidance
Why the chosen formula keeps every
exp
argument at or below zero for both dtypes, so
over='raise'
never fires.
Where the output dtype comes from: allocation, intermediates and the NEP 50 rules for scalars.
A vectorized implementation with no Python-level loop, checked against the example.
Behavior at the extremes and for special values such as
inf
,
-inf
and NaN.
Follow-up Questions Guidance
Why use the sign-branched form when you could clip
z
before calling
exp
?
How would you implement the branch without a Python-level
if
so it stays vectorized?
How would you compute
log(sigmoid(z))
stably for a log-loss?