forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswish.py
More file actions
38 lines (25 loc) · 947 Bytes
/
Copy pathswish.py
File metadata and controls
38 lines (25 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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()