-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
The tanh activation function is added #8689
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
60235f4
tanh function been added
TMGA-WAY dbea59d
tanh function been added
TMGA-WAY 6e3f28d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d42af7b
tanh function is added
TMGA-WAY 84ea4c1
tanh function is added
TMGA-WAY fa675a4
tanh function is added
TMGA-WAY a6b5d05
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2827b15
tanh function added
TMGA-WAY 8092744
tanh function added
TMGA-WAY 9114b52
tanh function added
TMGA-WAY bbf4067
Merge branch 'TheAlgorithms:master' into master
TMGA-WAY a4ca897
tanh function is added
TMGA-WAY e049502
Apply suggestions from code review
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor suggestion I have is to add a brief description of what the function does at the beginning of the docstring. For example:
"""
Implements the tangent hyperbolic or tanh function.
The function takes a vector of K real numbers as input and then applies
the tanh function to each element of the vector. The output values are
mostly in the range (-1, 1).
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you for suggestion✌✌.