Lesson 3.1 · 11 min read · about 1 h 5 min to master at a very slow pace
Layers, Matrices and Shapes
A multi-layer perceptron stacks layers of neurons. How one layer is written as a matrix product, how to read the shapes, how to count parameters, and the two conventions (lecture vs code) you must not mix.
One layer as a matrix product
A layer applies many neurons to the same input at once. Stack each neuron's weights as a row of a matrix and its bias into a vector :
The output of one layer is the input of the next. Computing all layers in order is the forward pass.
Reading the shapes
For a layer with inputs and neurons:
- is (one row per neuron, one column per input);
- has entries;
- the layer has trainable parameters.
Worked examples: counting parameters
| Network | Layer 1 | Layer 2 | Total |
|---|---|---|---|
| 2 → 3 → 1 | 13 | ||
| 100 → 50 → 10 | 5,560 | ||
| 784 → 128 → 10 | 101,770 |
Almost all the parameters sit in the widest connections. A 28×28 image flattened to 784 inputs already needs 100,352 weights for one modest hidden layer, which is part of why images use convolutions (Section 10).
Worked example: a 2–3–1 network, neuron by neuron
Two inputs, three hidden neurons with ReLU, and one output neuron with the identity activation (so it can output any number, as in regression). Each neuron has its own weights and bias.
The forward pass for
| Neuron | Weights | Bias | Weighted sum | Output |
|---|---|---|---|---|
| identity: |
- Information flows one way: the inputs feed every hidden neuron, and the three hidden outputs feed the output neuron.
- 's weighted sum is negative, so ReLU outputs 0: for this input, contributes nothing.
- Parameters: in the hidden layer, at the output: 13 in all.
Two conventions: don't mix them
| Lecture (maths) | Code (NumPy, PyTorch) | |
|---|---|---|
| Input | One example, a column vector | A batch, matrix with one example per row |
| Weight shape | : outputs × inputs | : inputs × outputs |
| Layer |
Both are correct; they are transposes of each other. State which one you use and check that every product has matching inner dimensions.
Stuck? Common doubts
Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.
First passWhy is the weight matrix outputs × inputs and not the other way round?
Because each row holds one neuron's weights, and a matrix–vector product takes the dot product of each row with . With neurons and inputs, that's rows of length . Code that stores one example per row uses the transpose, inputs × outputs, so that works.
First passDo all neurons in a layer have the same weights and biases?
No. Each neuron has its own weights and bias, and that's the point: each one learns to detect something different. If they all started identical, they would get identical gradients and stay identical forever, so the layer would act like one neuron. That's why weights start random.
First passHow do the weights and the bias change a neuron's output?
Each weight scales how much one input matters, and its sign says whether that input pushes the output up or down. The bias shifts the weighted sum up or down regardless of the inputs, which moves the point where ReLU switches on. In the example, raising 's bias from to would make its sum , so it would switch on.
First passHow do I multiply a 1×2 by a 2×1, and a 2×1 by a 1×2?
Write the shapes side by side. The inner numbers must match, and the result has the outer numbers.
| Product | Shapes | Result | Example |
|---|---|---|---|
| Row × column | (1×2)(2×1) | 1×1: one number (a dot product) | |
| Column × row | (2×1)(1×2) | 2×2: a full table (an outer product) |
Both appear in a network. The forward pass uses dot products: . Backpropagation uses an outer product for a layer's weight gradient, : every entry is (error at that output) × (input on that connection).
Thumb rule: "row times column gives a number; column times row gives a table."
Cheatsheet
Layer and parameter count
Shape conventions
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.