Revisiting Neural Network Parameters
The total number of parameters in a neural network is the sum of the parameters in each layer.
Why Count Parameters Before Training
A neural network is defined by its parameters, the learned numbers that the network adjusts during training. Before you can reason about training cost, memory, or model size, you need to know how many of these parameters a network has. This article works through one concrete network and counts its parameters two ways: once by hand, and once with code. The goal is not just to get the right answer but to build a habit of checking one method against the other.
What do you think happens?
Before reading on, make a guess: how many trainable numbers do you think a network with 2 inputs, hidden layers of 30 and 20 nodes, and 2 outputs contains in total?
Reveal answer
752
The programmatic count over the trainable parameters reports 752. The rest of this article shows how to reach that same total by hand, one layer at a time.
The Network Being Counted
The network in this example has two inputs and two outputs. It also has two hidden layers, with 30 nodes in the first hidden layer and 20 nodes in the second. Every count in this article refers to this same architecture, so you can follow the numbers from one section to the next without changing the network.
Read the architecture as a chain of connections. The 2 input values feed the first hidden layer of 30 units. That layer feeds the second hidden layer of 20 units, which feeds the 2 output units. Each arrow in this chain is a layer whose parameters must be counted, and the network's total count is built from those layers.
Counting Every Number in Code
Start with the programmatic route. The code below builds the network, asks it for its parameters, keeps only the parameters that are trainable, and adds up how many numbers they contain. Read it once before the explanation that follows, and notice what the code counts: it counts individual numbers (elements), not parameter tensors.
752model.parameters()returns the network's parameter listrequires_gradkeeps only the parameters that are trainablep.numel()returns how many individual numbers a parameter containssum(...)adds those element counts into one total
752The program reports 752. Keep that number in mind, because the next step asks you to reach the same total without running any code.
The Rule for One Layer
The total number of parameters in a neural network is found by summing the parameters in each layer. So the central task is to determine the parameter count for one layer and then repeat that for every layer. The source describes the rule for a single layer: the number of parameters is the product of the number of inputs and the number of units, plus the number of bias units.
In this formula, $n_{\text{in}}$ is the number of incoming values to the layer, $n_{\text{units}}$ is the number of units in the layer, and $n_{\text{bias}}$ is the number of bias units, which is equal to the number of units, so $n_{\text{bias}} = n_{\text{units}}$. Each unit receives every incoming value through its own weight, which is why the product appears, and each unit also carries one bias, which is why the bias term is added.
- 1
n_inxn_unitscounts the weights: one weight for every pairing of an incoming value with a unit - 2
n_unitscounts the biases: one bias carried by each unit - 3The sum is the full parameter count for that single layer
Working the Three Layers by Hand
Counting the first hidden layer
Two inputs feed a first hidden layer with 30 units. How many parameters does that layer contain?
- 1Identify the inputs and units
n_in= 2 incoming values andn_units= 30 units. - 2Count the weights
n_inxn_units= 2 x 30 = 60. - 3Count the bias units
n_biasequalsn_units, son_bias= 30. - 4Add the two parts
60 + 30 = 90.
The first layer contributes 90 parameters.
The same procedure applies to the remaining layers. The second hidden layer receives 30 values from the first hidden layer and has 20 units, so its weights number 30 x 20 = 600 and its bias units number 20, giving 620. The output layer receives 20 values and has 2 units, so its weights number 20 x 2 = 40 and its bias units number 2, giving 42.
| Layer | Inputs | Units | Weights (inputs x units) | Bias units | Layer total |
|---|---|---|---|---|---|
| First hidden layer | 2 | 30 | 60 | 30 | 90 |
| Second hidden layer | 30 | 20 | 600 | 20 | 620 |
| Output layer | 20 | 2 | 40 | 2 | 42 |
Per-layer parameter counts for the network, computed by hand
Adding Layers Into a Total
Once each layer has its own count, the network total is the running sum from the first layer to the last. The layer counts 90, 620, and 42 add to 752, which matches the programmatic result. The diagram below shows how the total accumulates as each layer's count is added in sequence.
The first-layer breakdown is worth studying in detail. Its 90 parameters are not just 30 weights per unit. They come from 2 weights per unit across 30 units, which is 60, plus one bias for each of the 30 units. The diagram below breaks that single layer into its parts.
Where Manual and Code Counts Split
The manual method and the programmatic method count the same thing: every trainable parameter in the network. The programmatic version counts individual numbers through the model's own parameter list, and the manual version reconstructs the same count from the architecture. If the architecture you described by hand is the architecture the code built, the two totals must match. When they do not match, the mismatch itself is useful information, because it tells you that one of the two descriptions of the network is wrong.
When a manual total and a programmatic total disagree, avoid guessing at a fix. Instead, compare them layer by layer. Compute the manual count for each layer and print the element count for each parameter from the model. The first layer where the two sides disagree is the place to look. This approach turns a single wrong total into a specific location, which is far easier to fix.
- Prints one line per parameter tensor with its element count
- Compare each printed count with the manual layer count for the same layer
Common mistake Leaving out the bias units
What it looks likeCounting the second hidden layer as 30 x 20 = 600 and stopping there.
Why it failsEach unit carries one bias, so the 20 units of that layer contribute 20 more parameters.
Do this insteadAdd n_bias = 20 to get 620 for that layer.
Common mistake Counting weights per unit instead of per input-unit pair
What it looks likeTreating the first hidden layer as 30 parameters plus 30 biases, giving 60.
Why it failsEach of the 30 units receives every one of the 2 incoming values through its own weight, so the weight count is 2 x 30.
Do this insteadUse n_in x n_units = 2 x 30 = 60 weights, then add 30 biases for 90.
Common mistake Summing only some layers
What it looks likeAdding 90 and 620 but forgetting the output layer, giving 710.
Why it failsThe total is the sum over every layer, and the output layer also has parameters.
Do this insteadInclude the output layer's 42 to reach 752.
Common mistake Changing the manual formula to fix a mismatch before finding the layer where the counts diverge
Practice the Layer Count
A small network has 3 inputs, one hidden layer of 4 units, and 1 output unit. Count the parameters of each layer by hand, then add them to get the network total. Check your total against a per-parameter count from code if you can.
Hints
- Apply
n_inxn_units+n_biasto each layer separately, withn_biasequal ton_units. - The output layer's inputs are the 4 values coming from the hidden layer, not the original 3 inputs.
- The expected answer is 21: 16 for the hidden layer and 5 for the output layer.
Summary
- 1The total number of parameters in a network is the sum of the parameters in each layer.
- 2For one layer, the count is inputs times units, plus the number of bias units, which equals the number of units.
- 3For the example network, the layer counts are 90, 620, and 42, giving a total of 752 that matches the programmatic count.
- 4When manual and programmatic totals disagree, compare them layer by layer to find the first mismatch rather than guessing at a fix.
At a glance
- The total number of parameters in a network is the sum of the parameters in each layer.
- For one layer, the count is inputs times units, plus the number of bias units, which equals the number of units.
- For the example network, the layer counts are 90, 620, and 42, giving a total of 752 that matches the programmatic count.
- When manual and programmatic totals disagree, compare them layer by layer to find the first mismatch rather than guessing at a fix.