Computer Vision Lab Lessons Activation functions

Activation functions

Why we need non-linearity, and what ReLU and Softmax are.

Beginner20 minConvolutional neural networksConvolution and neural networks

This content was prepared automatically from documented sources and its human scientific review is not yet complete. If you find an error, let us know.

Introduction

Without activation functions, a deep network is equivalent to a single linear transform however many layers it has.

Learning objectives

  • Understand the role of non-linearity.
  • Apply ReLU to a feature map.
  • Understand Softmax and its limits.

Prerequisites

Convolution step by step.

Scientific explanation

Composing linear transforms stays linear, so non-linearity is what allows the network to represent complex decision boundaries. ReLU zeroes negative values and passes positive ones; it is fast and eases the vanishing gradient. Softmax in the last layer turns scores into probabilities that sum to 1 over the known classes only.

In-depth explanation

Why is non-linearity necessary?

Composing linear transforms gives a linear transform: W_2(W_1x) = (W_2W_1)x. So a network of a hundred layers without activation functions is equivalent to a single layer. The non-linear activation function is what allows the network to approximate complex functions (the universal approximation theorem).

Detailed comparison

FunctionRangeDerivativeMain problem
Sigmoid \sigma(x)=\frac{1}{1+e^{-x}}(0,1)\sigma(1-\sigma)\le0.25Saturation and vanishing gradients
tanh(−1,1)1-\tanh^2Saturation
ReLU[0,∞)0 or 1Dead units
GELU≈[−0.17,∞)SmoothSlightly more expensive
SoftmaxA probability distributionJacobian p_i(\delta_{ij}-p_j)Overconfidence

Vanishing gradients in numbers

The derivative of the sigmoid never exceeds 0.25. Across 10 layers, at best the gradient is multiplied by 0.25^{10}\approx 10^{-6}, so the first layers hardly learn. ReLU, however, has a derivative of 1 for positive inputs, so the gradient does not vanish this way.

Softmax and temperature: an example

Scores z=(2,1,0): the probabilities with T=1 ≈ (0.665, 0.245, 0.090). With T=2 ≈ (0.506, 0.307, 0.186): softer. With T=0.5 ≈ (0.867, 0.117, 0.016): sharper. This is the basis of distillation (a high temperature reveals the "dark knowledge" in the wrong classes) and of calibration.

Sigmoid or Softmax in the last layer?

  • One class per image (cat or dog or…): Softmax + cross-entropy.
  • Several classes possible at once (an X-ray with more than one finding): a sigmoid per class + binary cross-entropy.
  • Binary segmentation: a sigmoid per pixel, or Softmax with two channels (equivalent).

Review questions

  1. Why does a deep network with sigmoid activations not learn well without special initialisation?
  2. Compute the Softmax of the scores (0,0,0).
  3. Choose the last layer for a model that detects several diseases in a single chest image.

Mathematical equations

ReLU(x) = max(0, x), and p_k = e^{z_k} / Σ_j e^{z_j}.

Practical example

In the convolution lab, negative cells appear struck through after ReLU.

Implementation steps

  1. Run the convolution with the Edge kernel.
  2. Enable ReLU and note the values that were zeroed.
  3. In the classification lab, note that the sum of the top 5 probabilities may approach 100%.

Expected results

ReLU keeps only the positive responses, and Softmax gives a sharp distribution when the model is confident.

Common mistakes

Interpreting the Softmax output as the probability that the answer is correct, which it is not unless the model is calibrated.

Practical applications

All neural networks.

References

  1. I. Goodfellow, Y. Bengio, A. Courville (2016).Deep Learning. MIT Press. www.deeplearningbook.org
  2. A. Krizhevsky, I. Sutskever, G. E. Hinton (2017).ImageNet classification with deep convolutional neural networks. Communications of the ACM 60(6) (NeurIPS 2012). doi:10.1145/3065386

Related algorithms

Try in convolution and neural networks