Two-Layer Network Forward Pass (and Why It Solves XOR)

~15 mincode completion

Implement mlp_forward(X, W1, b1, W2, b2) returning the list of probabilities , one per row of . Compute the sigmoid in a way that does not overflow for very large positive or negative .

Examples

XOR truth table (the worked example)

Input
mlp_forward([[0, 0], [0, 1], [1, 0], [1, 1]], [[1, 1], [1, 1]], [0, -1], [10, -20], -5)
Output
[0.006693, 0.993307, 0.993307, 0.006693]

One row, three inputs, two hidden units

Input
mlp_forward([[0.5, -1, 2]], [[1, -1], [0.5, 2], [-0.25, 1]], [0.1, -0.2], [0.8, -0.6], 0.3)
Output
[0.574443]

ReLU zeroes negative pre-activations

Input
mlp_forward([[-3, -3], [3, 3]], [[1, 0], [0, 1]], [0, 0], [1, 1], 0)
Output
[0.5, 0.997527]

Hints

Hint 1

applies elementwise, so negate the whole array and exponentiate it in one go.

Hint 2

Do not forget to relu so layers collapse. That step is easy to skip.

Requirements

  • X: inputs, shape (n, d)

  • W1: hidden weights, shape (d, k)

  • b1: hidden biases, shape (k,)

  • W2: output weights, shape (k,)

  • b2: output bias, a float

  • Return List of n probabilities.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~15 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 mlp_forward(X, W1, b1, W2, b2) -> list:
    """
    Forward pass of a 2-layer network: ReLU hidden layer, sigmoid output.

    Args:
        X:  inputs, shape (n, d)
        W1: hidden weights, shape (d, k)
        b1: hidden biases, shape (k,)
        W2: output weights, shape (k,)
        b2: output bias, a float

    Returns:
        List of n probabilities.
    """
    X = np.asarray(X, dtype=float)
    # 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.