DEEP LEARNING LESSON 3 ACTIVATION FUNCTIONS

Sigmoid

Sigmoid is an activation function that converts a number into a smooth value between 0 and 1. It is especially useful when we want an output that can be interpreted as a probability-like score.

In simple words

Sigmoid takes any number and squeezes it into a value between 0 and 1.

Remember the Step Function

The Step Function gave us a hard decision:

z > 0
1
z ≤ 0
0

Sigmoid behaves differently. Instead of immediately jumping from 0 to 1, it changes smoothly.

z
Sigmoid
0 to 1

The Sigmoid Formula

The mathematical formula for Sigmoid is:

σ(z) = 1 / (1 + e^(-z))

You do not need to memorize the formula immediately. The important thing to understand first is what the function does to the input.

The symbol σ is commonly used to represent the Sigmoid function.

Example 1 — z = 0

Start with the easiest example:

z = 0

Apply the Sigmoid formula:

σ(0) = 1 / (1 + e^0)

e^0 = 1

σ(0) = 1 / (1 + 1)

σ(0) = 0.5

Therefore:

z = 0
Sigmoid
0.5

Example 2 — Positive Input

Now suppose:

z = 2

Applying Sigmoid gives approximately:

σ(2) ≈ 0.881
z = 2
Sigmoid
0.881

Notice that the output is close to 1, but it is not exactly 1.

Example 3 — Negative Input

Now suppose:

z = -2

Applying Sigmoid gives approximately:

σ(-2) ≈ 0.119
z = -2
Sigmoid
0.119

The output is now close to 0.

Notice the Pattern

Input z
Sigmoid Output
-5
≈ 0.007
-2
≈ 0.119
0
0.500
2
≈ 0.881
5
≈ 0.993

As the input becomes more positive, the Sigmoid output approaches 1. As the input becomes more negative, the output approaches 0.

Sigmoid "Squashes" the Input

The input can be any number:

-100
-10
-2
0
2
10
100

But Sigmoid converts all of them into the range:

0 < output < 1
Very Negative
Near 0
z = 0
0.5
Very Positive
Near 1

Using Sigmoid for Binary Classification

One common use of Sigmoid is the output layer of a binary classification model.

Suppose we want to predict:

Spam
or
Not Spam

Suppose the model calculates:

z = 2

Sigmoid converts it to:

sigmoid(2) ≈ 0.881

This can be interpreted as a score of about 88.1% toward the positive class, depending on how the model and labels are defined.

z = 2
Sigmoid
0.881
Positive Class

Turning the Sigmoid Output Into a Class

Sigmoid gives us a continuous value. If we need a final 0-or-1 decision, we can apply a threshold.

A common example is:

if probability >= 0.5:
    prediction = 1
else:
    prediction = 0

For example:

probability = 0.881

Since:

0.881 >= 0.5

the prediction becomes:

prediction = 1
z = 2
Sigmoid
0.881
Class 1

Sigmoid With Python

Python provides the mathematical tools needed to implement Sigmoid ourselves.

import math


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


print(sigmoid(0))
print(sigmoid(2))
print(sigmoid(-2))

Output:

0.5
0.8807970779778823
0.11920292202211755

Understand the Python Code

First:

import math

We import Python's built-in math module because the Sigmoid formula uses the mathematical constant e.

Then we create the function:

def sigmoid(z):

The function accepts one value, z.

Then:

return 1 / (1 + math.exp(-z))

This is the Python version of:

σ(z) = 1 / (1 + e^(-z))

For example:

sigmoid(2)

gives approximately:

0.8808

Sigmoid Inside a Neuron

Remember that the neuron first calculates z.

z = (x1 * w1) + (x2 * w2) + bias

output = sigmoid(z)

For example:

x1 = 2
x2 = 3

w1 = 0.5
w2 = 0.4

bias = 1

z = (x1 * w1) + (x2 * w2) + bias

output = sigmoid(z)

print(output)

First calculate:

z = (2 × 0.5) + (3 × 0.4) + 1
z = 3.2

Then apply Sigmoid:

sigmoid(3.2) ≈ 0.961
Inputs
Weights + Bias
z = 3.2
Sigmoid
≈ 0.961

Step Function vs Sigmoid

Step Function
Sigmoid
Outputs 0 or 1
Outputs between 0 and 1
Hard threshold
Smooth transition
No useful gradient at the threshold
Has a smooth gradient
Mainly useful for learning the basic idea
Useful for binary-output models

Is Sigmoid Perfect?

No. Sigmoid is useful, but it has limitations.

When the input becomes extremely positive or extremely negative, the output gets very close to 1 or 0. The gradient can become very small.

Very Negative
Output ≈ 0
Very Positive
Output ≈ 1

This is one reason Sigmoid is generally not the default activation function for hidden layers in modern deep networks. ReLU and its variants are commonly preferred there.

The Big Picture

Inputs
   ↓
Weights
   ↓
Weighted Sum
   ↓
+ Bias
   ↓
z
   ↓
Sigmoid
   ↓
Value between 0 and 1
   ↓
Prediction / Next Layer

The most important thing is to remember that Sigmoid does not calculate the neuron's weighted sum. It transforms the value after that calculation.

What You Should Remember

Sigmoid takes the neuron's value z and converts it into a smooth value between 0 and 1. Positive values move the output toward 1, negative values move it toward 0, and z = 0 produces exactly 0.5.

QUICK CHECK

Check Your Understanding

What range does Sigmoid produce?
Values between 0 and 1.

What is sigmoid(0)?
Exactly 0.5.

What happens when z becomes very positive?
The Sigmoid output approaches 1.

What happens when z becomes very negative?
The Sigmoid output approaches 0.

Where is Sigmoid commonly useful?
A common use is the output of binary classification models, where the result can be interpreted as a probability-like score.

NEXT TOPIC

ReLU

Sigmoid produces smooth values between 0 and 1. Next, we will learn ReLU, one of the most commonly used activation functions in neural networks.