All problems
ReLU With Gradient
Problem
Apply ReLU and return the forward values plus local gradients.
Return [outputs, grads] where outputs[i] = max(0, x_i) and grads[i] is 1.0 when the input is strictly positive, otherwise 0.0. An empty input returns [[], []].
Hint
The derivative of ReLU is a step function: 1 above zero, 0 elsewhere.
Test cases
case 1
in: [[-1,0,2]]
expected: [[0,0,2],[0,0,1]]
case 2
in: [[5]]
expected: [[5],[1]]
case 3
in: [[]]
expected: [[],[]]
case 4
in: [[-3,-0.5]]
expected: [[0,0],[0,0]]
Starter codePython
def relu_with_grad(x):
# Return [outputs, grads]
# Your code here
pass4 test cases · run your code against them in the editor