Counting Parameters Exactly
Count every weight matrix entry and bias scalar across consecutive dense layers from a vector of layer widths.
The problem
A dense neural network is often sketched as circles and arrows, hiding how many trainable scalars actually live inside its layers. Suppose a two-layer classifier maps input features to hidden units and then to class scores, represented by the width vector [4, 10, 3]. How many trainable parameters sit in each affine layer, and how many sit in the network overall?
Getting an exact count requires tracking both parts of every dense map . Each output unit owns one weight per incoming feature plus its own scalar bias, so forgetting biases or attaching biases to inputs instead of outputs produces the wrong parameter budget.
Work through the numbers
Take consecutive pairs from [4, 10, 3]. Layer 0 maps inputs to hidden units. Its weight matrix has shape , holding weights, and its bias vector holds scalars, one per hidden unit. Together Layer 0 stores parameters.
Layer 1 maps those hidden activations to output logits. Its weight matrix has shape , holding weights, and its bias vector holds scalars, one per output class. Together Layer 1 stores parameters.
Across the two layers, the per-layer parameter vector is [50, 33] and the network total is scalars: weights and biases. Dropping the bias vectors undercounts Layer 0 as and Layer 1 as , missing all intercept terms.
The live model
The chart plots each dense layer's parameter count alongside the network sum as hidden_width changes inside [4, hidden_width, 3]. At the default hidden_width , the bars show [50, 33] and total . Dragging hidden_width down to shrinks the counts to [20, 15] and total ; dragging hidden_width up to gives [80, 51] and total , and at the counts reach [160, 99] and total .
![Horizontal bars for dense architecture [4, 10, 3] showing 50 parameters in layer 0, 33 in layer 1, and 83 total.](../assets/ml/counting-parameters-exactly--demo.png)
let hidden_width = scene.parameter("hidden_width", 10.0, 4.0, 32.0, 2.0);
let w0 = input.slice(0, 0, 1);
let wh = torch.full(1, hidden_width).to(DType.F64);
let w2 = input.slice(0, 2, 3);
let widths = torch.cat(torch.list().add(w0).add(wh).add(w2), 0);
let n = widths.size(0);
let d_in = widths.slice(0, 0, n - 1);
let d_out = widths.slice(0, 1, n);
let answer = (d_in + 1.0) * d_out;
out.set("layer_params", answer).set("total_params", answer.sum());
{
"layer_params": [
50,
33
],
"total_params": 83
}Watch the parameter change
Your turn
Given a 1D layer-width vector input of length , answer must hold the length- vector of exact trainable parameter counts (weights plus output biases) for each consecutive dense layer. The starter slices d_in and d_out and leaves a zero placeholder for answer; on [4, 10, 3], the completed model returns [50, 33]. Check tests networks with different depths and layer widths.
![Starter parameter bars for [4, 10, 3] showing zero placeholders [0, 0] and total 0 before the per-layer formula is entered.](../assets/ml/counting-parameters-exactly--exercise.png)
let n = input.size(0);
let d_in = input.slice(0, 0, n - 1);
let d_out = input.slice(0, 1, n);
let answer = torch.zeros(n - 1).to(DType.F64);
out.set("layer_params", answer).set("total_params", answer.sum());
{
"layer_params": [
0,
0
],
"total_params": 0
}Return the vector of exact parameter counts (weights plus biases) for every consecutive dense layer defined by the width vector input.
Read the result carefully
Notice the linear slope when one hidden layer widens in . Increasing by adds incoming weights and hidden bias in Layer 0 plus outgoing weights in Layer 1, while the output biases stay fixed. Every extra hidden neuron adds parameters: moving from to adds ( to ), and moving from to adds ( to ).
Once two adjacent hidden layers widen together, however, their shared weight matrix grows with the product of both widths. A pair of 1,000-unit hidden layers holds parameters in a single layer, turning a two-line architecture spec into megabytes of weights.
Recap
- A dense layer from inputs to outputs stores weights plus biases, totaling parameters.
- Bias count matches the output width of each layer, never the incoming feature width .
- Widening one isolated hidden layer grows parameter count linearly, whereas widening two consecutive hidden layers multiplies their connecting weight matrix.