-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Added Scaled Exponential Linear Unit Activation Function #9027
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
tianyizheng02
merged 11 commits into
TheAlgorithms:master
from
AdarshAcharya5:feat/SELU_activation
Sep 6, 2023
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b3ca5a8
Added Scaled Exponential Linear Unit Activation Function
AdarshAcharya5 cf66ff8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b106229
Update scaled_exponential_linear_unit.py
AdarshAcharya5 563d5b7
Update scaled_exponential_linear_unit.py
AdarshAcharya5 5544cc2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 01758c1
Update scaled_exponential_linear_unit.py
AdarshAcharya5 cd2484e
Update scaled_exponential_linear_unit.py
AdarshAcharya5 185d2fe
Update scaled_exponential_linear_unit.py
AdarshAcharya5 6a08c33
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b5a4d22
Update scaled_exponential_linear_unit.py
AdarshAcharya5 76a11d1
Update scaled_exponential_linear_unit.py
AdarshAcharya5 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
44 changes: 44 additions & 0 deletions
44
neural_network/activation_functions/scaled_exponential_linear_unit.py
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,44 @@ | ||
""" | ||
Implements the Scaled Exponential Linear Unit or SELU function. | ||
The function takes a vector of K real numbers and two real numbers | ||
alpha(default = 1.6732) & lambda (default = 1.0507) as input and | ||
then applies the SELU function to each element of the vector. | ||
SELU is a self-normalizing activation function. It is a variant | ||
of the ELU. The main advantage of SELU is that we can be sure | ||
that the output will always be standardized due to its | ||
self-normalizing behavior. That means there is no need to | ||
include Batch-Normalization layers. | ||
References : | ||
https://iq.opengenus.org/scaled-exponential-linear-unit/ | ||
""" | ||
|
||
import numpy as np | ||
|
||
|
||
def scaled_exponential_linear_unit( | ||
vector: np.ndarray, alpha: float = 1.6732, _lambda: float = 1.0507 | ||
) -> np.ndarray: | ||
""" | ||
Applies the Scaled Exponential Linear Unit function to each element of the vector. | ||
Parameters : | ||
vector : np.ndarray | ||
alpha : float (default = 1.6732) | ||
_lambda : float (default = 1.0507) | ||
|
||
Returns : np.ndarray | ||
Formula : f(x) = _lambda * x if x > 0 | ||
_lambda * alpha * (e**x - 1) if x <= 0 | ||
Examples : | ||
>>> scaled_exponential_linear_unit(vector=np.array([1.3, 3.7, 2.4])) | ||
array([1.36591, 3.88759, 2.52168]) | ||
|
||
>>> scaled_exponential_linear_unit(vector=np.array([1.3, 4.7, 8.2])) | ||
array([1.36591, 4.93829, 8.61574]) | ||
""" | ||
return _lambda * np.where(vector > 0, vector, alpha * (np.exp(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.
Is there a reason why
_lambda
has an underscore? Is the user not meant to change this coefficient?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.
Yes, firstly lambda is a reserved keyword in python. Secondly both alpha and lambda are fixed constants.
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.
The user can change these values, that may yield slightly different behaviour from the function, but the default values given to them are
alpha: float = 1.6732, _lambda: float = 1.0507
.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.
Oh yeah, duh 🤦
Could you rename the variable to something like
lambda_
instead? Having an underscore at the start of a variable name generally signifies that the user isn't supposed to use it.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.
Ah. Yes for sure, I'll get it done right away!
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.
Done