Skip to content

Commit f6b1242

Browse files
Added Leaky ReLU Activation Function (TheAlgorithms#8962)
* Added Leaky ReLU activation function * Added Leaky ReLU activation function * Added Leaky ReLU activation function * Formatting and spelling fixes done
1 parent fd7cc4c commit f6b1242

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Leaky Rectified Linear Unit (Leaky ReLU)
3+
4+
Use Case: Leaky ReLU addresses the problem of the vanishing gradient.
5+
For more detailed information, you can refer to the following link:
6+
https://en.wikipedia.org/wiki/Rectifier_(neural_networks)#Leaky_ReLU
7+
"""
8+
9+
import numpy as np
10+
11+
12+
def leaky_rectified_linear_unit(vector: np.ndarray, alpha: float) -> np.ndarray:
13+
"""
14+
Implements the LeakyReLU activation function.
15+
16+
Parameters:
17+
vector (np.ndarray): The input array for LeakyReLU activation.
18+
alpha (float): The slope for negative values.
19+
20+
Returns:
21+
np.ndarray: The input array after applying the LeakyReLU activation.
22+
23+
Formula: f(x) = x if x > 0 else f(x) = alpha * x
24+
25+
Examples:
26+
>>> leaky_rectified_linear_unit(vector=np.array([2.3,0.6,-2,-3.8]), alpha=0.3)
27+
array([ 2.3 , 0.6 , -0.6 , -1.14])
28+
29+
>>> leaky_rectified_linear_unit(np.array([-9.2, -0.3, 0.45, -4.56]), alpha=0.067)
30+
array([-0.6164 , -0.0201 , 0.45 , -0.30552])
31+
32+
"""
33+
return np.where(vector > 0, vector, alpha * vector)
34+
35+
36+
if __name__ == "__main__":
37+
import doctest
38+
39+
doctest.testmod()

0 commit comments

Comments
 (0)