Deep learning: Count the parameters

Find out how big a network really is.

What to build

A program that counts the weights and biases of a fully connected network.

Requirements

  • The input lists the layer sizes from the inputs to the output.
  • Print layer K: P for each layer of weights (from 1), then total T.

Input

One line: the layer sizes.

Output

One line per layer, then the total.

Constraints

2 to 20 sizes, each at most 100,000.

Example

Input:

2 3 1

Output:

layer 1: 9
layer 2: 4
total 13

Why it works

Every connection between two layers is a weight, and every neuron after the input has a bias.

Examples

Example 1 · Input
2 3 1
Expected output
layer 1: 9
layer 2: 4
total 13
Example 2 · Input
784 128 10
Expected output
layer 1: 100480
layer 2: 1290
total 101770

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Between a layer of size a and the next of size b there are a × b weights and b biases.

Hint 2

Add the layers up for the total.

Solution

Show one correct solution (try it yourself first)
sizes = list(map(int, input().split()))
total = 0
for k, (a, b) in enumerate(zip(sizes, sizes[1:]), start=1):
    count = a * b + b
    total += count
    print(f"layer {k}: {count}")
print("total", total)

Lesson: Training a multi-layer network →