Concepts / Revisiting Neural Network Parameters

Revisiting Neural Network Parameters

The total number of parameters in a neural network is the sum of the parameters in each layer.

  • Programming

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
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.

python
Output (expected)
752
Notes on the code
  • model.parameters() returns the network's parameter list
  • requires_grad keeps only the parameters that are trainable
  • p.numel() returns how many individual numbers a parameter contains
  • sum(...) adds those element counts into one total
Output
752

The 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.

Rule

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.

Formula, step by step
  1. 1
    n_in x n_units counts the weights: one weight for every pairing of an incoming value with a unit
  2. 2
    n_units counts the biases: one bias carried by each unit
  3. 3
    The sum is the full parameter count for that single layer

Working the Three Layers by Hand

Worked example

Counting the first hidden layer

Two inputs feed a first hidden layer with 30 units. How many parameters does that layer contain?

  1. 1
    Identify the inputs and units

    n_in = 2 incoming values and n_units = 30 units.

  2. 2
    Count the weights

    n_in x n_units = 2 x 30 = 60.

  3. 3
    Count the bias units

    n_bias equals n_units, so n_bias = 30.

  4. 4
    Add the two parts

    60 + 30 = 90.

Result

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.

LayerInputsUnitsWeights (inputs x units)Bias unitsLayer total
First hidden layer230603090
Second hidden layer302060020620
Output layer20240242

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.

Layer 1: 902 to 30Layer 2: 62030 to 20Layer 3: 4220 to 2Network total: 752addaddadd
How does the total accumulate as each layer's parameter count is added in order from input to output?

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.

Inputs: 2Weights: 2 x 30 = 60Layer total: 90Units: 30Bias units: 30paired with each unitone weight per pairingone bias per unitaddadd
Which connections and bias terms are counted in the first hidden layer, and how the inputs, units, and bias units combine into its total of 90?

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.

python
Notes on the code
  • Prints one line per parameter tensor with its element count
  • Compare each printed count with the manual layer count for the same layer
Layer 1: 90Layer 1: 90Layer 2: 600Layer 2: 620Layer 3: 42Layer 3: 42
In this generated scenario, the hand count left out the 20 bias units of the second hidden layer. Lined up layer by layer, the first disagreement appears at the second layer, so that is where to look.
  • Common mistake

    Leaving out the bias units

    What it looks like

    Counting the second hidden layer as 30 x 20 = 600 and stopping there.

    Why it fails

    Each unit carries one bias, so the 20 units of that layer contribute 20 more parameters.

    Do this instead

    Add n_bias = 20 to get 620 for that layer.

  • Common mistake

    Counting weights per unit instead of per input-unit pair

    What it looks like

    Treating the first hidden layer as 30 parameters plus 30 biases, giving 60.

    Why it fails

    Each 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 instead

    Use 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 like

    Adding 90 and 620 but forgetting the output layer, giving 710.

    Why it fails

    The total is the sum over every layer, and the output layer also has parameters.

    Do this instead

    Include 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

EASY

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_in x n_units + n_bias to each layer separately, with n_bias equal to n_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

  1. 1
    The total number of parameters in a network is the sum of the parameters in each layer.
  2. 2
    For one layer, the count is inputs times units, plus the number of bias units, which equals the number of units.
  3. 3
    For the example network, the layer counts are 90, 620, and 42, giving a total of 752 that matches the programmatic count.
  4. 4
    When 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.

Related concepts

More concepts starting with R