Layers and the Forward Pass
Why one neuron isn't enough, how stacking neurons into layers builds real capacity, and what a forward pass is.
Three honest attempts, and the ceiling all three hit
Here is the pattern. Two switches, and the answer is yes when exactly one of them is on.
Now three genuine attempts with one neuron, which answers yes when its total is above zero.
Three of four is the ceiling. Not "three is the best these three attempts managed". Three is the ceiling for every one of the infinitely many settings of those three numbers, and this page proves that before it fixes it.
Two inputs on the left, two neurons in the middle, one answer on the right, and every one of the nine numbers in between is a slider. Line thickness is weight magnitude, blue is positive, orange is negative, and that convention holds on every diagram on this site.
Drag the two inputs first and watch the four numbers in the readout move together. Then drag a weight and watch which of them move and which do not.
0:13What this animation shows
A four-layer network (input, two hidden layers, output) appears fully wired, then a wave of activation lights up each layer in turn, left to right. Every edge brightens just before the next layer switches on, showing the signal traveling forward through the whole stack in one pass. This is what "the forward pass" means: one sweep of computation from input to output.
Two inputs, two hidden neurons, one output. Line thickness is weight magnitude, blue is positive, orange is negative.
9 parameters: 6 weights + 3 biases across 2 computing layers. The input layer computes nothing and holds none of them.
Set the hidden activation to Linear, so the middle layer just passes its total straight through. Now: is there a single neuron, with two weights and one bias, that gives the same answer as this whole two-layer network for every possible input?
Commit to a guess, then open this
Yes, and here it is: W1 = 2.600000, W2 = 2.000000, B = -0.740000. Check it on the current inputs:
Which is exactly what the two-layer network reads out.
Not "close". Identical, and identical for every input, not just this one, because a weighted sum of weighted sums is another weighted sum. Stack a thousand linear layers and the same folding collapses all of them into one neuron with one straight boundary. You would have a thousand times the parameters and none of the extra power.
Switch the hidden activation back to Tanh and no such single neuron exists any more. That is the entire job of step 3, and rung 3 does the fold line by line.
A layer is several disagreeing specialists reading identical evidence. Each neuron in it sees the same numbers, applies its own weights, and reaches its own conclusion; they never talk to each other, and the whole value of having more than one is that they conclude different things. The next layer never sees the original evidence at all - it reads only those conclusions, and combines them. That combination is what a single neuron cannot do, and it is why depth buys shapes that width alone does not.
The folded paper
One straight cut through a flat sheet gives you two pieces, and no amount of skill with the scissors changes that. Fold the sheet once first and the same single straight cut gives you four pieces, with edges the flat sheet could not have produced. The hidden layer is the fold. The activation function is what makes folding possible at all, and if you take it out you are back to cutting a flat sheet, which is exactly what the Socratic question above just showed you.
Where it breaks downa fold is one crease in one place. A hidden layer bends the input space everywhere at once, by a different amount at every point.
WordsWhat a layer isWidth, depth, fully connected, and the nine parameters of this network counted by hand.Rung 01
A single neuron can only draw one straight dividing line through its inputs - it can decide "more of clue A, less of clue B" but it can't represent an "either/or" kind of pattern (bring an umbrella if it's cold orif it's windy, but not if it's both, say). This particular kind of pattern, true when exactly one of two things holds, false otherwise, has a name: XOR("exclusive or"), and it's the textbook example of something one neuron provably cannot learn, no matter how its weights are set. Real problems are rarely that simple. The fix is the same one your brain uses: don't rely on one neuron, chain many of them together.
A layer is a row of neurons that all read the same set of numbers and all produce their own answer. That is the whole definition. The neurons in a layer do not talk to each other; they work in parallel on identical input and disagree, which is the point.
Three pieces of vocabulary that get used constantly from here on:
Width is how many neurons a layer has. Depthis how many layers the network has. A network described as "2 inputs, 2 hidden, 1 output" is two wide and one hidden layer deep.
Fully connected (also called dense) means every neuron in a layer reads every number from the previous layer. It is the default arrangement, and the only one this module uses. Convolutional layers, later in the curriculum, deliberately break it.
Counting parameters is worth doing once by hand, because it is how you size a model. Every connection is one weight, and every neuron has one bias. For a layer with n inputs and m neurons, that is n * m weights plus m biases. For the small network used throughout this page: the hidden layer has 2 inputs and 2 neurons, so 4 weights and 2 biases; the output layer has 2 inputs and 1 neuron, so 2 weights and 1 bias. Nine parameters total. Every one of those nine is a number that training is allowed to change, and nothing else about the network changes at all.
NumbersThe proof, and the network that beats itFour inequalities that contradict each other, then all four XOR corners traced through a two-layer network.Rung 02
That claim ("provably cannot", not just "hasn't managed to") deserves the proof, because it is four lines of arithmetic and it is the reason deep learning exists.
Take one neuron with two inputs, and say it answers yes when z > 0. XOR demands these four answers:
The last two lines contradict each other, so no values of w1, w2 and bcan satisfy all four demands at once. Not "no values anyone has found". No values exist. This is the geometry from the previous page cashed out: one neuron draws one straight line, and no straight line puts (0,1) and (1,0) on one side while putting (0,0) and (1,1) on the other. Draw the four points on paper and try it.
And here's the two-layer network that does solve it
The fix takes exactly one hidden layer of two neurons, with ReLU as the activation. ReLU is max(0, z), meaning "keep the number if it is positive, otherwise give zero". The next page covers it properly; for now that one line is all you need.
Four for four. Now look at what the two hidden neurons actually do, reading their values straight out of the block above rather than guessing.
h1 counts how many inputs are on. It outputs 0, then 1, then 1, then 2. It is not a yes/no answer at all; it is a tally.
h2 only wakes up when both inputs are on. It outputs 0, 0, 0, then 1. Its bias of -1 is what holds it shut until the tally reaches 2.
The output neuron then computes h1 - 2 * h2: take the tally, and if h2 reports that both are on, subtract twice as much as h1 added. Follow the last row: h1 contributes 2, h2triggers a subtraction of 2, and the total is dragged back to 0. So the output reads as "how many inputs are on, minus a correction that switches on precisely when there are too many". That is "one but not both", which is XOR.
It is tempting to call h1 an OR gate and h2 an AND gate, and that is nearly right but not right: a real OR gate would output 1 at input (1,1), and h1 outputs 2. The correction term is doing the work that the extra 1 creates. Getting this exactly right matters, because the whole point of the block above is that you can check it.
Neither hidden neuron is doing anything a single neuron couldn't do. A tally and a both-are-on detector are each a single straight line and always were. What the second layer buys is the ability to combine two straight-line answers into something that is not a straight line, and that combining is the entire reason depth works.
These particular nine numbers were chosen by hand so the arithmetic comes out in whole numbers. A trained network would find different ones, almost certainly uglier ones, that also work. This exact solution is the one in Goodfellow, Bengio and Courville's Deep Learning, section 6.1.
PictureTwo layers, and the one neuron they fold intoThe house network run with no activation at all, then the single neuron that reproduces it exactly.Rung 03
The previous page said the activation function is there to bend the neuron's response. Here is that claim as arithmetic, using the small rain network that the rest of this module reuses.
Now run it with no activation at all, meaning every neuron just passes its pre-activation straight through.
Two layers, three neurons' worth of arithmetic, nine parameters, answer 2.540000. (Three, not five: the two inputs are just numbers sitting there, and an input layer computes nothing.) Now here is a single neuron that gives the identical answer:
Identical, to the last digit, and not by coincidence for this input. That single neuron produces the same answer as the whole two-layer network for every possible input, because a weighted sum of weighted sums is just another weighted sum. Stack a thousand layers this way and the same folding collapses all of them into one neuron with one straight boundary. You would have a thousand times the parameters and exactly none of the extra power.
The activation function is the one thing that stops the folding. Take tanh, which is the curve this module uses in its hidden layer from here on: it is an S-shaped squashing curve like the sigmoid from the previous page, but it runs from -1 to 1 instead of 0 to 1, so a neuron that concludes "no" is allowed to say so with a negative number. Once a1 = tanh(z1) sits between the two layers, there is no set of weights W1, W2, B that reproduces the network, because tanh is not a weighted sum of anything. That, and not tidiness of range, is why step 3 exists.
The instrument draws this rung for you. Set its hidden activation to Linear and a panel appears under the readout printing W1, W2 and B live from whatever the nine sliders currently hold, plus the check line. Switch back to Tanh and the panel is replaced by one sentence saying no such neuron exists. The fold appearing and disappearing as you flip one dropdown is the picture.
EquationThe full forward pass, input to answerEvery multiplication from the two inputs to the 86.4% answer, tanh expanded in full.Rung 04
Now the real thing, end to end, with tanh in the hidden layer and a sigmoid on the output because the question is yes-or-no. Recall from a moment ago what tanh is: the same S-shape as sigmoid, running from -1 to 1 rather than 0 to 1, so it can return a negative number. That is the only thing you need to know to follow the block; page 4 covers why it is the better choice inside a hidden layer. Watch a2 come out negative below and you will see the range doing its job.
The answer is not the 80.2% from the previous page, and it should not be. Same weather, same two clues, same first neuron, different number. Page 2 was one neuron deciding on its own. This is two neurons disagreeing and a third weighing them up, through a different curve, which is a different and better machine. A bigger network giving a different answer from a smaller one is the normal case, not a mistake, and it is the whole reason to build the bigger one. Watch for this again on the next two pages, where changing only the hidden activation moves the same answer between 73.1% and 94.1%.
Three more things worth noticing, because they are the things that generalise.
The two hidden neurons disagree, and that is the point. h1 came out strongly positive at +1.40, h2 strongly negative at -0.70. They read the identical two inputs and reached opposite conclusions, because they hold different weights. A layer is useful precisely to the extent its neurons disagree; if they all held the same weights they would all produce the same number and the layer would be one neuron wearing a costume. That failure has a name, symmetry, and the weight-initialization page is about avoiding it.
The negative weight does real work. h2 ends up at -0.604368, and the output neuron reads it through v2 = -0.80, drawn orange. Negative times negative gives +0.483494, a positive contribution. h2is "evidence against rain" being read by a connection that treats evidence-against as evidence-for. Follow the two signs and it makes sense; guess at it and it won't.
The activation is applied one number at a time. tanh did not look at z1 and z2 together. It ran on each independently. Every activation in this module works that way, with exactly one exception: softmax, on the last page, which deliberately couples all the outputs together. Everything else is element by element.
General caseThe same pass in one line, and how far this goesz = W x + b, the index-order trap it hides, and the honest reading of the universal approximation theorem.Rung 05
Writing out every multiplication is how you learn it and not how anyone codes it. Here is the identical calculation in the form you will actually meet.
Stack the hidden layer's weights into a grid with one row per neuron:
One trap, worth catching now rather than in someone else's code. The entry in row 1, column 2 of that grid is +2.00, which is the weight this module has been calling w21. So the grid puts the neuron first and the input second, which is the exact reverse of the w<input><hidden>subscript order you have been reading all page. Nothing is wrong with either; both orders are standard, and which one a piece of code uses is a convention its author picked. Read the grid as "one row per neuron" and the subscripts as "input, then neuron", and check which one you are looking at before trusting an index.
Then the entire hidden layer is z = W x + b, where multiplying a grid by a column of inputs means "take each row, dot it with the input, and write the result on its own row":
Same two numbers as above, same arithmetic, one symbol. Then a = tanh(z) applied to every entry, and the output layer is zo = V a + bo with V = [ +1.20 -0.80 ].
Two full layers of a neural network:
That is the same arithmetic you just did by hand, with nothing hidden and nothing added. Matrix notation is a shorter way to write many multiplications, not a different algorithm. The backpropagation page makes the same point about the backward pass.
How far can this go?
There's a real theorem here, not just an intuition: a network with even a single hidden layer, given enough neurons in it, can approximate any continuous function on a bounded input range to any accuracy you like - this is called the universal approximation theorem. It's worth reading carefully, though, because it's easy to overclaim: it says such a network exists, not that gradient descent will actually find it, and not that "enough neurons" is a small or practical number. In practice, going deeper (more layers) tends to represent complex functions far more efficiently than going wider with one giant layer, which is the whole reason the rest of this module, and deep learning generally, is about depth rather than a single very wide layer.
One practical footnote to that theorem. It says a wide-enough single hidden layer suffices, and it is silent about howwide. For many functions the required width grows exponentially with the input dimension, while a deeper network can express the same function with a number of neurons that grows far more gently. That gap is why "just make one layer bigger" is not the answer in practice, and why the field is called deep learning rather than wide learning.
Why this and not that
Why not one enormous single layer?
The theorem says a wide-enough single hidden layer can approximate any continuous function, and it says nothing about how wide. For many functions the width needed grows exponentially with the number of inputs while depth handles the same function with far fewer neurons. Rung 5 states it; the practical version is that "just make the layer bigger" runs out of memory long before it runs out of error.
Why do neurons in a layer not connect to each other?
In this module, they don't, by definition: a layer reads the previous layer and nothing else. Networks where a neuron reads its own layer's output, or its own output from the previous timestep, exist and are called recurrent, and they are a different module.
Why is the input layer not counted?
It computes nothing. It is two numbers sitting there. This matters for one reason beyond pedantry: it is why the network above does "three neurons' worth of arithmetic" and not five, and why a "3-layer network" in a paper usually means three layers that compute.
Why did the answer change from 80.2% to 86.4%?
Because it is a different and better machine. A bigger network giving a different answer from a smaller one is the normal case, not a mistake, and it is the entire reason to build the bigger one.
Why does the XOR solution use those exact nine numbers?
Because they were chosen by hand so the arithmetic comes out in whole numbers. A trained network would find different ones, almost certainly uglier, that also work. There is nothing canonical about them beyond their appearance in Goodfellow section 6.1.
If the fold in rung 3 works for linear layers, why can't I fold the tanh one too?
Because folding requires that the composition of two weighted sums is a weighted sum, which is true, and that the composition of a weighted sum with tanh is a weighted sum, which is not. Tanh is not a weighted sum of anything, so there is nothing to fold it into.
- XOR
- "exclusive or", from logic. Plain "or" allows both; the exclusive version does not.
- hidden layer
- nobody outside the network reads those values. Not secret, just unlooked-at.
- fully connected
- also "dense": every neuron reads every number from the previous layer, so the weight grid has no empty cells. "Dense" is the same claim from the storage side.
- forward pass
- named for the direction data moves. The backward pass, which moves corrections the other way, gets its own page, and the two names only make sense as a pair.
- width, depth
- width is across a layer, depth is along the stack. "Deep learning" is a claim about the second number and nothing else.
- perceptron
- Rosenblatt, 1958, for the single-neuron machine of the previous page. In 1969 Minsky and Papert published the XOR limitation this page's break reenacts, and the field's funding collapsed for over a decade on the strength of it. The limitation was real; the conclusion drawn from it, that layering could not fix it, was not.
- tensor
- in this subject it just means "an array with any number of dimensions". A number is a 0-tensor, a list is a 1-tensor, a grid is a 2-tensor. The physics meaning is stricter and is not what the frameworks mean.
- What is happening
- With a linear activation, every layer folds into the one before it. Depth without a bend is arithmetic you are paying for and not using.
- Fix
- Put a non-linear activation between every pair of layers. Any of the curves on the next two pages will do; leaving it out is the only choice that is definitely wrong.
- Watch for
- The hidden activation reads Linear,
z_oreads2.540000, and the collapse panel prints the single neuronW1 = 2.600000,W2 = 2.000000,B = -0.740000that reproduces it exactly.
- What is happening
- Neurons that hold the same weights read the same inputs and produce the same number, receive the same correction, and stay identical for the life of the network. The failure has a name, symmetry, and the weight-initialization page is about avoiding it.
- Fix
- Initialize the weights randomly rather than uniformly. That is the whole fix, and it is why nobody starts a network at zero.
- Watch for
- Both hidden circles read
0.885352,z_o = 0.654141andp = 0.657943. Draggingw₁₁breaks the tie immediately - you would have to dragw₁₂to match to stay in this state.
- What is happening
- The grid indexes (neuron, input) and the
w21subscripts index (input, neuron). Both conventions are standard. Swap two of them and every number downstream changes. - Fix
- Check the convention before you trust an index: print one weight you can identify by eye and see which cell it lands in.
- Watch for
w₂₁andw₁₂swapped:z1 = -0.400000,z2 = 1.700000,z_o = -0.904266,p = 0.288175. The same weather, the same nine numbers, arranged two cells differently, and the forecast went from 86.4% to 28.8%.
- What is happening
h2concluded "no" with -0.604368, and the output reads it throughv2 = -0.80, drawn orange. Negative times negative is a positivecontribution of +0.483494. Draw that connection blue instead and the same "no" now subtracts.- Fix
- Follow both signs on a connection before deciding what it does. One negative is evidence-against; two negatives is evidence-for.
- Watch for
v₂is+0.80, the edge is blue, the second contribution reads-0.483494instead of+0.483494,z_o = 0.878928andp = 0.706600.
- What is happening
- The input layer computes nothing and holds no parameters. This network is two computing layers and nine parameters: 4 weights and 2 biases in the hidden layer, 2 weights and 1 bias in the output layer.
- Fix
- Count computing layers, not circles on the diagram, and count parameters as connections plus neurons.
- Watch for
- The parameter panel lights up and reads
9 parameters: 6 weights + 3 biasesacross 2 computing layers, and the two input circles are drawn without any incoming edges at all, because there are none.
- 01Compute z1 and z2 on paper from the nine parameters and the two inputs, then load the network and check.
Hint
Each is two multiplications and two additions, and the bias is one of the additions.Answer
z1 = 1.50 * 0.80 + 2.00 * 0.60 + (-1.00) = 1.200000 + 1.200000 - 1.000000 = 1.400000z2 = (-1.00) * 0.80 + 0.50 * 0.60 + (-0.20) = -0.800000 + 0.300000 - 0.200000 = -0.700000 - 02Set both inputs to 0.00 and predict both hidden pre-activations before you look.
Hint
Every contribution from an input is that input times something.Answer
z1 = -1.000000andz2 = -0.200000, which are the two biases alone. With no evidence at all a neuron outputs its standing lean and nothing else, which is exactly what a bias is for. - 03Find an input where the two hidden neurons agree in sign, and say why they normally do not.
Hint
h2's only blue weight is on the second input.Answer
Inputs (0.00, 1.00) give
z1 = 1.000000andz2 = 0.300000, both positive. They usually disagree becauseh2reads the first input through an orange -1.00 whileh1reads it through a blue +1.50, so a grey sky pushes them in opposite directions. Turning the sky off removes the thing they disagree about. - 04Set the hidden activation to Linear and find, by hand, the single neuron that reproduces the whole network. Then check it against the collapse panel.
Hint
Push each output weight back through the hidden weights it multiplies.Answer
W1 = v1 * w11 + v2 * w12 = 1.20 * 1.50 + (-0.80) * (-1.00) = 1.80 + 0.80 = 2.600000W2 = v1 * w21 + v2 * w22 = 1.20 * 2.00 + (-0.80) * ( 0.50) = 2.40 - 0.40 = 2.000000B = v1 * b1 + v2 * b2 + bo = -1.20 + 0.16 + 0.30 = -0.740000check: 2.60 * 0.80 + 2.00 * 0.60 - 0.74 = 2.540000 - 05Hardest. Build the XOR network on the widget and verify all four corners.
Hint
Hidden activation ReLU, output activation Linear, and every weight is +1, -1 or -2.Answer
w11 = 1, w21 = 1, b1 = 0;w12 = 1, w22 = 1, b2 = -1;v1 = 1, v2 = -2, bo = 0. Then (0,0) gives 0, (0,1) gives 1, (1,0) gives 1, (1,1) gives 0, which is rung 2's block, reproduced on the instrument. Watchh1read 0, 1, 1, 2 across the four corners: it is counting, not deciding.
- Your search boxEvery layer of the model that just autocompleted it is this exact block: one weight grid, one matrix multiply, one bias vector, one activation applied element by element. A model described as having ninety-six layers is that block, ninety-six times.
- Why a GPUW x is thousands of independent multiply-and-adds with no ordering between them, and a GPU is a machine for doing thousands of independent multiply-and-adds at once.
One neuron draws a line. A layer draws several. Depth lets you cut along a fold.
Straighten the paper, which is what dropping the activation does, and every fold in the stack flattens back out into a single sheet with a single straight cut in it, however many times you folded it.
The forward pass above ran tanh in the hidden layer and never said why. Swap tanh for sigmoid and the same weather comes out at 73.1%. Swap it for PReLU and the same weather comes out at 89.3%. Sixteen percentage points, on identical evidence, with not one of the nine weights touched.
So the curve is not a detail. The next page is what is inside it, and the answer turns out to have almost nothing to do with the curve's shape and almost everything to do with one number attached to every point on it.