Sigmoid Gradient
Problem Statement
Compute the derivative of the Sigmoid function given the activation output.
Background
The Sigmoid function is defined as: σ(z)=1+e−z1​
A useful property is that its derivative can be computed directly from the output:
- If g=σ(z), then the derivative g′(z)=g×(1−g)
This makes backpropagation efficient since we already have the sigmoid output from the forward pass.
Your Task
Write a function sigmoid_derivative(sigmoid_output) that returns the gradient values for each element.
Output Format
Return a list of floats representing the derivatives. Round each value to 4 decimal places.
Example:
sigmoid_output=[0.5, 0.8]
[0.25, 0.16]
For 0.5: 0.5 × (1 - 0.5) = 0.5×0.5 = 0.25. For 0.8: 0.8 × (1 - 0.8) = 0.8×0.2 = 0.16.
Constraints:
- 0 <= sigmoid_output[i] <= 1 for each element
- List length: 1 to 100 elements
1. Background Knowledge
The sigmoid function is a fundamental activation function in neural networks, defined as: σ(z)=1+e−z1​
It maps any real input z∈R to the range (0,1), making it ideal for binary classification and probability outputs. Key properties include:
- Smooth and differentiable everywhere
- S-shaped (S-curve) with inflection point at z=0, where σ(0)=0.5
- Asymptotic behavior: σ(z)→1 as z→∞, σ(z)→0 as z→−∞
Critical property for backpropagation: The derivative can be computed directly from the output: σ′(z)=σ(z)⋅(1−σ(z))
Proof sketch: Let g = \sigma(z). Then: σ′(z)=dzd​(1+e−z1​)=(1+e−z)2e−z​=1+e−z1​⋅1+e−ze−z​=g⋅(1−g)
This eliminates the need to store z values during the forward pass, saving memory in deep networks.
Prerequisites:
- Basic calculus (chain rule, quotient rule)
- Understanding of neural network backpropagation
- Python list operations and rounding
2. Algorithm Approach
Direct computation using the closed-form derivative formula—no numerical differentiation needed:
- For each element gi​ in the input list, compute gi​×(1−gi​)
- Round result to 4 decimal places
- Return as list
Alternative approaches (not needed here but educational):
- Numerical approximation: \sigma'(z) \approx \frac{\sigma(z+h)−\sigma(z - h)}{2h} (less efficient, error-prone)
- Vectorized operations (NumPy preferred for larger arrays)
Time complexity: O(n) where n is list length (1-100 elements).
3. Step-by-Step Strategy
- Input validation (optional but good practice): Ensure 0≤gi​≤1
- Element-wise computation: For each g in sigmoid_output, calculate g×(1−g)
- Precision handling: Use round(value, 4) for each result
- List construction: Collect results in new list
- Return: Output list matching input length
Sample implementation:
def sigmoid_derivative(sigmoid_output):
return [round(g * (1 - g), 4) for g in sigmoid_output]
Verification:
- Input [0.5, 0.8] → [0.5×0.5=0.25, 0.8×0.2=0.16] ✓
- Edge cases: [0.0] → [0.0], [1.0] → [0.0], maximum at [0.5] → [0.25]
4. Common Pitfalls
- Not rounding to 4 decimals: 0.159999999 instead of 0.1600
# Wrong
[g * (1 - g) for g in sigmoid_output] # [0.16] may print as [0.159999999]
# Correct
[round(g * (1 - g), 4) for g in sigmoid_output] # [0.16]
- Input range violation: Derivative undefined or negative outside [0,1]
sigmoid_derivative([-0.1, 1.1]) # Produces negative values—invalid!
-
Using sigmoid input instead of output: Must use activation output g, not pre-activation z
-
Floating-point precision: Use round() consistently; Python's print may show 0.16 as 0.159999999
-
Mutable input modification: Don't alter original list—create new one
5. Time & Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(n) | Single pass over n elements (1 ≤ n ≤ 100); constant-time arithmetic per element |
| Space | O(n) | Output list stores n floats; no additional data structures needed |
Scalability notes:
- Perfectly efficient for constraints (n ≤ 100)
- For large arrays, use NumPy: np.round(sigmoid_output * (1 - sigmoid_output), 4)
- No recursion, sorting, or heavy computation—purely linear
Gradient interpretation: Maximum gradient magnitude is 0.25 at g=0.5, vanishing at boundaries (g=0,1)—explains vanishing gradient problem in deep sigmoid networks.