Hyperbolic tangent (Tanh)DescriptionIn the context of artificial neural networks, the Hyperbolic tangent is an activation function defined as: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() 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: |