Sigmoid Activation Function: Understand It Visually and with Python
The Sigmoid activation function is one of the most important functions to understand when beginning machine learning and neural networks.
Its job is simple:
Mathematically:
This makes Sigmoid particularly useful when the output of a model needs to represent something similar to a probability.
What Happens to Different Inputs?
Consider a few values:
| Input z | Sigmoid Output | Meaning |
|---|---|---|
| Large negative | Close to 0 | Strongly towards Class 0 |
| 0 | 0.5 | Decision boundary |
| Large positive | Close to 1 | Strongly towards Class 1 |
1. Interactive Sigmoid Visualizer
Start with the interactive version below.
Move the slider or enter different values of z. Watch how the point moves along the Sigmoid curve and how the corresponding probability changes.
Try These Values
Experiment with:
- z = -10
- z = -5
- z = -1
- z = 0
- z = 1
- z = 5
- z = 10
Notice an important property:
2. Now Implement the Same Idea in Python
Once you understand the curve visually, run the Python program below.
This version uses:
- NumPy to calculate the Sigmoid function.
- Matplotlib to plot the S-shaped curve.
- A user-entered value of z.
- A threshold of 0.5 to demonstrate binary classification.
Understanding the Python Program
The central function is:
def sigmoid(z):
return 1 / (1 + np.exp(-z))
NumPy calculates e-z, and the result is placed into the Sigmoid formula.
The program then converts the output into a percentage and applies a simple decision rule:
if probability >= 0.5:
print("CLASS 1")
else:
print("CLASS 0")
Why Use 0.5?
The Sigmoid value at z = 0 is:
Therefore, 0.5 is a convenient example of a decision threshold.
In a real machine-learning application, however, the threshold does not always have to be 0.5. It can be adjusted according to the problem.
From a Number to a Classification
The complete idea can now be seen as a sequence:
For example:
| z | Approx. Sigmoid | Threshold 0.5 |
|---|---|---|
| -5 | 0.0067 | Class 0 |
| -1 | 0.2689 | Class 0 |
| 0 | 0.5000 | Class 1 |
| 1 | 0.7311 | Class 1 |
| 5 | 0.9933 | Class 1 |
The Most Important Idea
Do not think of Sigmoid as something mysterious that exists only inside a neural network.
At its core, it is simply a mathematical transformation:
Experiment with both programs above. Change the value of z, predict the result first, and then compare your prediction with the actual Sigmoid output.
Learn With Champak
Learn programming, artificial intelligence and machine learning by running the programs yourself.
0 Comments
Please comment