AI Run Lab

All chapters

Neural networks

Connecting neurons in layers lets a model build a prediction in stages. Every line in a fully connected layer corresponds to a weight you can count.

Open this chapter with experiments and audio

How many parameters are in this network?

Read the diagram from left to right. Each input is one feature of the same example. Each hidden neuron receives every input and computes its own weighted sum and activation. Each output neuron receives the hidden activations. Set the widths to 4 inputs, 8 hidden neurons and 2 outputs. Predict how many lines you will see before reading the total.

The eight hidden neurons each need four weights. That is 32 lines. The two outputs each need eight weights, adding 16. A bias belongs to each hidden or output neuron, adding 10 numbers without drawn lines. The total is 32 + 16 + 10 = 58 trainable parameters. The four inputs are measurements, so they add no biases.

What happens when you add one neuron?

Move from eight to nine hidden neurons while keeping everything else fixed. The new neuron has four incoming connections and its own bias. Both outputs gain a connection to it. That adds seven parameters and brings the total to 65. Increasing only the number of examples in a batch would keep the total at 58 in the original network. Every example uses the same learned transformation.

parameters = inputs × hidden + hidden + hidden × outputs + outputs

def parameters(inputs, hidden, outputs):
    return (inputs + 1) * hidden + (hidden + 1) * outputs

print(parameters(4, 8, 2))
print(parameters(4, 9, 2))

What does a billion parameters occupy?

A parameter count tells you how many numerical values describe the learned model. Storage also depends on how each value is encoded. At 16 bits per value, every number uses two bytes. Multiply the count by two to obtain the raw weight storage. The table uses decimal units, where one GB is one billion bytes.

Parameter countRaw weights at 16 bits
1 million2 MB
100 million200 MB
1 billion2 GB
7 billion14 GB
70 billion140 GB

Runtime memory also includes intermediate results and, for generation, often a cache of earlier tokens. Training adds gradients and optimizer state. A 7B label alone therefore cannot tell you whether a complete training run will fit on a particular machine.

Follow one prediction through training

Forward pass
Apply the saved weights and compute outputs.
Loss
Compare the outputs with the target.
Backpropagation
Propagate derivatives backwards through the operations.
Optimizer
Use those gradients to update the parameters.

The values at the nodes depend on the current example. The weights on the lines are shared across examples. In a language model, a fact such as a capital city can influence the predictions of many prompts through many interacting weights. There is generally no single parameter you could label with that fact.

Can two networks with exactly 58 parameters make different predictions?

Yes. Even identical architectures can store different learned values. Their shapes and parameter counts match, but the computations use different numbers.

What does a narrow hidden layer make cheaper?

Compare two designs that accept 16 sensor readings and produce 16 outputs. A single dense linear layer without biases assigns a distinct weight to every input–output pair. It stores 16 × 16 = 256 weights and uses 256 scalar multiplications per example in the direct calculation. An alternative first maps the readings to four intermediate numbers and then maps those four to the 16 outputs. It stores 16 × 4 + 4 × 16 = 128 weights and uses 128 multiplications per example.

There is a cost to the saving. In the second design, with linear operations throughout, every output is a mixture of the same four intermediate measurements. Two inputs that have the same four measurements produce exactly the same outputs. In particular, no settings can make this network copy every possible 16-number input back unchanged. A fully dense 16-by-16 layer can do that with ones on its diagonal and zeros elsewhere. A narrow representation makes a claim about which variations the task can afford to lose.

What changes when you process ten examples, or add biases?

Ten examples reuse the weights. Multiplications rise to 2,560 versus 1,280 in this direct count. With one bias per output unit of each layer, parameter counts become 256 + 16 = 272 and 128 + 4 + 16 = 148. These counts exclude additions, memory transfers and device scheduling. A measured speedup depends on the actual implementation.

Change the width and inspect the weights and biases separately →

Further reading · PyTorch · Linear