Layers and the Forward Pass
Follow numbers through a small network by hand, see a layer as one matrix multiplication, and count the parameters of a real digit classifier.
Welcome back!
Step 1 of 13Learn
Data flowing left to right
A typical first network for handwritten digits has three layers, written 784-128-10. The 784 input values are the pixels. The 128 hidden neurons each look at all 784 pixels and compute a feature. The 10 output neurons each look at all 128 features and produce a score for one digit, which softmax turns into probabilities.
Computing an output from an input is called the forward pass: values flow from the input layer, through each hidden layer, to the output. Every connection carries a value multiplied by its weight; every neuron adds up what arrives, adds its bias and applies its activation function.
A few terms you'll see everywhere:
- Dense or fully connected layer: every neuron receives every output of the previous layer.
- Width: the number of neurons in a layer (128 above).
- Depth: the number of layers with weights. A network with many hidden layers is called deep, which is where "deep learning" gets its name.
The input layer is a bit of a misnomer: it holds the data but does no computation and has no weights. When people count layers, they usually count only the layers that have weights.
Step 2 of 13Practice · Choose one
A network is described as 784-256-128-10. How many hidden layers does it have?
Step 3 of 13Learn
A forward pass by hand
Let's run a tiny network with 2 inputs, 2 hidden ReLU neurons and 1 linear output (no activation on the output, as in a regression model).
- Hidden neuron h1: weights (0.5, −1), bias 1
- Hidden neuron h2: weights (1, 1), bias 0
- Output: weights (2, 1) applied to (h1, h2), bias −1
Input x = (1, 2):
- h1: z = 0.5·1 + (−1)·2 + 1 = −0.5, and ReLU(−0.5) = 0.
- h2: z = 1·1 + 1·2 + 0 = 3, and ReLU(3) = 3.
- Output: 2·0 + 1·3 − 1 = 2.
Notice that h1 was switched off by ReLU for this input, so its weight in the output layer had no effect. Different inputs activate different combinations of hidden neurons, which is how a ReLU network builds a piecewise function out of simple pieces.
In matrix form, the hidden layer computes h = ReLU(Wx + b), where W has one row per hidden neuron and one column per input: a 2 × 2 matrix here, a 128 × 784 matrix in the digit network. Frameworks process many inputs at once by stacking them into a matrix, so a whole batch of images goes through a layer in one multiplication.
Step 4 of 13Practice · Work it out
Using the same tiny network (h1: weights 0.5 and −1, bias 1; h2: weights 1 and 1, bias 0; output: weights 2 and 1, bias −1; ReLU in the hidden layer), what does it output for the input x = (2, 1)?
Step 5 of 13Learn
Counting parameters
How big is a network? The usual measure is its number of parameters: all the weights and biases that training adjusts. For a dense layer:
parameters = inputs × outputs + outputs
The first term counts weights (one per connection), the second counts biases (one per neuron).
For the 784-128-10 digit network:
- Hidden layer: 784 × 128 + 128 = 100,352 + 128 = 100,480
- Output layer: 128 × 10 + 10 = 1,280 + 10 = 1,290
- Total: 101,770 parameters
Notice how lopsided that is: almost all the parameters sit in the first layer, because 784 inputs connect to every hidden neuron. This is typical for dense networks on raw data and is one reason images are usually handled by convolutional networks instead, which you'll meet in the last module.
Parameter counts matter for three practical reasons. Each parameter takes memory (4 bytes as a 32-bit float, so 101,770 parameters is about 0.4 MB). More parameters mean more computation per prediction. And a model with many parameters relative to its training examples can memorize the data instead of learning general patterns. Modern language models have billions of parameters and need correspondingly huge datasets.
Step 6 of 13Practice · Chart
Where the parameters live in a 784-128-10 network
Tap the layer that holds almost all of the parameters.
Tap the bar that answers the question, then press Check.
Step 7 of 13Try the calculator
Parameter counter for a network with two hidden layers
Each dense layer has inputs × outputs weights plus one bias per neuron.
- Layer 1 parameters
- 100,480
- Layer 2 parameters
- 8,256
- Output layer parameters
- 650
- Total parameters
- 109,386
- Memory at 4 bytes each
- 0.44 MB
Step 8 of 13Practice · Work it out
How many parameters (weights plus biases) does a dense 784-256-128-10 network have?
Step 9 of 13Note
Step 10 of 13Learn
Why deep rather than just wide?
A famous result, the universal approximation theorem, says a network with just one hidden layer can approximate any continuous function as closely as you like, given enough hidden neurons. So why bother with depth?
Because "enough neurons" can mean an absurd number. Depth lets a network reuse what earlier layers computed. In an image network trained on photos, researchers who visualize the layers typically find that the first layer responds to edges and color blobs, the next to corners and textures, later ones to parts like eyes or wheels, and the last layers to whole objects. Each layer builds on the features of the one before, so complex concepts are assembled from simpler parts instead of being learned from scratch in one giant layer.
Depth has costs, too. Very deep networks used to be hard to train because the learning signal faded as it traveled back through many layers. Techniques such as ReLU, careful initialization and residual connections (shortcuts that let a layer's input skip ahead and be added to its output) solved much of this, which is why networks with dozens or even hundreds of layers are now common.
Step 11 of 13Compare
Wide and shallow vs deep and narrow
| Aspect | Wide, one hidden layer | Deep, several hidden layers |
|---|---|---|
| Can approximate any function? | Yes, in theory, with enough neurons | Yes |
| Neurons needed for complex patterns | Can grow enormously | Far fewer, because features are reused |
| How features are built | All at once, from raw inputs | In stages: edges, then shapes, then objects |
| Training difficulty | Easier to optimize | Needs tricks (ReLU, good initialization, normalization) |
Step 12 of 13Practice · Put in order
Put the computations of one dense layer in the order they happen during a forward pass.
Drag the items, or use the arrow buttons, to put them in order.
- Apply the activation function to each value
- Multiply it by the layer's weight matrix
- Add the bias vector
- Pass the result to the next layer
- Receive the output vector of the previous layer
Step 13 of 13Practice · Choose one
Which statement about the input layer of a dense network is correct?
Lesson complete!