Problem
Implement the sigmoid activation function: sigma(x) = 1 / (1 + e^(-x)). Given an input value, return the sigmoid output rounded to 4 decimal places.
Solution
import math
def sigmoid(z: float) -> float:
return round(1 / (1 + math.exp(-z)), 4)
Explanation
- The sigmoid function maps any real number to the range (0, 1).
- For large positive z, e^(-z) approaches 0, so sigmoid approaches 1.
- For large negative z, e^(-z) is very large, so sigmoid approaches 0.
- At z = 0, sigmoid = 0.5 (the midpoint).
Complexity