All courses

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.

▶ Watch this lesson Start the whole course

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.

What are the 70 billion numbers in a model?

A model described as "70B" contains 70 billion numbers, and a model file is mostly those numbers stored on disk. What are they, and what does the model do with them when you send it a prompt? The answer is the basic vocabulary used for the rest of this topic: **layers, weights (parameters), activations and the forward pass**.

Fixed numbers in, scratch numbers out

A neural network is a function built as a chain of **layers**. A basic layer takes a vector, multiplies it by a matrix, adds a vector, then applies a simple nonlinear function to each entry. The matrix and vector entries are the **weights**, also called **parameters**. They are learned once from data and then stay fixed. The vectors computed along the way for one specific input are **activations**. They are scratch values, thrown away after each run.

One layer, and a stack of them

A row vector $x \in \mathbb{R}^{1\times d_{in}}$ goes through one layer: $$h = f(xW + b)$$ Here $W \in \mathbb{R}^{d_{in}\times d_{out}}$ and $b \in \mathbb{R}^{1\times d_{out}}$ are parameters, and $f$ is applied to each entry, e.g. $\max(0,\cdot)$. This layer has $d_{in}d_{out}+d_{out}$ parameters. Stacking layers gives $h_\ell = f(h_{\ell-1}W_\ell + b_\ell)$. Computing all of them in order is the **forward pass**. The model's size is the total parameter count.

One layer by hand

$x=[1,\,2]$, $W=\begin{bmatrix}1&-1&0\\2&1&-1\end{bmatrix}$, $b=[0,0,1]$, $f=\max(0,\cdot)$. $xW = [1{+}4,\; -1{+}2,\; 0{-}2] = [5,1,-2]$ $+b = [5,1,-1]$, then $f \Rightarrow [5,1,0]$ $[5,1,0]$ is an activation for this input. Parameters: $2\cdot3+3=9$. A different $x$ uses the same 9 numbers but produces different activations.

Many inputs, one set of weights

Stack $T$ inputs as rows of $X \in \mathbb{R}^{T\times d}$. Then $XW$ applies the **same** $W$ to every row at once. In an LLM, each row is one **token**: a word or piece of a word, represented as a vector. A model reuses its weights for every token and every input. Longer inputs create more activations but never add parameters.
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

A network is a stack of layers, each computing $f(xW+b)$. The entries of the $W$s and $b$s are the parameters: learned, fixed, shared across every token and input, and their count is the model's size. Activations are the per-input vectors produced during the forward pass.

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.