forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tanh.py
42 lines (28 loc) · 1.1 KB
/
tanh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
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 as np
def tangent_hyperbolic(vector: np.array) -> np.array:
"""
Implements the tanh function
Parameters:
vector: np.array
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
Examples:
>>> tangent_hyperbolic(np.array([1,5,6,-0.67]))
array([ 0.76159416, 0.9999092 , 0.99998771, -0.58497988])
>>> tangent_hyperbolic(np.array([8,10,2,-0.98,13]))
array([ 0.99999977, 1. , 0.96402758, -0.7530659 , 1. ])
"""
return (2 / (1 + np.exp(-2 * vector))) - 1
if __name__ == "__main__":
import doctest
doctest.testmod()