Here's a Python function to get the derivative of the ReLU activation function using numpy
:
Explanation:
numpy
as np
to use its array functions.relu_derivative
that takes an input array x
or a scalar as an argument.creates a boolean array with
Truewhere elements of
xare greater than
0 and False
elsewhere.derivative = np.where(condition, 1, 0)
uses np.where
to compute the derivative efficiently. It replaces True
values with 1
and False
values with 0
, resulting in an array that represents the derivative of the ReLU function for each element in the input.Here's an example of how to use the relu_derivative function with both single values and arrays of values:
Output:
In the first example, we calculate the derivative of the ReLU function for a single value (x_single = 2.0
), and it correctly returns 1
.
In the second example, we calculate the derivative for an array of values (x_array
), and it efficiently computes the derivative for each element in the array, resulting in [0 0 0 1 1]
, which is the derivative of the ReLU function for the corresponding input values.
Alternate Approach:
Here's a Python function that calculates the derivative using this mathematical definition:
It handles the case of x
being exactly equal to 0, which is typically considered undefined in the context of the ReLU derivative.