Layers as Matrix Multiplication
A layer is many neurons reading the same inputs, so stacking their weight vectors as the rows of a matrix turns the whole layer into one matrix-vector product, . The shapes tell you everything: has one row per neuron in the layer and one column per input.
What the symbols mean
- Previous activations
What the layer before put out. For the first layer, it's the input x.
- Weight matrix
Row i holds the weights of neuron i in layer l; entry (i, j) is the weight from input j into neuron i.
- Bias vector
One bias per neuron in layer l.
- Activation
Applied to each entry separately.
- This layer's output
One number per neuron, passed on to the next layer.
What the symbols mean
- Width of layer l
How many neurons layer l has.
- Width of the layer before
How many inputs each neuron in layer l reads.
Explanation
A layer is simply a group of neurons that all look at the same inputs at the same time, each with its own weights. Writing every neuron's sum out by hand gets messy fast (a layer of neurons reading pixels is long sums), so we pack the weights into a matrix and let matrix multiplication do all the sums at once. In , the superscript only says which layer, is what the previous layer put out (for the first layer, the input ), holds every weight, is the list of biases, and is applied to each entry on its own. It's the single neuron from The Artificial Neuron, written for a whole column of neurons at once.
Rows are neurons. Take a layer of neurons reading inputs. Neuron 1 computes , and neuron 2 does the same with its own weights. Now stack the two weight vectors as rows:
A matrix times a vector is exactly "dot each row with the vector" (matrix operations), so each row produces one neuron's weighted sum. The entry is the weight from input into neuron : the row says where the signal is going, the column says where it came from.
Shapes do the bookkeeping. is : rows for the neurons in this layer, columns for the neurons in the layer before. The product gives , one number per neuron, and it only works because the inner sizes match. When code complains about a shape mismatch, the culprit is almost always a matrix written the wrong way around.
Counting parameters. Each layer has weights plus biases. A classic handwritten-digit network goes (one input per pixel of a image, ten outputs for the digits). The first layer has parameters and the second , so in all. Notice that almost all of them sit in the first layer: the count grows like the product of neighboring widths, so wide layers next to each other are where the memory goes.
A whole batch at once. Put inputs side by side as the columns of a matrix of shape . Then is , and column is the layer's response to example ; the bias gets added to every column. Same formula, one matrix-matrix product instead of separate ones. Graphics chips are built to do enormous matrix products fast, and that's the whole reason networks train on them.
Each layer is a linear map, then a bend. The map is a linear transformation: it stretches, rotates and projects the input space. The bias shifts it, and bends each coordinate. A deep network alternates the two, over and over. There's an animation further down showing a tiny 2-2-1 network doing exactly this as one input slides.
A common mistake. It's tempting to write as , "inputs by outputs", because data flows from inputs to outputs. Then doesn't even multiply. In this course's convention, which most textbooks share, the rows belong to the layer's own neurons. (Some software stores the transpose and computes instead; same numbers, flipped.) The other classic slip is forgetting the biases when counting parameters: there's one per neuron, in every layer.
See it
Worked examples
A network has two inputs, two ReLU hidden neurons and one linear output, with , , and . Find the output for .
- 1
Check the shapes first. is (two hidden neurons, two inputs) and is , so is : one number per hidden neuron. is , so the output is a single number.
A network has inputs, a hidden layer of neurons and outputs. Give the shape of each weight matrix and count the parameters.
- 1
Layer 1 reads inputs with neurons. gets one row per neuron and one column per input, so it's .
Practice
Type your answer and check it. Solve them all to complete the lesson.
A layer of neurons reads inputs. What is the shape of its weight matrix?
How many parameters (weights and biases) does a fully connected network have?
A hidden layer has , and ReLU activations; the output neuron is linear with weights and bias . Find the output for .
A batch of examples with features each is stored as the columns of . A layer has a weight matrix. What is the shape of ?
A vibration sensor on a motor feeds features into a network with hidden layers of and neurons and outputs (fully connected). It runs on a microcontroller that stores each parameter as a -byte number. How many bytes do the parameters take?
A network takes inputs, has one hidden layer of neurons and a single output neuron, and has parameters in all. Find .
A layer computes with . What is the partial derivative ?
The hardest one here. Get it, and you've got this lesson.
A network has ReLU hidden neurons with , , and a sigmoid output with weights and bias . Find the output for , to 4 decimal places.
Where it's used
Parameter counts decide what hardware a model needs. Every weight has to be stored and read for each prediction, so a model with billion parameters at bytes each needs GB of memory before it computes anything, while the small network on a fitness tracker fits in a few kilobytes. And because a forward pass is a chain of matrix products, the same counting tells you the work: about one multiply-add per weight per example, which is how engineers estimate whether a model can answer within a phone's time budget.