Sigmoid Derivative

Quick Overview

Implement a NumPy function that returns the derivative of the logistic sigmoid for an array of raw scores. It tests the closed-form identity, a sigmoid that does not overflow, and keeping the result exactly symmetric with full precision when scores are large.

Sigmoid Derivative

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `sigmoid_derivative(z)`. Given a 1-D float64 array `z` of raw scores (not probabilities), return a new float64 `np.ndarray` of the same shape where element `i` equals `sigma(z[i]) * (1 - sigma(z[i]))`, with `sigma` the standard sigmoid computed internally. The function must meet two requirements: - Do not mutate the input `z`. - The result peaks at exactly `0.25` at `z = 0` and is symmetric about `z = 0`. ```python z = np.array([-2.0, -1.0, 0.0, 1.0, 2.0]) sigmoid_derivative(z) # [0.10499358540350662, 0.19661193324148185, 0.25, # 0.19661193324148185, 0.10499358540350662] ``` ```hint Try large scores Evaluate your function at `z = 40` and at `z = -40` and compare the two results. The symmetry requirement applies there too. ``` ### Constraints & Assumptions - `z` is a 1-D float64 array of raw scores and can hold any finite values, including scores large in magnitude. - Outputs are compared to float64 precision, for example with `np.allclose`, except for the exact peak at `z = 0`. ### What a Strong Answer Covers - Applies the sigmoid to the raw scores before using the derivative identity, and shows where the identity comes from. - Computes the sigmoid without overflow or warnings for large negative scores. - Keeps the peak exactly 0.25 and the symmetry intact for large `|z|`, and explains where the straightforward formula loses precision. - Returns a new array, leaves `z` untouched, and states the cost. ### Follow-up Questions - How would you keep the sigmoid numerically stable for large negative `z`? - Why express the derivative in terms of `sigma` instead of recomputing exponentials? - The derivative never exceeds 0.25. What does that mean for training a deep network with sigmoid hidden layers?

Overview: Implement a NumPy function that returns the derivative of the logistic sigmoid for an array of raw scores. It tests the closed-form identity, a sigmoid that does not overflow, and keeping the result exactly symmetric with full precision when scores are large.

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

Implement sigmoid_derivative(z). Given a 1-D float64 array z of raw scores (not probabilities), return a new float64 np.ndarray of the same shape where element i equals sigma(z[i]) * (1 - sigma(z[i])), with sigma the standard sigmoid computed internally.

The function must meet two requirements:

  • Do not mutate the input z .
  • The result peaks at exactly 0.25 at z = 0 and is symmetric about z = 0 .
z = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
sigmoid_derivative(z)
# [0.10499358540350662, 0.19661193324148185, 0.25,
#  0.19661193324148185, 0.10499358540350662]

Constraints & Assumptions

  • z is a 1-D float64 array of raw scores and can hold any finite values, including scores large in magnitude.
  • Outputs are compared to float64 precision, for example with np.allclose , except for the exact peak at z = 0 .

What a Strong Answer Covers Guidance

  • Applies the sigmoid to the raw scores before using the derivative identity, and shows where the identity comes from.
  • Computes the sigmoid without overflow or warnings for large negative scores.
  • Keeps the peak exactly 0.25 and the symmetry intact for large |z| , and explains where the straightforward formula loses precision.
  • Returns a new array, leaves z untouched, and states the cost.

Follow-up Questions Guidance

  • How would you keep the sigmoid numerically stable for large negative z ?
  • Why express the derivative in terms of sigma instead of recomputing exponentials?
  • The derivative never exceeds 0.25. What does that mean for training a deep network with sigmoid hidden layers?