Curious Minds

Clear answers to science and history questions

Why Do We Need Activation Functions?

· 6 min read · Science

We need activation functions because they introduce non-linearity into neural networks, allowing them to learn complex patterns and approximate any continuous function, which would be impossible with purely linear operations. Without them, stacking multiple layers would collapse into a single linear transformation, making the network no more powerful than a simple linear model.

Background: The Linear Collapse Problem

In a neural network, each neuron computes a weighted sum of its inputs, adds a bias, and then passes the result through an activation function. If we omit activation functions, the output of each layer is just a linear combination of the previous layer's outputs. Mathematically, composing several linear functions still yields a linear function, so a deep network without activations is equivalent to a single-layer network. This is known as the "linear collapse" problem: no matter how many layers you stack, the entire network can be represented by one matrix multiplication plus a bias, severely limiting what it can learn.

Real-world data—images, audio, text—contains highly non-linear relationships. For example, a model that decides whether an image contains a cat cannot be expressed as a straight-line boundary in pixel space. Activation functions give networks the flexibility to draw curved, complex decision boundaries.

How Activation Functions Work

An activation function takes the weighted sum of inputs (often denoted as z) and produces an output that is passed to the next layer. The choice of function affects how information flows and how gradients propagate during training. Common activation functions include:

  • Sigmoid: Maps any real number to a value between 0 and 1. Historically popular but suffers from vanishing gradients for extreme inputs.
  • Tanh (hyperbolic tangent): Maps inputs to values between -1 and 1, zero-centered, which helps with optimization but still has vanishing gradient issues.
  • ReLU (Rectified Linear Unit): Outputs the input directly if positive, otherwise 0. Simple and effective, but can cause "dying neurons" if many inputs are negative.
  • Leaky ReLU: A variant that allows a small, non-zero gradient for negative inputs to avoid dead neurons.
  • Softmax: Used in the output layer for multi-class classification, converting raw scores into probabilities that sum to 1.

Each function has a specific shape that determines how the network responds to inputs. For hidden layers, ReLU and its variants are the most common choice today because they are computationally cheap and mitigate vanishing gradients.

Why Non-Linearity Matters

Non-linearity is what enables a network to model almost any function. The Universal Approximation Theorem states that a feedforward network with a single hidden layer containing enough neurons and a non-linear activation function can approximate any continuous function on a compact domain to arbitrary accuracy. This theorem does not hold for linear activations—without non-linearity, the network remains a linear model regardless of depth.

Consider a simple task: classifying points inside a circle vs. outside. A linear model can only draw a straight line, so it fails. A non-linear activation allows the network to create a circular boundary by combining multiple linear pieces. In practice, deep networks with non-linear activations can learn features like edges, textures, and object parts in images, or syntactic patterns in text.

Choosing the Right Activation Function

The choice depends on the task and network architecture. Here are key guidelines:

  • Hidden layers: ReLU is the default for most feedforward and convolutional networks. Leaky ReLU or ELU (Exponential Linear Unit) can be used if dead neurons are a problem.
  • Output layer for regression: No activation (linear) is typical, so the output can be any real number.
  • Output layer for binary classification: Sigmoid, which outputs a probability between 0 and 1.
  • Output layer for multi-class classification: Softmax, which outputs a probability distribution over classes.
  • Recurrent networks: Tanh or ReLU are common, though tanh was historically preferred for its bounded output.

There is no universal best function; experimentation and validation on a hold-out set are standard practice. For example, a convolutional network for image classification often uses ReLU in hidden layers, while a language model might use GELU (Gaussian Error Linear Unit) for smoother gradients.

Common Problems and Misconceptions

One frequent misconception is that activation functions are only needed in deep networks. In truth, even a single-layer network benefits from a non-linear activation when the task is not linearly separable—otherwise it is just logistic regression or linear regression. Another issue is the vanishing gradient problem: with sigmoid or tanh, gradients become extremely small for large or small inputs, slowing or halting training. ReLU avoids this for positive inputs but introduces the dying ReLU problem, where neurons permanently output zero if they become negative. Solutions include using leaky variants or adjusting learning rates.

Another misconception is that activation functions are only for neural networks. They appear in other machine learning models, such as radial basis function networks, but the term is most associated with deep learning.

Alternatives and Extensions

While activation functions are standard, some architectures use alternative mechanisms. For example, self-normalizing networks use the SELU (Scaled Exponential Linear Unit) activation to automatically keep activations near zero mean and unit variance, reducing the need for batch normalization. Swish (or SiLU) is a smooth activation that sometimes outperforms ReLU in deeper networks. Parametric ReLU learns the slope for negative inputs during training.

In some cases, researchers have explored learnable activation functions, where the network itself determines the best shape, but these are not yet standard. For most practical applications, a well-chosen fixed activation function suffices.

Costs and Computational Considerations

Activation functions add negligible computational cost compared to matrix multiplications in large networks. However, some functions are more expensive than others. For example, sigmoid and tanh require exponential operations, while ReLU is a simple comparison. In very large models, using ReLU can speed up training. Memory usage is also minimal, as activations are computed on the fly during forward propagation and stored for backpropagation.

There is also a trade-off in training stability. ReLU can cause exploding gradients if the weights are large, so proper weight initialization and learning rate scheduling are important. Using batch normalization can mitigate this.

Safety and Numerical Stability

Numerical stability is a practical concern. Sigmoid and tanh can saturate, leading to gradients of zero and preventing learning. Softmax can produce extremely small probabilities, causing underflow; using log-softmax is more stable. In practice, frameworks like TensorFlow and PyTorch provide stable implementations that avoid overflow. It is also important to avoid using ReLU in the output layer for regression, as it clips negative outputs to zero, which would bias predictions.

Frequently Asked Questions

Can a neural network work without an activation function?

Technically yes, but it would be equivalent to a linear model, so it could only solve linearly separable problems. For any real-world task like image recognition or language translation, non-linearity is essential.

Why is ReLU so popular if it has the dying neuron problem?

ReLU is computationally cheap and avoids vanishing gradients for positive inputs, which makes training faster and more effective. The dying neuron issue can be mitigated with leaky variants or careful initialization, and in practice, ReLU performs well for most tasks.

What is the difference between sigmoid and softmax?

Sigmoid is used for binary classification and outputs a single probability between 0 and 1. Softmax is used for multi-class classification and outputs a vector of probabilities that sum to 1, where each value represents the probability of one class.

Does the choice of activation function affect training time?

Yes. ReLU and its variants are faster because they involve simple operations, while sigmoid and tanh require exponentials. However, the difference is usually small compared to the overall training time, which is dominated by matrix multiplications.

Can I use different activation functions in different layers?

Absolutely. It is common to use ReLU in hidden layers and softmax in the output layer. You can also mix functions within a network, though consistency in hidden layers often simplifies tuning.

In this article
  1. Background: The Linear Collapse Problem
  2. How Activation Functions Work
  3. Why Non-Linearity Matters
  4. Choosing the Right Activation Function
  5. Common Problems and Misconceptions
  6. Alternatives and Extensions
  7. Costs and Computational Considerations
  8. Safety and Numerical Stability
  9. Frequently Asked Questions