๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #67

Sigmoid Function

The sigmoid function squashes any real number into the range (0, 1), making its output directly interpretable as a probability. It was the dominant hidden-layer activation function in early deep learning, before its serious drawbacks (covered below) led to ReLU becoming the default.

Formula

\[ \sigma(z) = \frac{1}{1+e^{-z}} \]

Derivative

\[ \sigma'(z) = \sigma(z)\big(1-\sigma(z)\big) \]

A convenient property: the derivative is expressible directly in terms of the function's own output โ€” no need to recompute \(z\) during backpropagation, just reuse \(\sigma(z)\) from the forward pass.

PropertyValue
Range\((0, 1)\)
\(\sigma(0)\)0.5
Maximum derivative0.25, at \(z=0\)

Graph

An S-shaped curve, flattening (saturating) toward 0 for large negative inputs and toward 1 for large positive inputs.

Numerical Example

\[ \sigma(0) = 0.5, \qquad \sigma(2) \approx 0.881, \qquad \sigma(-2) \approx 0.119 \]

The Vanishing Gradient Problem โ€” Sigmoid's Fatal Flaw

Look at the graph: for large \(|z|\) (either very positive or very negative), the curve is nearly flat โ€” the function saturates. Where the curve is flat, \(\sigma'(z)\) is close to 0. Since backpropagation multiplies gradients together layer by layer (via the chain rule), stacking many sigmoid layers means multiplying together many numbers that are each at most 0.25 and often much smaller โ€” the product shrinks exponentially with depth. This is the concrete mechanism behind the vanishing gradient problem, and it's the single biggest reason sigmoid fell out of favor for hidden layers in deep networks.

Not Zero-Centered โ€” A Second Drawback

Sigmoid's output is always positive (between 0 and 1), never negative. This means gradients flowing back through a sigmoid-activated layer tend to all push in the same direction for a given neuron's weights, which can make optimization less efficient (a less severe issue than vanishing gradients, but still a real one that tanh, next, was designed to fix).

Code

import numpy as np
import torch

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

print(sigmoid(np.array([-2, 0, 2])))   # [0.119 0.5 0.881]

z = torch.tensor([-2.0, 0.0, 2.0])
print(torch.sigmoid(z))

Where It's Still Used Today

  • Binary classification output layers โ€” where you specifically want a probability-like output in (0,1).
  • Gates inside LSTM and GRU cells โ€” where a 0-to-1 "how much to let through" signal is exactly what's needed (covered in the LSTM & GRU category).

It's rarely used for hidden layers in modern feedforward or convolutional networks โ€” that role has been taken over by ReLU and its variants.

Common Mistakes

  • Using sigmoid throughout a deep network's hidden layers "because it looks like a natural probability curve" โ€” this reintroduces vanishing gradients, exactly the problem that limited pre-2012 deep networks.
  • Forgetting sigmoid's output range makes it wrong for multi-class classification โ€” for that, softmax (covered later in this category) is needed instead.

Interview Relevance

Q: "Why did ReLU replace sigmoid as the default activation for hidden layers in deep networks?" Sigmoid saturates for large positive or negative inputs, making its derivative close to zero in those regions. Backpropagating through many sigmoid layers multiplies many such small gradients together, causing the vanishing gradient problem in deep networks. ReLU's derivative is either 0 or exactly 1, with no saturation on the positive side, which avoids this compounding shrinkage.

Practice Question

Compute \(\sigma(5)\) and \(\sigma'(5)\). What does the small value of \(\sigma'(5)\) tell you about how much gradient signal would pass through this neuron during backpropagation if it were operating in this region?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Sigmoid Function โ€“ FAQs

Quick answers about learning Sigmoid Function in Deep Learning.

This free note from CodingNow 2.0 explains Sigmoid Function in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Sigmoid Function, is 100% free with no signup required.
With focused practice, most students grasp Sigmoid Function in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now