forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquareplus.py
38 lines (26 loc) · 1.02 KB
/
squareplus.py
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
"""
Squareplus Activation Function
Use Case: Squareplus designed to enhance positive values and suppress negative values.
For more detailed information, you can refer to the following link:
https://en.wikipedia.org/wiki/Rectifier_(neural_networks)#Squareplus
"""
import numpy as np
def squareplus(vector: np.ndarray, beta: float) -> np.ndarray:
"""
Implements the SquarePlus activation function.
Parameters:
vector (np.ndarray): The input array for the SquarePlus activation.
beta (float): size of the curved region
Returns:
np.ndarray: The input array after applying the SquarePlus activation.
Formula: f(x) = x^2 if x > 0 else f(x) = 0
Examples:
>>> squareplus(np.array([2.3, 0.6, -2, -3.8]), beta=2)
array([5.29, 0.36, 0. , 0. ])
>>> squareplus(np.array([-9.2, -0.3, 0.45, -4.56]), beta=3)
array([0. , 0. , 0.091125, 0. ])
"""
return np.where(vector > 0, vector**beta, 0)
if __name__ == "__main__":
import doctest
doctest.testmod()