v9.1.1
Menu

ReLU Derivative

easyFREEBackpropagationDeep LearningCV: Deep Learning

Implement a function to compute the derivative of the Rectified Linear Unit (ReLU) activation function, a crucial component in Backpropagation. The ReLU function is defined as f(x)=max⁡(0,x)f(x) = \max(0, x), and its derivative is essential for training Deep Learning models.

The ReLU derivative is a piecewise function that depends on the input value xx. Understanding this derivative is vital for optimizing neural networks using Backpropagation, as it helps compute the gradients of the loss function with respect to the model's parameters.

To compute the ReLU derivative, follow these steps:

  1. Evaluate the input value xx.
  2. Apply the derivative formula based on the value of xx.
ddxReLU(x)={1x>00x≤0\frac{d}{dx}\text{ReLU}(x) = \begin{cases} 1 & x > 0 \\ 0 & x \leq 0 \end{cases}

This technique is widely used in Deep Learning models for image and speech recognition tasks.

Example

Input
relu_derivative([-1, 0, 1, 2])
Output
[0, 0, 1, 1]
Reasoning
  • The input is a list of pre-activation values xix_i, the numbers a neuron produces before ReLU is applied. Here x=(−1,0,1,2)x = (-1, 0, 1, 2).
  • The output has one entry per input: the slope of ReLU(x)=max⁡(0,x)\text{ReLU}(x) = \max(0, x) at that point, so four inputs give four slopes.
  • On the negative side ReLU is flat at 00, so its slope is 00: x1=−1x_1 = -1 gives 00.
  • At x2=0x_2 = 0 the function has a kink and no true derivative; the convention in the description (x≤0⇒0x \leq 0 \Rightarrow 0) assigns it 00.
  • On the positive side ReLU is the line y=xy = x with slope 11: x3=1x_3 = 1 and x4=2x_4 = 2 both give 11, whatever their size.
  • Result: [0, 0, 1, 1], whole numbers rather than decimals. Notice that during backpropagation this acts as a gate: the incoming gradient passes through unchanged where the entry is 11 and is blocked where it is 00.

Constraints

  • Return derivative for each element

More from CV: Deep Learning

solution.py

Test results

Sign in to run your code against the test cases.