Activation Functions Algorithms
Discover and understand AI algorithms at Programming Ocean Academy with clear explanations, equations, and illustrations for better learning.
AI Algorithms Explainer
Identity (Linear) Activation Function
$$ f(x) = x $$
- Output: Returns the input unchanged, acting as a pass-through layer.
- Gradient: Constant gradient of 1, enabling uninterrupted backpropagation.
- Linearity: No non-linearity is introduced, limiting its use to output layers.
- Use Case: Common in regression problems where real-valued output is needed.
- Limitation: Cannot model complex relationships when used in hidden layers.
Step (Binary Step) Activation Function
$$ f(x) = \begin{cases} 1 & \text{if } x \geq 0 \\ 0 & \text{if } x < 0 \end{cases} $$
- Binary Output: Produces a hard 0 or 1 response based on the sign of the input.
- No Gradient: Not differentiable at x = 0 and zero gradient elsewhere, making it unsuitable for backpropagation.
- Historical Use: Common in early perceptrons and logic gate modeling.
- Modern Usage: Rarely used in deep learning models due to non-trainability.
- Use Case: Rule-based systems, simple decision thresholds, or interpretable logic circuits.
Sigmoid (Logistic) Activation Function
$$ \sigma(x) = \frac{1}{1 + e^{-x}} $$
- Range: Outputs values between 0 and 1, making it ideal for probabilistic interpretation.
- Shape: S-shaped curve (smooth and differentiable), centered around 0.5.
- Gradient: Saturates for large positive or negative inputs, leading to vanishing gradients during backpropagation.
- Use Case: Binary classification output layers, probabilistic neural models, logistic regression.
- Limitation: Not zero-centered and gradients become very small outside (–2, 2) range.
Tanh (Hyperbolic Tangent) Activation Function
$$ \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}} $$
- Range: Outputs values between –1 and 1, making it zero-centered.
- Shape: S-shaped curve like sigmoid but symmetric around the origin.
- Gradient: Still suffers from vanishing gradients at extremes but better than sigmoid in practice.
- Use Case: Hidden layers in older neural networks, time series tasks, recurrent neural networks (RNNs).
- Advantage: Centered activation helps in gradient flow and faster convergence compared to sigmoid.
ReLU (Rectified Linear Unit) Activation Function
$$ f(x) = \max(0, x) $$
- Range: Outputs zero for negative inputs and identity for positive ones (0 to ∞).
- Simplicity: Easy to compute, introduces nonlinearity while being computationally efficient.
- Sparsity: Promotes sparse activations since many outputs become zero.
- Drawback: "Dying ReLU" problem where neurons can get stuck with zero output and stop updating.
- Use Case: Most common activation in hidden layers of deep neural networks, especially CNNs and MLPs.
Leaky ReLU Activation Function
$$ f(x) = \begin{cases} x & \text{if } x \geq 0 \\ \alpha x & \text{if } x < 0 \end{cases} $$
- alpha: Small constant (e.g., 0.01) that defines the slope for negative inputs.
- Improvement: Addresses the “dying ReLU” problem by allowing a small gradient when x is negative.
- Continuity: Function is continuous and avoids zero gradients in the negative domain.
- Use Case: Deep networks where ReLU units often get stuck, especially in early layers.
- Trade-off: Slightly increased complexity but often yields better gradient flow than standard ReLU.
GELU (Gaussian Error Linear Unit) Activation Function
$$ f(x) = x \cdot \Phi(x), \quad \text{where } \Phi(x) = \frac{1}{2} \left[1 + \text{erf} \left( \frac{x}{\sqrt{2}} \right) \right] $$
- Phi(x): Standard Gaussian cumulative distribution function.
- Smoothness: Applies a probabilistic gating to inputs, smoothly combining linearity and squashing.
- Nonlinearity: Retains small negative values and introduces smoother transitions compared to ReLU.
- Use Case: Popular in Transformer-based architectures like BERT and GPT for their robust convergence behavior.
- Advantage: Matches the performance of ReLU while benefiting from a smoother gradient flow.
Softmax Function
$$ P(y|x) = \frac{e^{w^Tx}}{\sum_{k=1}^{K}e^{w_k^Tx}} $$
- P(y|x): Probability of class \(y\) given input \(x\).
- e: Exponential function to ensure positive outputs.
- wTx: Dot product of weights and inputs for a specific class.
- Denominator: Sum of exponentials for all classes ensures probabilities sum to 1.