ReLU Activation

~10 mincode completion

Implement that applies ReLU element-wise.

Examples

Mixed positive and negative values

Input
relu([-3, -1, 0, 1, 3])
Output
[0, 0, 0, 1, 3]

2D array

Input
relu([[1, -1], [2, -2]])
Output
[[1, 0], [2, 0]]

All negative: all zeros

Input
relu([-5, -4, -3, -2, -1])
Output
[0, 0, 0, 0, 0]

Hints

Hint 1

compares elementwise against a scalar or another array.

Hint 2

Reach for np maximum rather than np max.

Requirements

  • z: Scalar or NumPy array

  • Return max(0, z) applied element-wise, same shape as input.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~10 min

8 employers weight this skill

4 frontier labs, 2 big tech firms, 1 autonomy company, 1 enterprise vendor. Top match scores 93.

Python
import numpy as np

def relu(z):
    """
    Apply the ReLU activation function element-wise.

    Args:
        z: Scalar or NumPy array

    Returns:
        max(0, z) applied element-wise, same shape as input.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.