Log inStart free
LESSON 5.02 · 6 STAGES · 3 CONTROLS · 3 CHECKS

MLP forward propagation

See one point flow through a 2-4-1 network and come out as a prediction.


EXPLAINER · 5.02 · MLP BAY
01
Stage 1 of 6 · The input
An MLP reads one input point, x = (0, 1), with XOR target 1.
Explain it simply

On the right you see the 4 corner points. Dots should give an output of 1, and squares should give 0. No single straight line splits the dots from the squares. The chosen point, 0 and 1, enters on the left as 2 cells.

Two input cells hold 0 and 1. The plane on the right marks this point among the 4 XOR points.

inputs and hidden valuesweights and biasesoutput ŷa 0 from ReLU, or a negative weight
Toy: a 2-4-1 MLP on the 4 XOR points; weights rounded to 2 decimals
HOVER OR TAP A SHAPE TO SEE ITS NUMBERS
(0, 1)
Pick the point that flows through the network.
trained
Trained weights, or the random weights before training.
ReLU
ReLU in the hidden layer, or no activation.
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
x=(x1,x2)\sy{x}{data}{\mathbf x} = (\sy{x}{data}{x_1}, \sy{x}{data}{x_2})
x=(0,1), y=1\mathbf x = (0, 1),\ y = 1

The input is one point with 2 numbers.

Show the code ▾Hide the code ▴ · Try it yourself · Python 3
import numpy as np

# the 4 XOR points (one batch) and their targets
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 1, 1, 0])

# weights trained by gradient descent, rounded to 2 decimals
W1 = np.array([[2.24, 2.24], [-2.86, 2.87], [-0.14, -0.87], [3.21, -1.48]])
b1 = np.array([-2.24, 0.0, 0.0, 0.0])
w2 = np.array([-3.82, 3.9, 0.07, 3.42])
b2 = -4.48

def relu(z):
    return np.maximum(0, z)

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

# forward propagation: all 4 points at once
Z = X @ W1.T + b1      # hidden weighted sums, one row per point
H = relu(Z)            # hidden outputs
z2 = H @ w2 + b2       # output weighted sums
y_hat = sigmoid(z2)    # outputs

i = 1  # the point the diagram shows: (0, 1)
print("z =", " ".join(f"{v:.2f}" for v in Z[i]))  # z = 0.00 2.87 -0.87 -1.48
print("h =", " ".join(f"{v:.2f}" for v in H[i]))  # h = 0.00 2.87 0.00 0.00
print(f"y_hat = {y_hat[i]:.3f}")  # y_hat = 0.999
print("batch:", " ".join(f"{v:.2f}" for v in y_hat))  # batch: 0.01 1.00 1.00 0.00

Copy it into a file and run it. It needs NumPy.

Common mix-ups

MYTHMore layers alone let a network learn XOR.

FIXWithout an activation function, two layers collapse into one straight cut. Trained that way, every point gets 0.50.

MYTHForward propagation changes the weights.

FIXForward propagation only reads the weights. Random weights get 2 of 4 points right. Training with backpropagation changes them.

Predict, then reveal
0 / 3 REVEALED
Q01Will the input (0, 0) give an output near 0 or near 1?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q02Remove the activation. How many of the 4 points does the network get right?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q03With random weights, how many of the 4 points does the network get right?
1 · GUESS2 · TRY IT IN THE DIAGRAM

Takeaways

  1. An MLP repeats one step: a weighted sum, then an activation.
  2. Forward propagation runs the layers in order, from input to output.
  3. Without the activation, the layers collapse into one straight cut.