-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathparametric_relu.py
88 lines (68 loc) · 2.28 KB
/
parametric_relu.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Parametric Rectified Linear Unit (PReLU)
Use Case: PReLU addresses the problem of dying ReLU by allowing a
small, learnable slope for negative values, which can improve model
performance.
For more detailed information, you can refer to the following link:
https://en.wikipedia.org/wiki/Rectifier_(neural_networks)#Parametric_ReLU
"""
import numpy as np
def parametric_rectified_linear_unit(
vector: np.ndarray, alpha: np.ndarray
) -> np.ndarray:
"""
Implements the Parametric ReLU (PReLU) activation function.
Parameters:
vector (np.ndarray): The input array for PReLU activation.
alpha (np.ndarray): The learnable slope for negative values,
must be the same shape as vector.
Returns:
np.ndarray: The input array after applying the PReLU activation.
Formula:
f(x) = x if x > 0 else f(x) = alpha * x
Examples:
>>> parametric_rectified_linear_unit(
... vector=np.array([2.3, 0.6, -2, -3.8]),
... alpha=np.array([0.3])
... )
array([ 2.3 , 0.6 , -0.6 , -1.14])
>>> parametric_rectified_linear_unit(
... vector=np.array([-9.2, -0.3, 0.45, -4.56]),
... alpha=np.array([0.067])
... )
array([-0.6164 , -0.0201 , 0.45 , -0.30552])
>>> parametric_rectified_linear_unit(
... vector=np.array([0, 0, 0]),
... alpha=np.array([0.1, 0.1, 0.1])
... )
array([0., 0., 0.])
>>> parametric_rectified_linear_unit(
... vector=np.array([-1, -2, -3]),
... alpha=np.array([0.5, 1, 1.5])
... )
array([-0.5, -2. , -4.5])
>>> parametric_rectified_linear_unit(
... vector=np.array([-1, 2, -3]),
... alpha=np.array([1, 0.5, 2])
... )
array([-1., 2., -6.])
>>> parametric_rectified_linear_unit(
... vector=np.array([-5, -10]),
... alpha=np.array([2, 3])
... )
array([-10, -30])
>>> parametric_rectified_linear_unit(
... vector=np.array([-1, -2]),
... alpha=np.array([1, 0])
... )
array([-1, 0])
>>> parametric_rectified_linear_unit(
... vector=np.array([1, -1]),
... alpha=np.array([0.5, 2])
... )
array([ 1., -2.])
"""
return np.where(vector > 0, vector, alpha * vector)
if __name__ == "__main__":
import doctest
doctest.testmod()