Quiz 2

⚡ Activation Functions

792 words
4 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# ⚡ Activation Functions ## 1. 🎯 Learning Objectives By the end of this topic, you will be able to: - Compute the output and derivative of sigmoid, tanh, ReLU, and their variants - Explain the vanishing gradient problem and how ReLU mitigates it - Choose the appropriate activation function for a given layer/network...

⚡ Activation Functions

1. 🎯 Learning Objectives

By the end of this topic, you will be able to:
  • Compute the output and derivative of sigmoid, tanh, ReLU, and their variants
  • Explain the vanishing gradient problem and how ReLU mitigates it
  • Choose the appropriate activation function for a given layer/network
  • Implement activation functions in Python with NumPy
  • Analyze the computational and numerical properties of each function

2. 📋 Prerequisites

PrerequisiteCourseWhy It Matters
DerivativesBSMA1002Need derivatives for backpropagation
PerceptronW1 T1Understanding why activation matters
Chain ruleBSMA1002For gradient computation

3. 📖 Core Content

3.1 Intuition: Why Do We Need Activation Functions?

Without activation functions, a neural network would just be a linear transformation of inputs — no matter how many layers, the entire network would collapse into a single linear transformation (because composition of linear functions is linear). Activation functions introduce nonlinearity, allowing networks to learn complex patterns. Each activation function has:
  1. A forward pass: a=f(z)a = f(z) — transforms the weighted sum zz into the activation aa
  2. A backward pass: az=f(z)\frac{\partial a}{\partial z} = f'(z) — derivative used for gradient computation

3.2 Sigmoid (Logistic)

σ(z)=11+ez\sigma(z) = \frac{1}{1 + e^{-z}} σ(z)=σ(z)(1σ(z))\sigma'(z) = \sigma(z)(1 - \sigma(z))
Range: (0, 1) Use: Output layer for binary classification, hidden layers (historically) Derivation of derivative:
σ(z)=ez(1+ez)2=11+ezez1+ez=σ(z)(1σ(z))\sigma'(z) = \frac{e^{-z}}{(1+e^{-z})^2} = \frac{1}{1+e^{-z}} \cdot \frac{e^{-z}}{1+e^{-z}} = \sigma(z)(1-\sigma(z))
Problems:
  • Vanishing gradient: For z>5|z| > 5, σ(z)0\sigma(z) \approx 0 or 11, so σ(z)0\sigma'(z) \approx 0. Gradients "vanish" — no learning.
  • Not zero-centered: Outputs are always positive, causing zigzagging in optimization.
  • Expensive: Exponential computation.

3.3 Tanh (Hyperbolic Tangent)

tanh(z)=ezezez+ez\tanh(z) = \frac{e^z - e^{-z}}{e^z + e^{-z}} tanh(z)=1tanh2(z)\tanh'(z) = 1 - \tanh^2(z)
Range: (-1, 1) Use: Hidden layers (zero-centered advantage over sigmoid) Properties:
  • Zero-centered: outputs in [-1, 1], which helps optimization
  • Still suffers from vanishing gradients for large z|z|

3.4 ReLU (Rectified Linear Unit)

ReLU(z)=max(0,z)\text{ReLU}(z) = \max(0, z) ReLU(z)={1z>00z0\text{ReLU}'(z) = \begin{cases} 1 & z > 0 \\ 0 & z \leq 0 \end{cases}
Range: [0, ∞) Use: Default for hidden layers in most modern networks Advantages:
  • No vanishing gradient for z>0z > 0 (gradient = 1)
  • Sparse activation (many neurons are 0)
  • Computationally cheap (just max(0, z))
  • Empirically accelerates convergence Disadvantages:
  • Dying ReLU: If a neuron's weights push all inputs to negative, gradient is 0 and the neuron never recovers
  • Not zero-centered

3.5 Leaky ReLU and Variants

Leaky ReLU:
LReLU(z)=max(αz,z)\text{LReLU}(z) = \max(\alpha z, z)
Typically α=0.01\alpha = 0.01 ELU (Exponential Linear Unit):
ELU(z)={zz>0α(ez1)z0\text{ELU}(z) = \begin{cases} z & z > 0 \\ \alpha(e^z - 1) & z \leq 0 \end{cases}
Swish (Self-Gated):
Swish(z)=zσ(z)\text{Swish}(z) = z \cdot \sigma(z)

3.6 Comparison Table

FunctionRangeDerivativeVanishing?Zero-Centered?Computational Cost
Sigmoid(0, 1)σ(1-σ)SevereNoMedium
Tanh(-1, 1)1-tanh²ModerateYesMedium
ReLU[0, ∞)1 or 0No (z>0)NoLow
Leaky ReLU(-∞, ∞)1 or αNoNoLow
ELU(-α, ∞)1 or αe^zNoNear-zeroMedium
Swish(-0.28, ∞)σ + z·σ(1-σ)NoNoHigh

4. 📐 Key Formulas

FunctionFormulaDerivative
Sigmoidσ(z)=1/(1+ez)\sigma(z) = 1/(1+e^{-z})σ(1σ)\sigma(1-\sigma)
Tanhtanh(z)=(ezez)/(ez+ez)\tanh(z) = (e^z-e^{-z})/(e^z+e^{-z})1tanh21-\tanh^2
ReLUmax(0,z)\max(0, z)1z>01_{z>0}
Leaky ReLUmax(αz,z)\max(\alpha z, z)1z>0+α1z01_{z>0} + \alpha \cdot 1_{z\leq 0}

5. ⚠️ Common Pitfalls

Pitfall 1: Using Sigmoid in Hidden Layers

The mistake: Using sigmoid activation in deep hidden layers. Why: Vanishing gradients prevent learning in early layers. Correct approach: Use ReLU (or Leaky ReLU) in hidden layers, sigmoid only for binary classification output.

Pitfall 2: Dying ReLU

The mistake: Using ReLU with too high learning rate. Why: Large updates can push weights such that a neuron always outputs negative, gradient becomes 0, neuron "dies." Solution: Use Leaky ReLU, or smaller learning rates, or batch normalization.

Pitfall 3: Forgetting the Derivative in Backprop

The mistake: Computing the gradient without multiplying by the activation derivative. Correct approach: Lz=Laf(z)\frac{\partial L}{\partial z} = \frac{\partial L}{\partial a} \cdot f'(z).

6. 📝 Practice Questions

Q1: Compute sigmoid(0), sigmoid(1), sigmoid(-1). What are the derivatives?
Answer: σ(0) = 0.5, σ(1) = 1/(1+e⁻¹) ≈ 0.731, σ(-1) = 1/(1+e¹) ≈ 0.269. Derivatives: σ'(0) = 0.5×0.5 = 0.25, σ'(1) = 0.731×0.269 ≈ 0.197, σ'(-1) = 0.269×0.731 ≈ 0.197. Q2: For z = [-5, 0, 5], compute ReLU outputs and derivatives.
Answer: ReLU(-5) = 0, derivative = 0. ReLU(0) = 0 (edge case, typically derivative = 0 or 0.5 in implementations). ReLU(5) = 5, derivative = 1. Q3: Why is tanh preferred over sigmoid for hidden layers despite both being S-shaped?
Answer: Tanh is zero-centered (range -1 to 1), which means gradients can be both positive and negative. This helps with optimization because the gradients don't all go in the same direction. Sigmoid outputs are always positive, which can cause zigzagging gradients. Join Discord PreviousDL History & PerceptronNextPerceptron to MLP
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.