Hyperbolic tangent (Tanh)

Description

In the context of artificial neural networks, the Hyperbolic tangent is an activation function defined as:

Hyperbolic tangent (Tanh)
where x is the input to a Artificial Neuron.

from matplotlib import pyplot as plt
import numpy as np

def tanh_forward(x):
    return (np.exp(2*x) - 1.0)/(np.exp(2*x) + 1.0)


x = np.arange(-7,7)
y = tanh_forward(x)

plt.style.use('fivethirtyeight')
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title("Plot of the Tanh function")
plt.show()
Plot of the Hyperbolic tangent (Tanh)

Hyperbolic tangent derivative with respect to x defined as:

Hyperbolic tangent (Tanh) derivative

Hyperbolic tangent used in computer vision and speech recognition using deep neural nets.

TensorFlow form of Hyperbolic tangent:
tf.tanh(
    x,
    name=None
)

Pytorch form of Hyperbolic tangent:
class torch.nn.Tanh

Forward propagation EXAMPLE

/* ANSI C89, C99, C11 compliance                                                             */
/* The following example shows the usage of Hyperbolic tangent function forward propagation. */
#include <stdio.h>
#include <math.h>


float tanh_forward(float x){
   float r_tanh = ((float)exp(2.0 * x) - 1.0f)/((float)exp(2.0 * x) + 1.0f);


   return r_tanh;
}

int main()  {
   float r_x, r_y;


   r_x = 0.1f;
   r_y = tanh_forward(r_x);
   printf("Tanh forward propagation for value x: %f\n", r_y);
   return 0;
}

Backward propagation EXAMPLE

/* ANSI C89, C99, C11 compliance                                                              */
/* The following example shows the usage of Hyperbolic tangent function backward propagation. */
#include <stdio.h>
#include <math.h>

float tanh_backward(float x){
   float r_tanh = ((float)exp(2.0 * x) - 1.0f)/((float)exp(2.0 * x) + 1.0f);


   return 1.0f - (r_tanh * r_tanh);
}

int main()  {
   float r_x, r_y;


   r_x = 0.1f;
   r_y = tanh_backward(r_x);
   printf("Tanh backward propagation for value x: %f\n", r_y);
   return 0;
}

REFERENCES:

0. TensorFlow Hyperbolic tangent

1. PyTorch Hyperbolic tangent