All formulas

Machine learning building blocks

Logistic sigmoid

ml.sigmoidAlso called sigmoid, logistic, logistic function, expit

σ(x)=11+e−x\sigma(x) = \frac{1}{1 + e^{-x}}

Maps a real number to (0, 1). It is the inverse of the logit and satisfies σ(−x) = 1 − σ(x).

Notation

xx

input

x ∈ ℝ

σ(x)\sigma(x)

Result: the probability

scalar

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    x=0x = 0

    Result

    σ(x)=12\displaystyle \sigma(x) = \frac{1}{2}

    ≈ 0.5

  2. Given

    x=1x = 1

    Result

    σ(x)=1e−1+1\displaystyle \sigma(x) = \frac{1}{e^{-1} + 1}

    ≈ 0.73105857863

  3. Given

    x=−2x = -2

    Result

    σ(x)=11+e2\displaystyle \sigma(x) = \frac{1}{1 + e^{2}}

    ≈ 0.119202922022

  4. Given

    x=1000x = 1000

    Result

    σ(x)=1e−1000+1\displaystyle \sigma(x) = \frac{1}{e^{-1000} + 1}

    ≈ 1

Common mistakes

  • Computes e× / (1 + e×)

    The two exponentials overflow to inf for x above about 709, and inf / inf is NaN.

    Fix: Use 1 / (1 + e−×) for x ≥ 0 and e× / (1 + e×) for x < 0, or call scipy.special.expit / torch.sigmoid.

  • Uses ex in the denominator

    The result is 1 / (1 + e×) = σ(−x) = 1 − σ(x).

    Fix: Negate the exponent: 1 / (1 + e−×).

Source

  • Goodfellow et al. 2016, §6.2.2.2

    Goodfellow, Ian; Bengio, Yoshua; Courville, Aaron. Deep Learning. MIT Press, 2016.

    deeplearningbook.org