mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
bc8df6de31
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.2.2...v0.3.2) - [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
"""
|
|
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.ndarray) -> np.ndarray:
|
|
"""
|
|
Implements the tanh function
|
|
|
|
Parameters:
|
|
vector: np.ndarray
|
|
|
|
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()
|