Back to Practice
#0017
ReLU Forward Pass
EasyML10 min10 XP
Problem
Given a NumPy array x, return ReLU(x), where negative values become 0 and positive values stay unchanged.
Why This Matters
ReLU is the tiny operation hiding inside many deep networks. Understanding it makes activation functions feel less mysterious.
Function Signature
def relu(x):
Examples
Example 1
Inputx = np.array([-2, 0, 3, -1, 5])
Outputarray([0, 0, 3, 0, 5])
Negative numbers are clipped to 0. Non-negative numbers pass through.
Constraints
- Input may be a scalar, vector, or matrix.
- Return a NumPy array or scalar with the same shape.
- Do not use Python loops for array inputs.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 4 hidden categories
Vector with mixed signs
Input[-2, 0, 3, -1, 5]
Expected[0, 0, 3, 0, 5]
ReLU keeps positive signal and removes negative signal.
Matrix input
Input[[-1, 2], [3, -4]]
Expected[[0, 2], [3, 0]]
The same rule is applied independently to every cell.
Hidden Test Categories
All negative valuesAll positive valuesFloating point valuesScalar input