AI Logic

Glossary

Activation Function

Updated:

On the left, the flow through each layer goes upward from input x, through the linear computation z = Wx + b and the activation function (nonlinear) a = f(z), to the output; the pair of linear computation and activation function repeats once per layer. On the right is a graph of common activation functions (horizontal axis z from −4 to 4, vertical axis f(z) from −1 to 4) showing ReLU, sigmoid and tanh, with a legend below.

Explanation

An activation function is a nonlinear function applied after the linear computation in each layer of a neural network.

Introducing this nonlinearity allows a neural network to learn complex patterns in the input data. Nonlinearity is necessary because most real-world problems are not linearly separable.

With this nonlinear function, the optimization landscape changes from a simple flat shape into a more complex, "curved" one.

(When the activation function is linear, the optimization landscape tends to be simple and flat.)

  • Typical properties of an activation function
  • It should add nonlinear curvature to the optimization landscape to improve training convergence of the network.
  • It should not excessively increase the computational complexity of the model.
  • It should not hinder gradient flow during training.
  • It should preserve the distribution of the data to promote better training of the network.

Where It Is Used

Activation functions play an important role in the design of neural networks. In image recognition, an activation function is applied to the neurons in each layer of a CNN to extract features; in natural language processing, activation functions are used in RNNs and Transformers to introduce nonlinearity into the output. In reinforcement learning, an activation function is sometimes applied to the Q-value function when an agent selects an action. In generative models, especially GANs, activation functions are also used to shape the internal representations of the generator and discriminator. Because the choice of activation function strongly affects model performance, it needs to be designed with care.

FAQ

Q. Why are activation functions nonlinear?
A.

If the activation function were linear, the neural network as a whole could only perform a linear transformation. In that case, no matter how deep the network is made, it could not learn complex nonlinear relationships in the input.

Q. What are some common activation functions?
A.

Examples include ReLU, Sigmoid, Tanh, Leaky ReLU and Softmax. They are chosen according to the use case and the layer.

Q. How should an activation function be chosen?
A.

ReLU-family functions are common in hidden layers, while in the output layer a function is chosen to suit the task, such as Sigmoid (binary classification), Softmax (multi-class classification) or the identity function (regression). Performance and the vanishing gradient problem are also taken into account.