-
Notifications
You must be signed in to change notification settings - Fork 0
/
PReLU.py
52 lines (42 loc) · 2.75 KB
/
PReLU.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
#******************************************************************************************************
# Class representing the PReLU (Parametric rectified linear unit) activation function *
# *
# The formula implemented in the code for the PReLU activation function is: *
# *
# ---> For the activation function: f(x) = { alpha * x, if x < 0 *
# x, if x >= 0 }, *
# *
# where alpha is the parameter that determines the slope for negative values. *
# *
# ---> For the derivative of the activation function: f'(x) = { alpha, if x < 0 *
# 1, if x >= 0 }, *
# *
# which is a piecewise function that depends on the value of x. *
# *
# ---> This implementation allows the PReLU activation function to have different slopes *
# for negative values (alpha * x) compared to positive values (x), providing more *
# flexibility in modeling the activation behavior. *
# *
#******************************************************************************************************
import numpy as np
class PReLU:
def __init__(self, alpha=0.01):
self.alpha = alpha
def activation(self, x):
"""
Compute the PReLU activation function for the input x.
Args:
x (numpy.ndarray): Input array.
Returns:
numpy.ndarray: Output of the PReLU activation function.
"""
return np.where(x < 0, self.alpha * x, x)
def derivative(self, x):
"""
Compute the derivative of the PReLU activation function for the input x.
Args:
x (numpy.ndarray): Input array.
Returns:
numpy.ndarray: Derivative of the PReLU activation function.
"""
return np.where(x < 0, self.alpha, 1)