What is the tanh activation function?

tanh activation function

Tanh activation function The activation function determines the range of possible activation levels for a synthetic neuron. The total weighted input data to the neuron is used for this operation. can be identified by their non-linearity. Without an activation function, multilayer perceptrons simply multiply the weights by the input values to calculate the outputs.

Any two linear operations performed in succession are identical to only one. With a non-linear activation function, the artificial neural network and its approximation function are non-linear. The approximation theorem states that any multilayer perceptron with a single hidden layer and a nonlinear activation function is a universal function approximator.

Activation Functions seem pointless, so why use them?

Activation functions in neural networks produce non-linear results. However, without the activation functions, the neural network can only compute linear mappings between x and y. Is there a particular reason why this is happening?

Forward propagation would simply involve the multiplication of weight matrices by input vectors if activation functions weren’t used.

In order to do useful calculations, neural networks need to be able to infer non-linear correlations between input vectors x and output y. Non-linearity in the x-to-y mapping occurs when the underlying data is intricate.

If our neural network didn’t have an activation function in its hidden layer, it wouldn’t be able to mathematically realise the complex relationships we’ve programmed into it.

The Big Four Activation Functions in Deep Learning

It is time to discuss the most popular activation functions used in Deep Learning, along with the benefits and drawbacks of each.

Anti-Sigmoid Function.

There was a time when the sigmoid activation function was the most widely used. The Sigmoid function maps inputs onto the interval [0,1].

Taking x as input, the function returns a value between and (0, 1]). Signed nonlinearity is rarely used in practise nowadays. In particular, it has these two problems:

As a practical matter, Sigmoid functions “kill” gradients.

The first is that it is possible for gradients to vanish for sigmoid functions. One important issue with the function is that neural activation peaks between 0 and 1 (blue areas).

The derivative of the sigmoid function approaches 0 in these azure areas (i.e., large negative or positive input values). Weight updates and learning would be impossible if the derivative was very small around 0.

function of activating tanh

In Deep Learning, the tanh activation function is also commonly utilised. Below is a graphic of the tangent hyperbolic function:

The derivative of the neuron’s response resembles the sigmoid function, with the value tending toward zero as the magnitude of the response increases or decreases dramatically (blue region in Fig. 3). Its outputs are zero-centered, unlike the sigmoid function’s. Compared to sigmoid, tanh is more commonly used in clinical settings.

This article will show you how to implement the tanh activation function in TensorFlow with the help of the following code.

To use TensorFlow in your code, simply import tf as tf from the Keras library.

TensorFlow (tanh) (tanh)

The tangent can be calculated as follows: z = tf.constant([-1.5, -0.2, 0, 0.5], dtype=tf.float32) (z)

print(output.numpy()) #[-0.90514827, -0.19737533, 0., 0] is an example of this .46211714]

Where can I get the Python code for the tanh activation function and its derivative?

This allows for the straightforward expression of both the tanh activation function and its derivative. To put it another way, we need to define a function in order to use the formula. The process is depicted in the following diagram:

meaning of the tanh activation function (np.exp(z) – np.exp(-z)) / (np.exp(z) + np.exp(-z)) = tanh function(z).

One way to characterise the tanh prime function is as follows: return 1 – np. power(tanh function(z),2).

Use the tanh activation function when: the values of the tanh activation function vary from -1 to 1, making it useful for putting data into a more centred location, with a mean closer to 0, which facilitates learning in the subsequent layer. This is why the tanh activation function can be put to good use in practise.

Here is some basic Python code for the tanh activation function: # import libraries

“import matplotlib.pyplot as plt”

bring in NumPy as np

Making a tanh activation function

defined tanh(x):



revert to a, d




Prepare axes with centres #

plt.subplots(figsize=(9, 5)); fig, axe = plt.subplots


set position(‘center’)


set position(‘center’)


set color(‘none’)

ax.spines[‘top’].set color(‘none’)

x-axis.set ticks position(‘bottom’)

y-axis.set ticks position(‘left’)

# Construct the plot and display it.

the code: ax.plot(b,tanh(b)[0], color=”#307EC7″, linewidth=3, label=”tanh”)

label = “derivative,” linewidth = 3, and colour = “#9621E2” in an ax.plot(b,tanh(b)[1])

Frameon=false upperright”>ax.legend(loc=”upper right”).


The following is the output of the aforementioned code, which plots the tanh and its derivative.

The Softmax Activation Function

One final activation function I’d want to cover is the softmax. This activation function is unique in comparison to others.

The softmax activation function limits the values of the output neurons to be between 0 and 1, which accurately represents probabilities in the interval [0, 1].

To put it another way, each feature vector, x, belongs to a specific category. It is impossible for the classes dog and cat to be equally represented by a feature vector that is an image of a dog. It is crucial that this feature vector adequately represents dogs as a whole.

By Alex

Leave a Reply

Your email address will not be published. Required fields are marked *