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 , and say what each row and column of 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 , works out a weighted sum , 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.
One neuron, four inputs: . 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 , and their biases into a vector . Then all three weighted sums come out of one matrix–vector product (lesson 1.4):
Row of is neuron 's weights, and column holds every neuron's weight on input . With 4 inputs and 3 neurons, is : outputs by inputs.
The first number, , is the single neuron from step 1, now row 1. Each neuron then applies its activation. This lesson uses ReLU, , 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:
Point at any line in the diagram to find its weight in the matrix, or at any cell to find its line:
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 with one example per row (lesson 2.3), shape . To run the layer on every example at once, multiply by the transpose of :
is , is for a layer of neurons, so is : one row per example, one column per neuron (the shape rule, lesson 0.6). Adding , shape , broadcasts it onto every row (lesson 0.5).
Row 0 of 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 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:
The superscript says which layer a weight belongs to (see the notation table). The hidden layer's outputs 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:
Five examples go in as ; the hidden layer makes ; the output layer . The network has parameters, the numbers training will set. A layer from inputs to neurons always has . Change the sizes and watch every shape follow:
(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): and are different things, and often only one of them exists. Write the layer as W @ X instead of X @ W.T:
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 , 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 . 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
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.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 6: Deep Feedforward Networks: layers, hidden units, and the forward pass
- Prince, Understanding Deep Learning, chapter 4: Deep neural networks: networks as matrices, and their notation
- NumPy: numpy.matmul: the @ operator, and when its shapes don't fit
- PyTorch: torch.nn.Linear: the same layer in PyTorch, which also stores W with one row per neuron