Python/maths/tanh.py

41 lines
1.1 KiB
Python
Raw Normal View History

2023-04-23 17:01:55 +05:30
"""
This script demonstrates the implementation of the tangent hyperbolic or tanh function.
The function takes a vector of K real numbers as input and then (e^x - e^(-x))/(e^x + e^(-x)).
After through tanh, the element of the vector mostly -1 between 1.
Script inspired from its corresponding Wikipedia article
https://en.wikipedia.org/wiki/Activation_function
"""
import numpy
import numpy as np
2023-04-23 17:34:24 +05:30
import doctest
2023-04-23 17:01:55 +05:30
2023-04-23 17:34:24 +05:30
def tanh_func(vector: np.array) -> np.array:
2023-04-23 17:24:14 +05:30
"""
2023-04-23 17:01:55 +05:30
Implements the tanh function
Parameters:
2023-04-23 17:34:24 +05:30
vector: np.array
2023-04-23 17:01:55 +05:30
Returns:
tanh (np.array): The input numpy array after applying
tanh.
mathematically (e^x - e^(-x))/(e^x + e^(-x)) can be written as (2/(1+e^(-2x))-1
2023-04-23 17:24:14 +05:30
Examples:
>>> tanh_func(np.array([1, 5, 6, 113, 13, 16, -5.23]))
2023-04-23 17:34:24 +05:30
array([ 0.76159416, 0.9999092 , 0.99998771, 1. , 1. ,
1. , -0.99994268])
2023-04-23 17:24:14 +05:30
"""
2023-04-23 17:01:55 +05:30
exp_vector = np.exp(-2 * vector)
return (2 / (1 + exp_vector)) - 1
if __name__ == '__main__':
2023-04-23 17:34:24 +05:30
doctest.testmod()