Weights, parameters and layers
A neural network is a function built from layers. A layer transforms vectors by multiplying them with matrices of learned numbers and applying simple nonlinear functions. Those learned numbers are the weights, or parameters: billions of them in an LLM, and the model's size is their count. The intermediate vectors computed for a particular input are called activations. Running an input through all the layers is the forward pass. The same weights are reused for every token and every input.
What the animation says
70 billion fixed numbers
A model called seventy B holds seventy billion numbers. Its file on disk is mostly those numbers. They sit inside layers. A neural network is a chain of layers. A layer takes a vector, multiplies it by a matrix, adds another vector, then turns every negative entry into zero. The entries of that matrix and that added vector are the weights, also called parameters. They're learned once, from data. So when you send a prompt, what happens to those seventy billion numbers?
One layer, by hand
The weights don't change. They're only read, and new scratch numbers come out. Let's run one tiny layer. The input x is one, two. The weight matrix has two rows and three columns. Each output entry is x times one column. That gives five, one, and minus two. Add the bias vector, zero, zero, one. Now we have five, one, minus one. Last step: take the max of zero and each entry. What comes out?
Activations vs. parameters
Five, one, zero. That vector is an activation. It's computed for this input and thrown away after the run. Feed a different x, and the same weights give a different activation. Now count the parameters. The matrix has two times three, so six entries. The bias adds three more. That's nine parameters. In general: inputs times outputs, plus one bias per output. Your turn. Tap the two blocks that are parameters.
Many tokens, one set of weights
Real inputs aren't one vector. In a language model, each token, a word or a piece of a word, is its own vector. Stack the tokens as rows of a matrix. One multiplication applies the same weight matrix to every row at once. Five tokens give five rows of activations. All of them went through the same nine numbers. Now make the input a hundred times longer: five hundred tokens. What happens to the parameter count?
Stacking layers: forward pass and size
Still nine. Five hundred rows of activations, but the same nine weights. A real network stacks layers. Here the widths are eight, thirty-two, thirty-two, and four. Count each layer the same way, inputs times outputs plus outputs: two hundred eighty-eight, one thousand fifty-six, one hundred thirty-two. Total: one thousand four hundred seventy-six. That total is the model's size. This loop is the forward pass. Each layer feeds the next: five inputs in, five rows of four out. Scale that up, and you reach seventy billion.
Fixed numbers in, scratch numbers out
One layer, and a stack of them
One layer by hand
Many inputs, one set of weights
import numpy as np
rng = np.random.default_rng(0)
sizes = [8, 32, 32, 4] # layer widths
Ws = [rng.normal(size=(a, b)) for a, b in zip(sizes, sizes[1:])]
bs = [np.zeros(b) for b in sizes[1:]]
print(sum(W.size + b.size for W, b in zip(Ws, bs))) # 1476 parameters
X = rng.normal(size=(5, 8)) # 5 inputs as rows, [T, d]
for W, b in zip(Ws, bs): # forward pass
X = np.maximum(0, X @ W + b) # activations, [5, width]
print(X.shape) # (5, 4)
Remember this
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses