Introduction
In our last lesson, you learned about the fundamental structure of a single artificial neuron. We established that a neuron computes its output by applying an activation function to a weighted sum of its inputs plus a bias: . We briefly touched on the Sigmoid and ReLU functions as examples of , noting that their purpose is to introduce non-linearity.
This lesson dives deep into that crucial concept. Our learning goal is to explain the role of common activation functions (e.g., Sigmoid, ReLU, Softmax). We will explore why non-linearity is not just a feature but a necessity for building powerful neural networks. Then, we will conduct a detailed tour of the most important activation functions, analyzing their mathematical properties, strengths, weaknesses, and specific use cases.
Understanding these functions is like a master architect understanding the properties of different building materials. Choosing the right one for the right layer is critical for designing networks that can learn effectively and solve the kind of complex problems that drive transformative technology.
The Critical Need for Non-Linearity
In the previous lesson, we stated that activation functions introduce non-linearity. But why is this so important? What would happen if we skipped the activation function, or used a simple linear one like , also called the identity function?
A network built this way would only be capable of learning linear relationships. No matter how many layers of neurons you stack, the entire network would behave as if it were just a single, simple linear model.
To see the mathematical proof of this, let's turn to a video by Andrew Ng, a leading figure in AI education.
Why Non-linear Activation Functions (C1W3L07)
This video, 'Why Non-linear Activation Functions', provides a concise and clear mathematical demonstration of why linear activation functions are insufficient for deep networks.
Watch from the beginning to 03:21. Pay close attention to the derivation that shows how two linear layers can be algebraically simplified into a single linear layer. This is the core argument against using linear activations in hidden layers.
As you saw, if we have:
- Layer 1: (linear activation)
- Layer 2: (linear activation)
By substituting the first equation into the second, we get:
If we define and , the equation becomes:
This is just another linear function. Adding more hidden layers would just result in a more complex, but still fundamentally linear, transformation. Since most real-world problems are highly non-linear, such a network would be severely limited.
The only common exception, as the video also mentions, is using a linear activation function in the output layer for regression tasks, where the goal is to predict a continuous value (like a stock price or temperature) that is not constrained to a specific range.
A Tour of Common Activation Functions
Now that we've established the need for non-linearity, let's explore the functions that provide it. Each has a unique shape and properties that make it suitable for different parts of a neural network.
This next video provides an excellent visual survey of the most popular activation functions.
Activation Functions In Neural Networks Explained | Deep Learning Tutorial
Watch this overview from AssemblyAI, 'Activation Functions In Neural Networks Explained', to get a quick, intuitive feel for the shape and purpose of each major function.
Please watch from 02:26 to 05:27. This covers Sigmoid, Tanh, ReLU, and Softmax, which are the most critical functions for you to know. For each one, focus on its output range and its primary use case (e.g., hidden layer, output layer for binary/multi-class classification).
Let's break these down in more detail, adding their mathematical formulas and discussing their pros and cons.
1. Sigmoid (or Logistic) Function
The Sigmoid function was a popular choice in the early days of neural networks.
Formula:
Properties:
- Output Range: (0, 1). It squashes any real number into this range.
- Shape: A smooth 'S'-shaped curve.
- Use Case: Primarily used in the output layer for binary classification. The output can be interpreted as the probability of the positive class.
Limitations:
- Vanishing Gradients: For very large positive or negative inputs, the function's slope (derivative) becomes extremely close to zero. During backpropagation, these tiny gradients can make learning incredibly slow or halt it altogether, especially in deep networks.
- Not Zero-Centered: The output is always positive. This can lead to less efficient updates during gradient descent.
2. Tanh (Hyperbolic Tangent)
The Tanh function is a close cousin of the Sigmoid function.
Formula:
Properties:
- Output Range: (-1, 1).
- Shape: Also 'S'-shaped, but steeper than Sigmoid.
- Advantage over Sigmoid: Its output is zero-centered. This property often helps the network learn more efficiently than with Sigmoid. For this reason, Tanh is generally preferred over Sigmoid for hidden layers.
Limitations:
- Vanishing Gradients: Like Sigmoid, Tanh saturates at its extremes, leading to the same vanishing gradient problem.
3. ReLU (Rectified Linear Unit)
ReLU has become the default and most popular activation function for hidden layers in modern neural networks.
Formula:
Properties:
- Output Range: [0, ∞).
- Shape: It's a simple function: 0 for all negative inputs, and a straight line for all positive inputs.
- Use Case: The standard choice for hidden layers.
Advantages:
- Computationally Efficient: Very simple and fast to compute.
- Avoids Vanishing Gradients (for positive inputs): The gradient is a constant 1 for positive inputs, allowing gradients to flow well during backpropagation.
- Sparsity: Because it outputs 0 for half of its input range, it tends to make some neurons "inactive," which can make the network more efficient.
Limitations:
- The "Dying ReLU" Problem: If a neuron's weights are updated in such a way that its input is always negative, it will always output 0. Consequently, the gradient flowing through it will also be 0, and it will never update its weights again. The neuron effectively "dies" and stops contributing to learning.
To address this, variants like Leaky ReLU were introduced. Leaky ReLU has the formula , where is a small constant like 0.01. This gives a small, non-zero slope for negative inputs, preventing the neuron from dying.
4. Softmax Function
Softmax is unique because it acts on an entire vector of outputs from the final layer, not just a single neuron's output.
Formula: For a vector of raw outputs (logits) , the Softmax function is applied to each element :
Properties:
- Output: It converts a vector of arbitrary real numbers into a probability distribution. Each output value is in the range (0, 1), and all output values sum to 1.
- Use Case: Used exclusively in the output layer for multi-class classification. Each output corresponds to the probability of the input belonging to a specific class.
For a deeper dive into these functions, including their derivatives and more detailed pros and cons, the following article is an excellent resource.
Activation Functions in Neural Networks [12 Types & Use Cases]
The article 'Activation Functions in Neural Networks' by V7 Labs provides a comprehensive breakdown of the functions we've discussed and more. It's a great reference for solidifying your understanding.
Read the sections titled 'Sigmoid / Logistic Activation Function', 'Tanh Function (Hyperbolic Tangent)', 'ReLU Function', and 'Softmax Function'. As you read, focus on the 'limitations' discussed for each function, such as the vanishing gradient problem for Sigmoid/Tanh and the Dying ReLU problem.
How to Choose the Right Activation Function
You now have a toolbox of activation functions. The final step is learning some practical rules for selecting the right tool for the job.

Here are the most common rules of thumb, which are also summarized well in the V7 Labs article:
-
For Hidden Layers:
- Start with ReLU. It's the most common and often the best-performing choice.
- If you notice your model is not training well due to many "dead" neurons, switch to a variant like Leaky ReLU.
- Tanh is a viable alternative, but Sigmoid should generally be avoided in hidden layers.
-
For the Output Layer:
- Regression (predicting a continuous value): Use a Linear activation function (or no activation).
- Binary Classification (predicting one of two classes): Use the Sigmoid function.
- Multi-class Classification (predicting one of many classes): Use the Softmax function.
Conclusion
In this lesson, we explored the indispensable role of activation functions in neural networks. You learned that they are the source of the non-linearity that allows networks to model the complex patterns found in real-world data.
Key Takeaways:
- Without non-linear activation functions, even a deep neural network collapses into a simple linear model.
- ReLU is the modern standard for hidden layers due to its efficiency and ability to mitigate vanishing gradients, though it can suffer from the "Dying ReLU" problem.
- Sigmoid is used in the output layer for binary classification, converting a raw score into a probability between 0 and 1.
- Softmax is used in the output layer for multi-class classification, converting a vector of scores into a probability distribution where all class probabilities sum to 1.
- The choice of activation function for the output layer is determined by the problem type (regression, binary classification, or multi-class classification).
Preview of the Next Lesson:
We have now examined the neuron's internal components in detail. The next logical step is to see how these neurons are assembled. In the upcoming lesson, we will describe the architecture of a multi-layer feedforward neural network, learning how layers of neurons are connected to process information from input to output.