Count weights in a small dense layer

Read a complete operation before typing its result. · 176 words

A dense layer connects every input to every output neuron. Suppose a model reads three input values and sends them to two output neurons. Each output needs one weight for each input, so the weight matrix has 3 x 2 = 6 weights. Draw six boxes: three feeding the first output and three feeding the second. The count does not depend on the numerical values of the inputs. It depends on the number of input features and the number of outputs.

Each output neuron also has a bias, an adjustable constant added after the weighted inputs. Two output neurons therefore add 2 biases, giving 6 + 2 = 8 trainable parameters in this dense layer. For comparison, a layer with 4 inputs and 3 outputs has 4 x 3 = 12 weights and 3 biases, or 15 parameters. A useful hand-check method is: multiply inputs by outputs for weights, then add outputs for biases. If a layer instead had no bias, its count would be only the weight product, such as 6 for the first example.

Check your recall

How many trainable parameters are in a dense layer with 5 inputs and 4 outputs when each output neuron has a bias?

Check your answer

There are 5 x 4 = 20 weights and 4 biases, for a total of 24 trainable parameters.

Sources and credits

Original KeyTouch lesson. Background reading · Google · Neural networks.