Speed

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 44 input features to 1010 hidden units and then to 33 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 y=Wx+by = W x + b. 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 din=4d_{\mathrm{in}} = 4 inputs to dout=10d_{\mathrm{out}} = 10 hidden units. Its weight matrix W0W_0 has shape 10×410 \times 4, holding 4⋅10=404 \cdot 10 = 40 weights, and its bias vector b0b_0 holds 1010 scalars, one per hidden unit. Together Layer 0 stores (4+1)⋅10=50(4 + 1) \cdot 10 = 50 parameters.

Layer 1 maps those din=10d_{\mathrm{in}} = 10 hidden activations to dout=3d_{\mathrm{out}} = 3 output logits. Its weight matrix W1W_1 has shape 3×103 \times 10, holding 10⋅3=3010 \cdot 3 = 30 weights, and its bias vector b1b_1 holds 33 scalars, one per output class. Together Layer 1 stores (10+1)⋅3=33(10 + 1) \cdot 3 = 33 parameters.

Across the two layers, the per-layer parameter vector is [50, 33] and the network total is 50+33=8350 + 33 = 83 scalars: 7070 weights and 1313 biases. Dropping the bias vectors undercounts Layer 0 as 4040 and Layer 1 as 3030, missing all 1313 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 =10= 10, the bars show [50, 33] and total 8383. Dragging hidden_width down to 44 shrinks the counts to [20, 15] and total 3535; dragging hidden_width up to 1616 gives [80, 51] and total 131131, and at 3232 the counts reach [160, 99] and total 259259.

Horizontal bars for dense architecture [4, 10, 3] showing 50 parameters in layer 0, 33 in layer 1, and 83 total.
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 L+1L + 1, answer must hold the length-LL 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.
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 [4,h,3][4, h, 3]. Increasing hh by 11 adds 44 incoming weights and 11 hidden bias in Layer 0 plus 33 outgoing weights in Layer 1, while the 33 output biases stay fixed. Every extra hidden neuron adds (4+1)+3=8(4 + 1) + 3 = 8 parameters: moving hh from 44 to 1010 adds 6⋅8=486 \cdot 8 = 48 (3535 to 8383), and moving hh from 1010 to 3232 adds 22⋅8=17622 \cdot 8 = 176 (8383 to 259259).

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 (1,000+1)⋅1,000=1,001,000(1{,}000 + 1) \cdot 1{,}000 = 1{,}001{,}000 parameters in a single layer, turning a two-line architecture spec into megabytes of weights.

Recap

  • A dense layer from dind_{\mathrm{in}} inputs to doutd_{\mathrm{out}} outputs stores din⋅doutd_{\mathrm{in}} \cdot d_{\mathrm{out}} weights plus doutd_{\mathrm{out}} biases, totaling (din+1)⋅dout(d_{\mathrm{in}} + 1) \cdot d_{\mathrm{out}} parameters.
  • Bias count matches the output width doutd_{\mathrm{out}} of each layer, never the incoming feature width dind_{\mathrm{in}}.
  • Widening one isolated hidden layer grows parameter count linearly, whereas widening two consecutive hidden layers multiplies their connecting weight matrix.