Skip to content

added tanh function to activation functions #9854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions neural_network/activation_functions/tanh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Hyperbolic Tangent Function (Tanh)
reference : https://paperswithcode.com/method/tanh-activation#:~:text=Tanh%20Activation%20is%20an%20activation,for%20multi%2Dlayer%20neural%20networks.

The hyperbolic tangent function, commonly denoted as "tanh," is a mathematical
function that maps real numbers to the range (-1, 1). It's defined as:

tanh(x) = (e^x - e^(-x)) / (e^x + e^(-x))

Where:
- e is the base of the natural logarithm (approximately 2.71828).
- x is the input value to the function.

The tanh function's curve is "S" shaped and centered at the origin (0,0). It
behaves as follows:
- As x approaches negative infinity, tanh(x) approaches -1.
- As x approaches positive infinity, tanh(x) approaches 1.
- At x = 0, tanh(x) equals 0.

In machine learning, tanh is often used as an activation function in neural
networks, introducing non-linearity and squishing neuron outputs to a range
between -1 and 1.

"""
import numpy as np


def tanh(vector: np.ndarray) -> np.ndarray:
"""_summary_
Args:
vector (np.ndarray): numpy array
Returns:
np.ndarray: numpy array after applying tanh
Examples :
>>> tanh(vector=np.array([-2, -1, 0, 1, 2]))
array([-0.96402758, -0.76159416, 0. , 0.76159416, 0.96402758])
"""
return (np.exp(vector) - np.exp(-vector)) / (np.exp(vector) + np.exp(-vector))


if __name__ == "__main__":
import doctest

doctest.testmod()