Original article was published on Deep Learning on Medium
Common Activation Functions and Why You Must Know Them
A summary of implementation, perks, caveats and utilisation of commonly used activation functions
Activation functions are a basic building block of neural networks. However, they must be studied carefully before one can use them effectively. This is because activation functions have active regions and dead regions, making them either learn or act dead in a model. Let’s go through one by one along with proper use of them and their downsides.
Sigmoid function or logistic function or Soft step function is the common starting point to study performance of neural networks. Mathematically, this function and its derivative can be represented as shown in Figure 1.
The function can be plotted as denoted in Figure 2.
We can see the function can only help the training within the range roughly -4 to 4. This is because beyond these limits on either sides, the gradients are not significant. Let’s have a look at the gradient curve shown in Figure 3.
We can see that beyond the limit of [-4, 4] shown in green and red lines, the gradients are not that significant. This is called the problem of diminishing gradients. Because of this reason, using the sigmoid function in a deep network is not desired. Furthermore, if you think of multiplying smaller values (<1) continuously, you will end up with very small values. This is an unfavourable condition in deep networks. So sigmoid is simply not a desired activation for deep learning. ReLU is the first workaround to overcome the undesired outcomes of the sigmoid function.
Although the sigmoid function is not used in hidden layers, it is an ideal candidate for the output layer. This is because sigmoid gives us values in the range [0, 1] which can help us to train a network for binary encoded output. This shall be done along with a binary cross entropy loss function.
ReLU (Rectified Linear Unit) Function
ReLU is simply outputting the non negative output of a neurone. Mathematically, this can be represented as shown in Figure 4.
We can plot the function and its derivate as illustrated in Figure 5.
As shown in Figure 5, the derivatives are never dead in the positive region. Furthermore, as the values are output as it is without any dampening, values will not vanish as we saw in the sigmoid function. Thus, ReLU becomes an ideal candidate for deep learning. However, as you might have noted, ReLU simply does not exist in the negative space. This is often okay since input values and output values of neural networks are positive. However, if you have scaled values or normalised values in the range [-1, 1], this might kill some neurones beyond recovery. This phenomenon is called dying ReLU. Though this is not something someone should worry about (can be avoided by scaling using a min-max scaler), it is worth knowing the workarounds. Note that due to the exploding nature of values through a ReLU network, it is often desired to not use ReLU in an output layer. Furthermore, exploding values can result in the phenomenon of exploding gradients. However, using proper clipping and regularisation can help.
ReLU variants and Other Linear Unit Functions
There are a few variants of ReLU which helps to overcome the problem of dying ReLU.
PReLU (Parametric ReLU) and Leaky ReLU
Parametric ReLU tries to parameterise the negative input thus enabling the recovery of the dying ReLU. However, the parameter is a learnable parameter with the exception of Leaky ReLU which uses a fixed parameter for the negative component. Usually this parameter is chosen to be a small value since the typical outputs of a network is supposed to be positive.
We have plot the diagram shown in Figure 7 for a given value of 𝛂 (0.1). This is the scenario of Leaky ReLU with 0.1 as the negative multiplier.
Note that the gradient in the negative region is subtle but non-zero. Also, the nodes will not be dead when faced with too many negative figures.
ELU (Exponential Linear Unit) Function
In this function, the negative component is modelled using an exponential representation. However, we will still have the learnable parameter 𝛂.
We can plot this as shown in Figure 9. Here, we assume 𝛂=0.5 for better visibility.
Now that we have talked about few important functions used in deep networks, let’s see some common but not so involving functions.
Tanh is the driving force behind LSTM networks. This helps avoiding the shortcomings of RNNs.
We can plot it as shown in Figure 11.
Few More Activation Functions
- Identity function: This is the naive function of
- Softmax function: This function is guaranteed to output values for the layer that adds up to 1. This is mostly used in categorical classification with categorical cross entropy as the loss function and mostly used in the output layer.
Activation functions are designed mostly with the derivative in mind. That’s why you see a nice simplified derivative function.
Activation functions must be chosen by looking at the range of the input values. However, ReLU or PReLU is a good starting point with sigmoid or softmax to the output layer.
Activation function with learnable parameters are often implemented as separate layers. This is more intuitive since the parameters are learned through the same back-propagation algorithm.
Hope you enjoyed reading this article.