Skip to content

Commit ad4bd92

Browse files
kausthub-kannantianyizheng02
authored andcommitted
Added Squareplus Activation Function (TheAlgorithms#9977)
* Added Squareplus Activation Function * Added parameter beta to the function * Fixed Squareplus Function * Update neural_network/activation_functions/squareplus.py --------- Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
1 parent 984f2a6 commit ad4bd92

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Squareplus Activation Function
3+
4+
Use Case: Squareplus designed to enhance positive values and suppress negative values.
5+
For more detailed information, you can refer to the following link:
6+
https://en.wikipedia.org/wiki/Rectifier_(neural_networks)#Squareplus
7+
"""
8+
9+
import numpy as np
10+
11+
12+
def squareplus(vector: np.ndarray, beta: float) -> np.ndarray:
13+
"""
14+
Implements the SquarePlus activation function.
15+
16+
Parameters:
17+
vector (np.ndarray): The input array for the SquarePlus activation.
18+
beta (float): size of the curved region
19+
20+
Returns:
21+
np.ndarray: The input array after applying the SquarePlus activation.
22+
23+
Formula: f(x) = ( x + sqrt(x^2 + b) ) / 2
24+
25+
Examples:
26+
>>> squareplus(np.array([2.3, 0.6, -2, -3.8]), beta=2)
27+
array([2.5 , 1.06811457, 0.22474487, 0.12731349])
28+
29+
>>> squareplus(np.array([-9.2, -0.3, 0.45, -4.56]), beta=3)
30+
array([0.0808119 , 0.72891979, 1.11977651, 0.15893419])
31+
"""
32+
return (vector + np.sqrt(vector**2 + beta)) / 2
33+
34+
35+
if __name__ == "__main__":
36+
import doctest
37+
38+
doctest.testmod()

0 commit comments

Comments
 (0)