Skip to content

Added Swish Activation Function #9692

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
38 changes: 38 additions & 0 deletions neural_network/activation_functions/swish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Swish Activation Function

Use Case: Enhances the expression of input data and weight to be learnt.
For more detailed information, you can refer to the following link:
https://en.wikipedia.org/wiki/Swish_function
"""

import numpy as np


def swish(vector: np.ndarray) -> np.ndarray:
"""
Implements the Swish activation function.

Parameters:
vector (np.ndarray): The input array for Swish activation.

Returns:
np.ndarray: The output array after applying the Swish activation.

Formula: f(x) = x / (1 + np.exp(-x))

Examples:
>>> swish(vector=np.array([2.3,0.6,-2,-3.8]))
array([ 2.09041719, 0.38739378, -0.23840584, -0.08314883])

>>> swish(np.array([-9.2, -0.3, 0.45, -4.56]))
array([-0.00092947, -0.12766724, 0.27478766, -0.04721304])

"""
return vector / (1 + np.exp(-vector))


if __name__ == "__main__":
import doctest

doctest.testmod()