Two Heads, One Backbone

Convolutional ArchitecturesHard+30 XP+30

Two Heads, One Backbone

VGG-16 carries 138 million weights, and its conv stack is only about 15 million of them. The rest sit in the dense layers bolted onto the end. Global average pooling later deleted almost all of that by averaging each feature map down to a single number, using no weights at all.

You are given a network description and asked to produce the ledger: what the backbone costs, what each of the two candidate heads costs, and how lopsided the result is.

Task: write weight_ledger(size, depth, stages, hidden, classes).

  • The input is a size × size grid of depth channels.
  • stages is a list of [num_convs, filters] pairs. A stage runs num_convs 3×33\times33×3 convolutions one after another — each outputs filters feature maps, with "same" padding so the grid is unchanged — and then a 2×22\times22×2 max pool with stride 2, which halves each side. Every stage ends with a pool, including the last. If a side is odd, the pool drops the leftover row and column (⌊s/2⌋\lfloor s/2 \rfloor⌊s/2⌋).
  • A conv layer costs 3×3×(input depth)×(filters)3 \times 3 \times (\text{input depth}) \times (\text{filters})3×3×(input depth)×(filters) weights, plus one bias per filter. Pooling and activations cost nothing.
  • Two candidate heads are then measured on whatever block comes out:
    • the dense head flattens the entire block, feeds it to a dense layer of hidden units, then to a dense layer of classes units;
    • the GAP head averages each feature map to one number — no weights — then feeds those to a single dense layer of classes units;
    • a dense layer costs (inputs)×(units)(\text{inputs}) \times (\text{units})(inputs)×(units) weights, plus one bias per unit.

Return [backbone, dense_head, gap_head, share], where the first three are the weight counts as plain integers and share is the fraction of a dense-headed network's total that sits in its head,

share=dense_headbackbone+dense_head\text{share} = \frac{\text{dense\_head}}{\text{backbone} + \text{dense\_head}}share=backbone+dense_headdense_head​

rounded to 4 decimal places.

The first test is VGG-16's shape, right down to the block it finishes on. The last number you return is the one that explains why anyone bothered inventing global average pooling — and why a backbone transfers to a new task so much more readily than the thing sitting on top of it.

weights in this one dense layer
103M
grid 7×7

Older networks flattened into huge dense layers. In VGG-16 these ate roughly 90% of the entire network's weights — for the last two layers.

Examples

size = 224
depth = 3
stages = [[2, 64], [2, 128], [3, 256], [3, 512], [3, 512]]
hidden = 4096
classes = 1000
returns [14714688, 106861544, 513000, 0.879]
size = 32
depth = 3
stages = [[1, 16], [1, 32]]
hidden = 64
classes = 10
returns [5088, 131786, 330, 0.9628]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run