Tanh Activation Function

The Tanh activation is similar to the Sigmoid function, in fact, it is a linear transformation of the sigmoid function that maps inputs into values between -1 and 1.

Mathematically: $$ f(x) = tanh(x) = \frac {e^x - e^{-x}}{e^x + e^{-x}} $$

Below, we implement the tanh activation function in pytorch and visualize the output

import torch
import matplotlib.pyplot as plt

x_inputs = torch.arange(-10., 10., 1)
y_outputs = torch.tanh(x_values)

plt.figure(figsize=(9,6))
plt.plot(x_inputs, y_outputs)
plt.title("Tahn Function, x[-10, 10]")
plt.show()    
Tanh Activation Function