Deep Learning

Activation Functions Explained

By NeuralBed Editorial Team · · 5 min read

Every neuron in a neural network does the same basic job. It takes inputs, multiplies them by weights, adds them up, and then passes the result through one more step before sending it onward. That last step is the activation function.

It looks like a small detail, but without it a deep network would be no more powerful than a single layer. Activation functions are what let networks bend, curve and model the messy patterns found in real data.

Why networks need non-linearity

Suppose you stack ten layers that only multiply and add. Multiplying and adding are linear operations, and a chain of linear operations collapses into one linear operation. Ten layers would behave exactly like a single layer with different weights.

A linear model can only draw straight lines, flat planes and their higher-dimensional cousins. It cannot separate two groups that are arranged in a spiral, recognize a face, or translate a sentence. The activation function adds a non-linearity between layers, so each layer can build on the last one in a genuinely new way. This is what gives depth its value.

What an activation function actually does

An activation function takes the neuron's weighted sum and maps it to an output. Different functions shape that output differently. They may squash it into a fixed range, cut off negative values, or turn a list of scores into probabilities.

When choosing one, people care about a few properties:

  • Range: does the output stay bounded, or can it grow without limit?
  • Smoothness: can we compute a useful gradient everywhere? Training relies on gradients.
  • Cost: is it cheap to compute, since it runs millions or billions of times?
  • Gradient behavior: does the gradient stay healthy as it flows backward through many layers?

Sigmoid

The sigmoid function squeezes any input into a range between 0 and 1. Large positive inputs give values near 1, large negative inputs give values near 0, and zero gives one half.

This makes sigmoid a natural fit for the output of a binary classifier, where the result can be read as a probability. It was also popular in early networks for hidden layers.

Its weakness is saturation. When the input is very large or very small, the curve is almost flat, so the gradient is nearly zero. During backpropagation these tiny gradients get multiplied across layers and the early layers barely learn. This is called the vanishing gradient problem. Another drawback is that sigmoid outputs are never negative, which can make training less balanced.

Tanh

The hyperbolic tangent, or tanh, looks like a stretched sigmoid. It maps inputs to a range between -1 and 1 and is centered on zero. Being zero-centered often makes optimization a little easier than with sigmoid.

Tanh still saturates at the extremes, so it shares the vanishing gradient issue. It remains common inside certain recurrent network designs, where a bounded output is useful.

ReLU

The rectified linear unit, or ReLU, is wonderfully simple: if the input is positive, return it unchanged; otherwise return zero.

def relu(x):
    return max(0.0, x)

ReLU became the default for hidden layers in many deep networks for good reasons. It is extremely cheap to compute. For positive inputs its gradient is a constant 1, so it does not saturate on that side and gradients flow well through deep stacks. It also produces sparse activations, since many neurons output exactly zero for a given input.

The main risk is the dying ReLU problem. If a neuron's input stays negative for all examples, its output and gradient are both zero, and it may never recover.

Leaky ReLU and its relatives

Several variants try to fix the dying neuron issue. Leaky ReLU lets a small fraction of negative values through instead of zeroing them, so the gradient is never completely zero. Parametric ReLU makes that fraction a learned value. Smoother options such as GELU and SiLU, which curve gently near zero instead of having a sharp corner, are widely used in modern language and vision models.

Which variant is better often depends on the architecture and dataset, and the differences are usually modest. Starting with plain ReLU and switching only if there is a reason is a sensible habit.

Softmax

Softmax is a bit different because it works on a whole vector of scores rather than one number. It turns a list of raw scores into probabilities that are positive and add up to one.

You typically see it at the final layer of a multi-class classifier. If a network scores an image as cat, dog or bird, softmax converts those scores into three probabilities, and the largest one is the model's top guess.

Choosing an activation function

A practical set of defaults looks like this:

  1. Use ReLU or a close variant for hidden layers.
  2. Use sigmoid on the output for a yes-or-no prediction.
  3. Use softmax on the output for choosing one class among many.
  4. Use no activation, or a linear output, when predicting a continuous number such as a temperature.

Beyond those defaults, experiment only when training shows a clear problem, such as gradients that vanish or neurons that stay dead.

Frequently Asked Questions

Can a neural network work without activation functions?

It can run, but it would only be able to represent linear relationships, no matter how many layers it has. Activation functions are what allow a deep network to model complex, curved patterns.

Why is ReLU so popular for hidden layers?

It is cheap to compute, it does not saturate for positive inputs, and it lets gradients flow well through many layers. These traits made training deep networks faster and more reliable than with sigmoid or tanh.

What is the difference between sigmoid and softmax?

Sigmoid maps a single score to a value between 0 and 1, which suits a yes-or-no decision. Softmax takes several scores at once and turns them into probabilities that sum to one, which suits choosing among multiple classes.

Related reading