Skip to content
Easy End-to-End

Two-Layer MLP

A multilayer perceptron (MLP) combines fully connected layers with activation functions. This exercise has two weight transformations, with ReLU between them. ReLU replaces negative values with zero and leaves positive values unchanged.

The forward pass is

h = ReLU(x @ W1 + b1)

y = h @ W2 + b2

For x = [[1, -1]], identity W1, and zero b1, the first transformation returns [[1, -1]] and ReLU gives [[1, 0]]. With W2 = [[2], [-3]] and b2 = [-4], the final output is [[-2]]. That negative output is valid: there is no ReLU on the final layer.

Your task

Implement two_layer_mlp(x, W1, b1, W2, b2) for floating-point tensors:

Input Shape
x (N, I)
W1 (I, H)
b1 (H,)
W2 (H, O)
b2 (O,)

N is the batch size, I the input width, H the hidden width, and O the output width. All are positive. The hidden result has shape (N, H); return the final result with shape (N, O).

Here the weight matrices are already stored in multiplication order, so do not transpose them. This differs from the (out_features, in_features) convention in the separate linear-layer exercise. Broadcast each bias across batch rows. Add b1 before ReLU, and add b2 after the second matrix multiplication. Do not apply softmax, a final activation, or any training update. Use the supplied parameters as given.