#164Dense layer — count the parametersEasy

Dense layer — count the parameters

Background

A fully connected (dense) layer from ninn_{\text{in}} inputs to noutn_{\text{out}} outputs has a weight matrix plus one bias per output neuron:

params=nin×nout+nout\text{params} = n_{\text{in}} \times n_{\text{out}} + n_{\text{out}}

Every input connects to every output (ninnoutn_{\text{in}}\,n_{\text{out}} weights), and each output neuron adds a bias (noutn_{\text{out}} more).

Problem statement

Implement dense_params(n_in, n_out) returning the number of trainable parameters in one dense layer.

Input

  • n_inint, number of input neurons.
  • n_outint, number of output neurons.

Output

Returns an int: ninnout+noutn_{\text{in}}\,n_{\text{out}} + n_{\text{out}}.

Examples

Example 1

Input:  n_in = 5, n_out = 3
Output: 18

Explanation: 5×3+3=15+3=185\times3 + 3 = 15 + 3 = 18.

Example 2 — the lesson's first layer

Input:  n_in = 3, n_out = 4
Output: 16

Explanation: 3×4+4=163\times4 + 4 = 16.

Constraints

  • Count weights and biases: ninnout+noutn_{\text{in}}\,n_{\text{out}} + n_{\text{out}}.
  • Return a plain int.

Notes

  • A handy refactor is nout(nin+1)n_{\text{out}}\,(n_{\text{in}} + 1) — the "+1" is the bias folded in as an extra input that is always 1.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example: 5 inputs to 3 outputs
  • Reference: lesson layer 3 to 4
  • Sample: lesson layer 4 to 2