ReLU Activation

Easy
~10 min
code completion

ReLU: Rectified Linear Unit

ReLU is the most popular activation function in deep learning:

It zeroes out negative values and passes positive values unchanged. Key advantages:

  • Cheap to compute
  • Does not saturate for positive inputs (no vanishing gradient problem)
  • Induces sparsity (dead neurons for negative inputs)
  • In NumPy: np.maximum(0, z)

    Note: np.maximum(0, z) is element-wise; np.max(z) takes the global max — different things!

    Your task:

    Implement relu(z) that applies ReLU element-wise.

    Example Tests

    Mixed positive and negative values

    Input: {"z":[-3,-1,0,1,3]}

    Expected: [0,0,0,1,3]

    2D array

    Input: {"z":[[1,-1],[2,-2]]}

    Expected: [[1,0],[2,0]]

    All negative: all zeros

    Input: {"z":[-5,-4,-3,-2,-1]}

    Expected: [0,0,0,0,0]

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.