Designing a network starts with tracking the shape of the data through every layer.
What to build
A program that follows an input shape through a list of layers and counts parameters.
Requirements
- Layers:
conv F K S P(F filters, kernel K, stride S, padding P),pool K S(max pooling),flatten, anddense N. - Print
conv -> H x W x C, P parameters,pool -> H x W x C, 0 parameters,flatten -> Nanddense -> N, P parametersas each layer is applied. - Finally print
total parameters T.
Input
Line 1: H, W and C. Line 2: the number of layers L. Then L lines, one layer each. Dense layers always come after a flatten.
Output
One line per layer and the total line.
Constraints
L ≤ 20; all sizes stay at least 1.
Example
Input:
32 32 3
2
conv 16 3 1 1
pool 2 2
Output:
conv -> 32 x 32 x 16, 448 parameters
pool -> 16 x 16 x 16, 0 parameters
total parameters 448
Why it works
Each layer's output size follows from its kernel, stride and padding, and its parameter count depends only on the kernel and the channel counts, not on the image size.
Examples
32 32 3 2 conv 16 3 1 1 pool 2 2
conv -> 32 x 32 x 16, 448 parameters pool -> 16 x 16 x 16, 0 parameters total parameters 448
28 28 1 4 conv 8 5 1 0 pool 2 2 flatten dense 10
conv -> 24 x 24 x 8, 208 parameters pool -> 12 x 12 x 8, 0 parameters flatten -> 1152 dense -> 10, 11530 parameters total parameters 11738
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Convolution: new size (H + 2P − K) // S + 1, new channels F, parameters F × (K × K × C + 1). Pooling: (H − K) // S + 1, parameters 0.
Hint 2
Flatten gives H × W × C values; a dense layer with N units has (inputs + 1) × N parameters.
Solution
Show one correct solution (try it yourself first)
h, w, c = map(int, input().split())
flat = None
total = 0
for _ in range(int(input())):
p = input().split()
if p[0] == "conv":
f, k, s, pad = map(int, p[1:])
params = f * (k * k * c + 1)
h, w, c = (h + 2 * pad - k) // s + 1, (w + 2 * pad - k) // s + 1, f
total += params
print(f"conv -> {h} x {w} x {c}, {params} parameters")
elif p[0] == "pool":
k, s = map(int, p[1:])
h, w = (h - k) // s + 1, (w - k) // s + 1
print(f"pool -> {h} x {w} x {c}, 0 parameters")
elif p[0] == "flatten":
flat = h * w * c
print(f"flatten -> {flat}")
else:
n = int(p[1])
params = (flat + 1) * n
total += params
flat = n
print(f"dense -> {n}, {params} parameters")
print("total parameters", total)