Neural networks · lesson 1 of 10
Parts in this lesson
Az = W @ x + ba layer: many neurons' weighted sums at once
BZ = X @ W.T + bthe same layer for a batch, one example per row
CA1 = relu(X @ W1.T + b1); Z2 = A1 @ W2.T + b2two layers stacked: the forward pass
DX @ w + b5.2: one neuron's weighted sum

Layers of neurons

Last updated

Lesson 5.6 solved XOR with two hand-made hidden units feeding one output unit. A neural network is that idea made regular: neurons arranged in layers, every neuron in a layer looking at every output of the layer before. This lesson writes a layer as one matrix product, stacks two of them into a network, and follows the shape of every array from the input to the output.

By the end of this lesson you can

  • Write a layer of neurons as Wx+b, and say what each row and column of W holds
  • Run a layer on a whole batch of examples at once with X @ W.T + b
  • Write a two-layer network's forward pass in NumPy and give the shape of every array in it
  • Count a network's parameters

A layer is many neurons at once

One neuron (Stage 5) takes an input vector x, works out a weighted sum z=w⊤x+b, and passes it through an activation. Lesson 5.6's XOR network had two such neurons, OR and AND, both looking at the same two inputs, and a third looking at their outputs.

A layer is a group of neurons that all look at the same inputs, each with its own weights and bias. A network is layers in a row: the outputs of one layer are the inputs of the next. The first layer looks at the data; the last layer's outputs are the predictions; the layers in between are hidden, because the data never says what their outputs should be.

layers.ipynb

One neuron, four inputs: z=−0.4. For three neurons, write that line three times, or once, with a matrix.

Wx + b

Stack the three neurons' weight vectors as the rows of a matrix W, and their biases into a vector b. Then all three weighted sums come out of one matrix–vector product (lesson 1.4):

z=Wx+bzi=∑jWijxj+bi

Row i of W is neuron i's weights, and column j holds every neuron's weight on input xj. With 4 inputs and 3 neurons, W is 3×4: outputs by inputs.

layers.ipynb

The first number, −0.4, is the single neuron from step 1, now row 1. Each neuron then applies its activation. This lesson uses ReLU, ReLU⁡(z)=max⁡(z,0), which keeps positive sums and turns negative ones into 0; lesson 6.2 explains why a network needs one, and why this one is popular:

layers.ipynb

Point at any line in the diagram to find its weight in the matrix, or at any cell to find its line:

Network diagram4 inputs → 3 hidden neurons → 2 outputs
x₁x₂x₃x₄a₁a₂a₃z₁z₂x: (4,)a¹: (3,)z²: (2,)W¹ (3 × 4)0.20.4−0.10.3−0.50.10.60.00.3−0.20.2−0.4W² (2 × 3)0.7−0.30.5−0.60.80.1row = one neuron · column = one input

Weight W¹ row 2, column 3 = 0.6: hidden neuron 2 multiplies input x₃ by it.

A batch at a time

Data comes as a matrix X with one example per row (lesson 2.3), shape (n,d). To run the layer on every example at once, multiply by the transpose of W:

Z=XW⊤+b

X is (n,d), W⊤ is (d,k) for a layer of k neurons, so Z is (n,k): one row per example, one column per neuron (the shape rule, lesson 0.6). Adding b, shape (k,), broadcasts it onto every row (lesson 0.5).

layers.ipynb

Row 0 of Z is the first example, and it matches step 2's answer exactly. This is the form every library uses. PyTorch's nn.Linear also keeps W as outputs by inputs and computes x @ W.T + b, so this course does the same, and Stage 7's code will look familiar.

Stacking layers

A two-layer network is two of these, with an activation between:

A[1]=ReLU⁡(XW[1]⊤+b[1])Z[2]=A[1]W[2]⊤+b[2]

The superscript [l] says which layer a weight belongs to (see the notation table). The hidden layer's outputs A[1] become the inputs of layer 2, so layer 2's weight matrix must have as many columns as layer 1 has neurons. Running all the layers, input to output, is the network's forward pass:

layers.ipynb

Five examples go in as (5,4); the hidden layer makes (5,3); the output layer (5,2). The network has 3×4+3+2×3+2=23 parameters, the numbers training will set. A layer from d inputs to k neurons always has dk+k. Change the sizes and watch every shape follow:

Shapes through the networkthe forward pass, to scale
X(5, 4)@W¹ᵀ(4, 3)→A¹(5, 3)@W²ᵀ(3, 2)→Z²(5, 2)

(5, 4) @ (4, 3) gives (5, 3); (5, 3) @ (3, 2) gives (5, 2). Each inner pair matches, and the batch size n rides through untouched. Parameters: 4 × 3 + 3 + 3 × 2 + 2 = 23, whatever n is.

The output layer has no ReLU here. What it gets depends on the task: nothing for regression, a softmax for classes (lesson 3.11). Lesson 6.4 uses a softmax.

Wrong way: the product the wrong way round

Matrix products don't commute (lesson 1.4): AB and BA are different things, and often only one of them exists. Write the layer as W @ X instead of X @ W.T:

layers.ipynb

NumPy refuses: matmul: Input operand 1 has a mismatch in its core dimension 0 ... (size 5 is different from 4). Read it as the shape rule failing: W is (3,4), so it wants something with 4 rows on its right, and X has 5 rows, one per example. NumPy names the operands from 0, so "operand 1" is the second one, X.

When a shape error appears, write the shapes beside the line, as in step 4's figure, and the fix is usually plain: the inner sizes must match. Here, X @ W.T is (5,4)@(4,3). Be glad when it's an error: lesson 3.4 showed a shape mistake that broadcast silently into wrong numbers instead.

Check what you learned

A layer takes 10 inputs and has 6 neurons. What shape is its weight matrix W, as this course stores it?
X has shape (32, 10) and the layer has W of shape (6, 10). What shape is X @ W.T + b?
How many parameters has a network with 64 inputs, one hidden layer of 32 neurons and 10 outputs?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning