← back

Sigmoid Activation Function Understanding

#22 · Deep Learning · Easy

⊣ Solve on deep-ml.com

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

1
2
3
4
import math

def sigmoid(z: float) -> float:
    return round(1 / (1 + math.exp(-z)), 4)

Explanation

  1. The sigmoid function maps any real number to the range (0, 1).
  2. For large positive z, e^(-z) approaches 0, so sigmoid approaches 1.
  3. For large negative z, e^(-z) is very large, so sigmoid approaches 0.
  4. At z = 0, sigmoid = 0.5 (the midpoint).

Complexity

  • Time: O(1)
  • Space: O(1)