Key takeaways
- A neuron does three things: take a weighted sum, add a bias, and pass the result through an activation function: y=f(w⊤x+b).
- A whole layer of neurons is one matrix multiplication, h=f(Wx+b); a whole batch of examples at once is H=f(XW+b).
- Stack layers and you get a multilayer perceptron (MLP). There has to be a nonlinearity in between, or any number of layers collapses into one. With nonlinearities, enough neurons can approximate any continuous function.
- Hidden layers usually use ReLU, GELU or SiLU. Sigmoid and tanh are prone to vanishing gradients. When the output should be a probability, use sigmoid (two options) or softmax (many options).
- The parameters are all the weights and biases: nin×nout+nout per layer. Anything shared by the whole batch gets its gradient summed over the batch.
What's inside the model?
Last episode, we said a language model is one giant function: a list of numbers goes in, and out comes a score for every word in the vocabulary. That's true of any neural network: at heart, it's a function that turns a pile of numbers into another pile of numbers.
Open the box and you find layer after layer of tiny parts, plus an enormous number of adjustable numbers called parameters. Even the smallest Llama 3 model has about 8 billion of them.
Pull out one of those parts. It's called a neuron, and what it does is almost unbelievably simple: a few multiplications, one addition, and then a little bend.
So here's the question: if the parts are this simple, how can piling them together do something so complex? This episode starts from a single neuron, builds up a multilayer perceptron piece by piece, and then comes back to answer it.
One neuron: weighing the evidence
Let's meet the neuron through one small task: deciding whether a movie review is positive or negative.
A neuron only understands numbers, so first we turn words into numbers. The simplest way: count how many times a few key words show up. Say "great" appears twice in a review, "boring" once, and "recommend" once. Then 2, 1, 1 are the neuron's inputs.
The neuron gives each input a weight. A weight says how much that input matters, and which way it pushes the verdict. "Great" gets 3 (strong evidence for positive), "boring" gets −2 (pushes toward negative), and "recommend" gets 1.
| Word |
Count xi |
Weight wi |
Product |
| great |
2 |
3 |
6 |
| boring |
1 |
−2 |
−2 |
| recommend |
1 |
1 |
1 |
The computation takes four steps:
- Multiply: each input times its weight gives 6, −2 and 1.
- Add: 6−2+1=5.
- Add the bias: the bias here is b=−1, so we get 5+(−1)=4. The bias works like a threshold: it sets how easily this neuron switches on.
- Activate: hand the result to an activation function. We'll start with the simplest one, ReLU: negative numbers become 0, positive numbers pass straight through. Four is positive, so it comes out unchanged.
The neuron is activated. It's telling us: this looks like a positive review.
Now try a bad review: "Long and boring. So, so boring." This time "boring" shows up twice and the other two words not at all, so the inputs are 0, 2, 0. Multiply by the weights and add up: −4. Add the bias: −5. ReLU turns negatives into 0, so the neuron stays silent.
Intuition: a weight wi says "how much this input matters, and which way it pushes"; the bias b says "how easily this neuron switches on".
From example to formula
Call the three inputs x1,x2,x3 and their weights w1,w2,w3. Take the weighted sum and add the bias, and call the result z:
z=w1x1+w2x2+w3x3+b
Plug in the numbers: 3×2+(−2)×1+1×1+(−1)=4, exactly what we got before.
There don't have to be just three inputs. With n of them, fold the sum up with a summation sign, then wrap it in the activation function f to get the neuron's output y:
y=f(i=1∑nwixi+b)
Here x∈Rn is the input vector (the activations coming from the previous layer), w∈Rn is the weight vector (the weights on the edges leading into the neuron), b is the bias, and f is the activation function.
There's an even more compact way to write it. Stack the weights in a column, the vector w, and the inputs in a column, the vector x. "Multiply pair by pair, then add everything up" is called the dot product: 3×2+(−2)×1+1×1=5.
In a formula, we transpose w: tip the column over on its side so it becomes a row, written w⊤. A row times a column means exactly that: multiply pair by pair, then add. So a single neuron is:
y=f(w⊤x+b)
Every neural network grows out of this one line.
Watch out: the name "neuron" comes from the nerve cells in your brain, which collect signals, add them up, and fire if the total is strong enough. But that's only a rough analogy. Underneath, it's just a simple function.
What one neuron looks like: a hinge
To be able to draw it, give the neuron a single input x. Then it's y=ReLU(wx+b).
- With w=1, b=0 it's just ReLU itself: flat at 0 on the left, a straight ramp on the right, with the kink at the origin.
- Turn w up and the ramp gets steeper; make w negative and the ramp flips over to the left.
- Change b and the kink slides left and right. The kink sits where wx+b=0, that is, at x=−b/w.
Intuition: draw a ReLU neuron and you get a hinge, a straight line with one bend. w sets the slope and direction; b sets where it bends. Remember this hinge: it's going to come in very handy.
A layer: many neurons at once
One neuron can only judge one thing. To judge several things at once, let several neurons look at the same inputs. That's a layer.
The first neuron is our old "positive detector". Add a "negative detector" with weights −2, 3, −1 and bias 1. Stack the two neurons' weights on top of each other and you get a table with 2 rows and 3 columns, the weight matrix W. Each row holds all the weights of one neuron. The biases form a vector b, and the inputs a vector x:
W=(3−2−231−1),x=211,b=(−11)
The rule for a matrix times a vector: take each row of W, dot it with x, and you get one number of the output.
- First row: 3×2+(−2)×1+1×1=5.
- Second row: (−2)×2+3×1+(−1)×1=−2.
- Add the biases, each neuron its own: 5−1=4 and −2+1=−1.
- Apply ReLU to each number separately: 4 stays, −1 becomes 0.
The layer's output is (4,0). As a formula:
h=f(Wx+b)
However many neurons the layer has, it's just one matrix multiplication plus one vector. The shapes: W∈Rnout×nin, with one row per neuron (the output dimension) and one column per input (the input dimension); b∈Rnout; and the output h∈Rnout.
Why bother writing it as a matrix? Because GPUs are incredibly fast at matrix multiplication. Even a layer with tens of thousands of neurons is still just one matrix multiplication.
A whole batch at once
In real training we don't process examples one at a time; we feed in a whole batch together. Let's put our positive and negative reviews together.
The convention is to make each example a row, stacked into a matrix X∈Rm×nin: m examples, m rows. To match, W gets transposed to W∈Rnin×nout. Before, each row was one neuron; now each column is one neuron.
X=(201210),W=3−21−23−1
The whole layer becomes XW: each row of X dotted with each column of W.
XW=(5−4−26)
The first row (the positive review) is 5 and −2, exactly as before; the second row (the negative review) is −4 and 6.
The bias is a single row, (−1, 1), but it has to be added to every example. So we copy it down to every row and then add. This is called broadcasting. That gives (4−5−17), and ReLU turns it into (4007): the positive review activates the first neuron, and the negative review strongly activates the second one (7).
So for a batch, a layer is:
H=f(XW+b)
with shapes (m×nin)(nin×nout)→m×nout, and b broadcast to m×nout.
Math notation vs PyTorch
Here's something you'll see when you read PyTorch code. Its linear layer, nn.Linear, stores the weights as nout×nin, one neuron per row, and the forward pass computes X @ W.T.
layer = nn.Linear(3, 2) # 3 inputs, 2 outputs
layer.weight.shape # torch.Size([2, 3])
H = X @ layer.weight.T + layer.bias
Watch out: that transpose costs almost nothing. Not a single number gets moved; memory is just read in a different order (only the stride changes). PyTorch does it this way so that the gradient for W naturally comes out as nout×nin, the same shape as W itself.
Stacking up: the multilayer perceptron
A layer's output h is itself just a list of numbers, so it can serve as the input to the next layer. Take a 3→4→4→2 network:
h1h2y=f(W1x+b1)=f(W2h1+b2)=W3h2+b3
- On the far left is the input layer, on the far right the output layer. The ones in between are hidden layers: we only ever see the inputs and outputs, never the values in the middle.
- Between neighboring layers, every neuron connects to every neuron in the layer before, so this is called fully connected. A fully connected network with an input layer, at least one hidden layer and an output layer is a multilayer perceptron (MLP).
- It's also often called a feed-forward network (FFN), because information only flows forward and never loops back. Strictly speaking, FFN is the broader category (any network where information flows in one direction), but the two terms are used interchangeably.
- The last layer usually has no activation function: its outputs are scores that can be any number, and when we need probabilities we put a softmax on top.
Inside every layer of a Transformer there's an MLP just like this, alternating with attention, and a model often stacks dozens of these layers. Episode 6 puts the whole picture together.
Why we need nonlinearity
Now to the opening question. A thought experiment first: what happens if we take the activation function away?
Start in one dimension. Layer one: h=2x+1. Layer two: y=3h−4. Plug the first into the second:
y=3(2x+1)−4=6x+3−4=6x−1
Plot it, and it's still a straight line! Two layers stacked together are no different from one.
Matrices work exactly the same way. Without activations, layer one is h=W1x+b1 and layer two is y=W2h+b2. Substitute h and multiply out:
y=W2(W1x+b1)+b2=W2W1x+W2b1+b2=Wx+b
The key is the last step: W2W1 is a matrix times a matrix, which is still just a matrix; call it W. And W2b1+b2 is still just a vector; call it b. Two layers have collapsed back into one!
It doesn't matter how many layers you stack: they always merge into one matrix multiplication plus one bias. Without nonlinearities, extra layers add no representational power at all. The network can only represent linear functions: a straight line in one dimension, a flat plane in two.
Intuition: that little bend in the activation function is what makes stacking layers worthwhile.
Building any curve out of hinges
What happens once we put the activation back? Remember, a single ReLU neuron is a hinge. Say we want the network to draw a wavy curve; with straight lines alone, no number of layers can do it. So we build it from hinges:
- The first neuron gives a sloped line for the uphill stretch at the start.
- A second neuron kicks in at x=2 and bends the slope downward, so the curve heads downhill.
- A third neuron bends it again at x=4, turning the curve back uphill.
Three straight pieces, and it's already starting to look right. Each hinge is one hidden neuron, and adding them up with weights is the output layer's job. So this is exactly an MLP with a single hidden layer:
y=i=1∑kaiReLU(wix+bi)+c
More neurons means more bends and a closer fit: 6, then 12, then 24.
Behind this is a famous result, the universal approximation theorem: give an MLP enough hidden neurons, and on any bounded range it can approximate any continuous function as closely as you like.
Watch out: it only promises that such parameters exist. It doesn't tell us how to find them, or how many neurons we'll need; finding them is the job of training. And in practice, people have found that stacking many layers usually works better, with fewer parameters, than making one layer extremely wide. The "deep" in deep learning simply means lots of layers.
Common activation functions
Sigmoid: squashing a score into a probability
σ(x)=1+e−x1∈(0,1)
No matter how big or small the input, the output is squeezed between 0 and 1, and at x=0 it's exactly 0.5. That makes it a natural way to turn a score into a probability: our review neuron's 4 becomes 0.98.
But it's a poor choice for hidden layers, for two reasons:
- Vanishing gradients. Its derivative is σ(x)(1−σ(x))≤0.25: even at its steepest, right in the middle, it's only 0.25. As we'll see next episode, training multiplies these derivatives together layer after layer. At most 0.25 per layer means that after ten layers you're at 0.2510≈0.00000095, below one millionth. The signal barely makes it back.
- Not zero-centered. Its output is always positive. That makes the gradients for all the weights of one neuron in the next layer either all positive or all negative, so on every update they go up together or down together, and training zigzags its way along.
Tanh: an S-curve centered on zero
tanh(x)=ex+e−xex−e−x=2σ(2x)−1∈(−1,1)
Tanh, the hyperbolic tangent, is an upgraded sigmoid: its output runs from −1 to 1, centered on zero. Its derivative, tanh′(x)=1−tanh2(x)∈(0,1], peaks at 1 right at x=0, a lot better than sigmoid. But away from the middle both ends are just as flat, and since each factor is never more than 1, multiplying them can only shrink the signal. The gradient still vanishes.
ReLU and its relatives
The workhorse of modern hidden layers is ReLU:
ReLU(x)=max(x,0)
Its derivative is 1 for x>0 and 0 for x<0. The positive side never flattens out, so gradients don't vanish easily, and it's lightning fast to compute.
Its weakness is the dead ReLU (or "dying ReLU") problem: if a neuron's input is negative for every single example, its output is always 0 and its derivative is always 0, so it can never learn anything again. A fraction of the network can die like this during training.
One fix is Leaky ReLU, which keeps a small slope α on the negative side, usually 0.01 (the video draws 0.1 so you can see it):
LeakyReLU(x)={xαxx>0x≤0
Later, people found that rounding off ReLU's sharp corner often works even better:
- GELU: GELU(x)=x⋅Φ(x), where Φ is the cumulative distribution function of the standard normal distribution. Think of it as an S-shaped curve rising smoothly from 0 to 1. GPT-2 and BERT both use GELU.
- SiLU, also known as Swish: SiLU(x)=x⋅σ(x). It's the one inside SwiGLU, in models like Llama.
The two look a lot alike: smooth everywhere with no sharp corners, and each has a small dip on the negative side, so they're not monotonic (they don't only ever go up).
The gate view: GLU and SwiGLU
There's one way to see all of these: each one is x times a "gate".
ReLU(x)GELU(x)SiLU(x)=x⋅1x>0=x⋅Φ(x)=x⋅σ(x)
ReLU's gate is hard: when x is positive it's fully open (multiply by 1); when x is negative it's fully shut (multiply by 0). GELU and SiLU have soft gates: Φ and σ both rise smoothly from 0 to 1, so the gate opens gradually.
Now take the idea one step further. Why should x decide its own gate? A GLU (gated linear unit) uses two separate sets of weights: one computes the "content" (the xW1 on the left below), the other computes the "gate" (the σ(xW2) on the right), and the two are multiplied element by element (⊙):
GLU(x)=(xW1)⊙σ(xW2)
Say the content is [2,−1,3] and the gate is [0.9,0.1,0.5]. The result is [1.8,−0.1,1.5]. Where the gate is wide open, most of the content gets through; where it's near 0, the content is almost blocked. The network learns for itself how much to let through at each position.
Swap the sigmoid in the gate for Swish and you get SwiGLU:
SwiGLU(x)=(xW1)⊙Swish(xW2)
Most of today's large language models use it in their FFN. Episode 9 digs into it.
Softmax: turning a list of scores into probabilities
Softmax is a special activation function whose job is to turn a list of scores into a probability distribution (everything adds up to 1):
softmax(z)i=∑jezjezi
Say the output layer gives three scores: 2 for positive, 1 for neutral, 0.1 for negative. Scores can be any number, positive or negative, and they don't add up to 1. Softmax works in two steps:
- Raise e to the power of each score, which makes them all positive: e2=7.39, e1=2.72, e0.1=1.11.
- Divide by their total, 11.21, giving 66%, 24% and 10%, which add up to exactly 1.
Watch out: unlike the other activation functions, softmax doesn't work one number at a time. Every output depends on the whole list, because the denominator is a sum over all of them.
We can also add a temperature T: divide every score by T first, then apply softmax.
softmax(z/T)i=∑jezj/Tezi/T
With T<1 the distribution gets sharper and the probability piles onto the top score; with T>1 it flattens out and the options get closer together. This is exactly the "temperature" you can adjust when a language model generates text. More in episode 11.
How to choose, in a nutshell
- Hidden layers: the ReLU family, or its smoother cousins GELU and SiLU.
- Outputs that should be probabilities: sigmoid for a choice between two options, softmax for a choice among many.
Counting parameters
The parameters are all the numbers that get trained: every weight and every bias. A layer has nin×nout weights (one per cell of W) plus nout biases (one per neuron), so the number of parameters in one layer is
nin×nout+nout
For our little 3→4→4→2 network:
| Layer |
Count |
Params |
| 1 |
3×4+4 |
16 |
| 2 |
4×4+4 |
20 |
| 3 |
4×2+2 |
10 |
| Total |
|
46 |
Real models are much bigger. In the smallest GPT-2, each layer's MLP takes in 768 dimensions, expands them to 3072 (exactly 4×768; the MLP's hidden size is usually 4 times the model width), applies a GELU, and squeezes them back down to 768.
(768×3072+3072)+(3072×768+768)=4,722,432
That's about 4.72 million per layer. GPT-2 small has 12 layers, so the MLPs alone hold about 57 million parameters, nearly half of the whole model's roughly 124 million.
All these numbers start out random. Training is the process of nudging every one of them toward a good value.
A sneak peek: the shape of gradients
During training, every parameter gets a number that tells it which way to move and by how much. That number is its gradient. The gradient of W is written ∂W∂L, read "the partial derivative of L with respect to W", where L is the loss. Its shape is exactly the same as the parameter's.
How to compute it is the subject of episodes 2 and 3, but let's get acquainted. For Z=XW+b:
∂W∂L=X⊤∂Z∂L
The shapes are (nin×m)(m×nout)=nin×nout. The m in the middle, the number of examples, gets summed away, and the result is exactly the size of W. Why does m disappear? Because W is shared by every example in the batch. Each example has its own "opinion" about how W should change, and in the end all those opinions get added together.
∂X∂L=∂Z∂LW⊤
The shapes are (m×nout)(nout×nin)=m×nin. Row i of Z depends only on row i of X (examples in a batch don't interact), so every example gets its own gradient, and they're stacked row by row.
The bias works just like W: the same b is added to every example, so its gradient is every example's gradient, added up along the batch:
∂bj∂L=i=1∑m∂zij∂L
Intuition: a rule of thumb worth keeping for life. First work out the clean result for a single example. Then ask whether the tensor is shared across the batch. Shared (like the weights W or the bias b) → the gradient is summed over the batch. Belongs to one example (like the activations X) → the batch dimension is kept.
Optional: deriving the bias gradient, and the formulas in PyTorch's layout
Written out with the chain rule, the bias gradient looks like this: the partial of zij with respect to bj is 1, each example contributes one term, and they all add up:
∂bj∂L=i=1∑m∂zij∂L⋅∂bj∂zij=i=1∑m∂zij∂L
With PyTorch's layout, Z=XW⊤ and W∈Rnout×nin, the formulas become:
∂X∂L=∂Z∂LW
with shapes (m×nout)(nout×nin)=m×nin, and
∂W∂L=(∂Z∂L)⊤X
with shapes (nout×m)(m×nin)=nout×nin, matching the shape of W in PyTorch.
Recap
A neuron does three things: weighted sum, plus bias, through an activation function. A layer of neurons computing together is one matrix multiplication, and a whole batch at once is XW+b. Stack layers and you get a multilayer perceptron; without a nonlinearity in between, any number of layers is no better than one, but with ReLU hinges, enough neurons can build any continuous curve. Hidden layers usually use ReLU, GELU or SiLU; sigmoid and tanh are prone to vanishing gradients; softmax turns output scores into probabilities. The parameters are all the weights and biases. So now we have a function with thousands, or even billions, of knobs. Which way should each knob turn, and by how much? Next time, we start with derivatives.