Hyperbolic Tangent (tanh) Activation Function [with python code]

by keshav


tanh activation function-new.png

The tanh function is similar to the sigmoid function i.e. has a shape somewhat like S. The output ranges from -1 to 1.

 

The Mathematical function of tanh function is:

tanh

Derivative of tanh function is:

derivative tanh

Also Read:

Python Code

 

import numpy as np
import matplotlib.pyplot as plt

# Hyperbolic Tangent (htan) Activation Function
def htan(x):
  return (np.exp(x) - np.exp(-x))/(np.exp(x) + np.exp(-x))

# htan derivative
def der_htan(x):
  return 1 - htan(x) * htan(x)

# Generating data for Graph
x_data = np.linspace(-6,6,100)
y_data = htan(x_data)
dy_data = der_htan(x_data)

# Graph
plt.plot(x_data, y_data, x_data, dy_data)
plt.title('htan Activation Function & Derivative')
plt.legend(['htan','der_htan'])
plt.grid()
plt.show()

 

tanh acticvation function and dericative

 

 

to read more about activation functions - link


No Comments


Post a Comment