Activation Functions

In the previous lessons, we have seen how the activation functions work. Let’s recap what we learned and then introduce some new activation functions.

This Lesson Will Cover
  • ⚑ Activation Functions Overview
  • πŸͺœ Step Function
  • πŸ”₯ ReLU Function
  • πŸ“ˆ Sigmoid Function
  • 🎯 Softmax Function
  • πŸ” Function Comparisons

What are Activation Functions?

A neural network layer with parameters W\mathbf{W} and b\mathbf{b} will operate on an input vector x\mathbf{x} as follows:

z=Wβ‹…x+b \mathbf{z} = \mathbf{W} \cdot \mathbf{x} + \mathbf{b}

Where an activation function is then applied element-wise to each component of z\mathbf{z}:

a=f(z) \mathbf{a} = f(\mathbf{z})

This activation function is responsible for introducing non-linearity into the network, allowing it to learn and model complex patterns that a simple linear transformation cannot capture.

The Step Function

The step function is the first activation function we talked about in this section.

It’s probably the simplest activation function:

  • It outputs 1 if the input is positive (or zero)
  • It outputs 0 if the input is negative

While rarely used in modern networks (since e.g. (a) it’s non-differentiable, and (b) the sigmoid function provides a more informative representation anyway), it was historically important in early neural network development.

Step Activation Function Output Curve
Output of the binary step activation function: the neuron fires 1 for inputs greater than or equal to 0 and stays silent 0 for inputs less than 0.

Formula

step(z)={1if zβ‰₯00if z<0 \text{step}(z) = \begin{cases} 1 & \text{if } z \geq 0 \\ 0 & \text{if } z < 0 \end{cases}

Example: Applying the Step Function

For an input z=2.0z = 2.0:

step(2.0)=1 since 2.0β‰₯0 \text{step}(2.0) = 1 \text{ since } 2.0 \geq 0

For a negative input z=βˆ’1.5z = -1.5:

step(βˆ’1.5)=0 since βˆ’1.5<0 \text{step}(-1.5) = 0 \text{ since } -1.5 < 0

The ReLU Function (Rectified Linear Unit)

We’ve also already seen the ReLU function in action.

  • It outputs the input directly if the input is positive
  • It outputs 0 otherwise

It’s simple, computationally efficient, and commonly used in hidden layers.

ReLU Activation Function Output Curve
Rectified Linear Unit (ReLU) returns the input unchanged for positive values and clamps negatives to zero, producing sparse activations that speed up deep-network training.

Formula

ReLU(z)=max⁑(0,z) \text{ReLU}(z) = \max(0, z)

Example: Applying the ReLU Function

For an input z=2.0z = 2.0:

ReLU(2.0)=max⁑(0,2.0)=2.0 \text{ReLU}(2.0) = \max(0, 2.0) = 2.0

For a negative input z=βˆ’1.5z = -1.5:

ReLU(βˆ’1.5)=max⁑(0,βˆ’1.5)=0 \text{ReLU}(-1.5) = \max(0, -1.5) = 0

The Sigmoid Function

The sigmoid function maps any real-valued input to a value between 0 and 1.

Unlike the step function, the sigmoid function provides a smooth, continuous output that can be interpreted as a probability, and it’s differentiable - making it suitable for gradient-based learning.

Sigmoid Activation Function Output Curve
The smooth S-shaped sigmoid curve maps any real-valued input into the (0, 1) interval, letting outputs be interpreted as probabilities while remaining differentiable.

Formula

Οƒ(z)=11+eβˆ’z \sigma(z) = \frac{1}{1 + e^{-z}}

Example: Applying the Sigmoid Function

Let’s apply the sigmoid function to a single input value:

z = 2.0

Substitute zz into the formula:

Οƒ(2.0)=11+eβˆ’2.0 \sigma(2.0) = \frac{1}{1 + e^{-2.0}}

First, compute eβˆ’2.0e^{-2.0}:

eβˆ’2.0β‰ˆ0.1353 e^{-2.0} \approx 0.1353

Then compute Οƒ(2.0)\sigma(2.0):

Οƒ(2.0)=11+0.1353=11.1353β‰ˆ0.8808 \sigma(2.0) = \frac{1}{1 + 0.1353} = \frac{1}{1.1353} \approx 0.8808

The output of the sigmoid function for z=2.0z = 2.0 is:

Οƒ(2.0)β‰ˆ0.8808 \sigma(2.0) \approx 0.8808

This result can be interpreted as an 88.08% probability for the positive class.

The Softmax Function

The softmax function is the multi-class generalization of the sigmoid function.

It converts a vector of raw scores into a probability distribution across multiple classes.

In fact, for a binary classification problem (n = 2), the softmax function reduces to the sigmoid function, making the sigmoid function a special case of softmax.

For a binary case with input zz, the softmax probabilities would be:

P(class1)=ezez+e0=11+eβˆ’z=Οƒ(z) P(class_1) = \frac{e^z}{e^z + e^0} = \frac{1}{1 + e^{-z}} = \sigma(z)
P(class2)=e0ez+e0=11+ez=1βˆ’Οƒ(z) P(class_2) = \frac{e^0}{e^z + e^0} = \frac{1}{1 + e^z} = 1 - \sigma(z)

This mathematical relationship shows why the sigmoid function is often considered the binary version of softmax, and why we can use either function for binary classification tasks.

Softmax Activation Function Probability Distribution
Softmax converts raw logits into a normalized probability distribution across multiple classes. This figure visualizes the example below, where x-axis corresponds to the softmax input for class 1. As the first logit (purple) increases, its probability rises while the fixed logits for the other classes (orange, pink) drop, keeping the total sum at 1. Note that the class 2 and 3 logits (-0.5 and 1.2, as seen below) must remain fixed in order for this plot to be valid.

Formula

softmax(zi)=eziβˆ‘j=1nezj \text{softmax}(z_i) = \frac{e^{z_i}}{\sum_{j=1}^n e^{z_j}}

Where:

  • ziz_i: The ii-th element of the input vector,
  • nn: Total number of classes.

Example: Applying the Softmax Function

Let’s apply the softmax function to a 3-dimensional input vector zz:

z=[0.4βˆ’0.51.2] \mathbf{z} = \begin{bmatrix} 0.4 \\ -0.5 \\ 1.2 \end{bmatrix}

Step 1: Compute the Exponentials
Calculate the exponentials of each element:

ez1=e0.4β‰ˆ1.4918,ez2=eβˆ’0.5β‰ˆ0.6065,ez3=e1.2β‰ˆ3.3201 e^{z_1} = e^{0.4} \approx 1.4918, \quad e^{z_2} = e^{-0.5} \approx 0.6065, \quad e^{z_3} = e^{1.2} \approx 3.3201
ez=[1.49180.60653.3201] e^{\mathbf{z}} = \begin{bmatrix} 1.4918 \\ 0.6065 \\ 3.3201 \end{bmatrix}

Step 2: Compute the Sum of Exponentials
Sum all the exponentials:

Sum=1.4918+0.6065+3.3201=5.4184 \text{Sum} = 1.4918 + 0.6065 + 3.3201 = 5.4184

Step 3: Compute the Softmax Outputs
Divide each exponential by the sum:

softmax(z1)=1.49185.4184β‰ˆ0.2752,softmax(z2)=0.60655.4184β‰ˆ0.1119,softmax(z3)=3.32015.4184β‰ˆ0.6129 \text{softmax}(z_1) = \frac{1.4918}{5.4184} \approx 0.2752, \quad \text{softmax}(z_2) = \frac{0.6065}{5.4184} \approx 0.1119, \quad \text{softmax}(z_3) = \frac{3.3201}{5.4184} \approx 0.6129

The final probabilities are:

softmax(z)=[0.27520.11190.6129] \text{softmax}(\mathbf{z}) = \begin{bmatrix} 0.2752 \\ 0.1119 \\ 0.6129 \end{bmatrix}

These outputs represent the probabilities for three classes. For example:

  • The probability of the first class is 27.52%.
  • The probability of the second class is 11.19%.
  • The probability of the third class is 61.29%.
Softmax activation with labeled probabilities for example vector
Softmax probabilities for three classes in the example above. We draw a vertical line through the first component of the input vector (z1 = 0.4) in order to visualize exactly how the softmax generates a probability distribution from the given input vector.

Comparison: ReLU vs Sigmoid vs Softmax

ReLU

  • Where to Use: Hidden layers in any type of neural network.
  • Why: It introduces non-linearity without constraining the output range, allowing the network to learn complex functions.
  • Example Use Case: Learning features in deep layers of a network for image or text data.

Sigmoid

  • Where to Use: Output layer for binary classification problems.
  • Why: It squashes values between 0 and 1, making it ideal for binary probability predictions.
  • Example Use Case: Spam detection, sentiment analysis (positive/negative).

Softmax

  • Where to Use: Output layer for multi-class classification problems.
  • Why: It converts raw scores into probabilities that sum to 1, making it ideal for interpreting outputs as class probabilities.
  • Example Use Case: Predicting the type of object in an image (e.g., cat, dog, bird).

Key Differences

Feature ReLU Sigmoid Softmax
Range of Outputs [0,∞)[0, \infty) for positive inputs; 00 otherwise [0,1][0, 1] [0,1][0, 1]
Primary Application Non-linear transformations in hidden layers Binary classification output layer Multi-class output layers
Output Normalization No normalization No normalization Outputs sum to 1

Conclusion

Congratulations! We’ve made it through the basics of neural networks. Understanding this section is an important foundation for grasping how AI works at a technical level.

Key Takeaways

⚑ Activation functions introduce non-linearity into neural networks, allowing them to learn complex patterns.

🏁 The Step function was historically important but is rarely used in modern networks.

πŸš€ ReLU is computationally efficient and commonly used in hidden layers.

🎯 Sigmoid maps inputs to values between 0 and 1, making it useful for binary classification.

πŸ“Š Softmax generalizes sigmoid for multi-class problems by outputting a probability distribution.

πŸ” Each activation function has specific use cases:

  • ⚑ ReLU: Hidden layers in any neural network
  • 🎯 Sigmoid: Binary classification output layers
  • πŸ“Š Softmax: Multi-class classification output layers

Next: Famous Nerual Networks

This leads us perfectly into the next section, where we get to talk about a few of the most famous neural networks that have paved the way to modern AI.

Comments (0)

No comments yet.