Deep Learning · MT2 · section
R26 Q6a
R26 selected MT2 practice
Question
Question (10 marks). For the binary CNN below, calculate output dimensions and trainable parameters after every layer. Input 256 × 256 × 3. → Conv K = 5, filters = 32, stride = 1, padding = 0. → Pool K = 2, stride = 2, padding = 0. → Conv K = 5, filters = 64, stride = 1, padding = 0. → Pool K = 2, stride = 2, padding = 0. → Conv K = 5, filters = 128, stride = 1, padding = 1. → Pool K = 2, stride = 2, padding = 0. → Flatten. → Dense 84 units. → Dense 32 units. → Dense 1 units. The printed output size is unspecified; assume one sigmoid output. Unspecified convolution padding is taken as valid.
Answer — step by step
Given
Input 256 × 256 × 3.
Conv K = 5, filters = 32, stride = 1, padding = 0.
Pool K = 2, stride = 2, padding = 0.
Conv K = 5, filters = 64, stride = 1, padding = 0.
Pool K = 2, stride = 2, padding = 0.
Conv K = 5, filters = 128, stride = 1, padding = 1.
Pool K = 2, stride = 2, padding = 0.
Dense 84 units.
Dense 32 units.
Dense 1 units.
assume one sigmoid output.
To find
Find every layer shape, its parameter count and the network total.
Formula — which one and why
Complete CNN calculation. Carry each output shape into the next layer. Convolution changes spatial size and channel count; pooling changes spatial size; flattening converts all activations into dense-layer inputs.
H,W,C = feature-map height, width and channels; K = kernel size; S = stride; P_pad = padding. One bias is assumed for each learned filter or dense neuron unless specified otherwise.
N_{\mathrm{out}}=\left\lfloor\frac{N+2P_{\mathrm{pad}}-K}{S}\right\rfloor+1P_{\mathrm{conv}}=(K^2C_{\mathrm{in}}+1)C_{\mathrm{out}}n_{\mathrm{flat}}=HWCP_{\mathrm{dense}}=(n_{\mathrm{in}}+1)n_{\mathrm{out}}P_{\mathrm{pool}}=P_{\mathrm{flatten}}=0Yaad rakhne ka cue: Ek layer ka output, next layer ka input; shape aur parameters alag count.
Solution — step by step
Use output size LaTeX:
\left\lfloor\frac{N+2P-K}{S}\right\rfloor+1, Conv weights K² × Cin × Cout and biases Cout, Dense weights Nin × Nout and biases Nout. Assume a bias per learned filter/unit, full input-channel connectivity and no trainable pooling parameters. ReLU and softmax activations themselves add no trainable parameters.
Layer-by-layer calculation
Carry the complete output shape of one layer into the next. Spatial size determines the number of activations; it does not multiply the number of shared convolution parameters.
Layer 1 — convolution
Spatial positions share the filter weights. Output depth equals the number of filters.
Input: 256 × 256 × 3. Use 32 filters of spatial size 5 × 5, stride 1, and padding 0 on each side.
1. Calculate height and width.
N_{\mathrm{out}}=\left\lfloor\frac{256+2(0)-5}{1}\right\rfloor+1=2522. Determine output channels. Each filter produces one feature map. Therefore the output is 252 × 252 × 32.
3. Count trainable values. Each filter receives all 3 input channels. Its weights are reused at every spatial position.
N_{\mathrm{weights}}=5\times5\times3\times32=2{,}400N_{\mathrm{biases}}=32P_1=2{,}400+32=2{,}432Layer 2 — pooling
Spatial dimensions change; the channel count stays the same. Pooling learns no weights.
Input: 252 × 252 × 32. Use a 2 × 2 window, stride 2, and zero padding.
N_{\mathrm{out}}=\left\lfloor\frac{252-2}{2}\right\rfloor+1=126Pooling acts independently on each channel. The output is 126 × 126 × 32.
Trainable parameters: 0. Max/average pooling has no learned filter weights or biases.
Layer 3 — convolution
Spatial positions share the filter weights. Output depth equals the number of filters.
Input: 126 × 126 × 32. Use 64 filters of spatial size 5 × 5, stride 1, and padding 0 on each side.
1. Calculate height and width.
N_{\mathrm{out}}=\left\lfloor\frac{126+2(0)-5}{1}\right\rfloor+1=1222. Determine output channels. Each filter produces one feature map. Therefore the output is 122 × 122 × 64.
3. Count trainable values. Each filter receives all 32 input channels. Its weights are reused at every spatial position.
N_{\mathrm{weights}}=5\times5\times32\times64=51{,}200N_{\mathrm{biases}}=64P_3=51{,}200+64=51{,}264Layer 4 — pooling
Spatial dimensions change; the channel count stays the same. Pooling learns no weights.
Input: 122 × 122 × 64. Use a 2 × 2 window, stride 2, and zero padding.
N_{\mathrm{out}}=\left\lfloor\frac{122-2}{2}\right\rfloor+1=61Pooling acts independently on each channel. The output is 61 × 61 × 64.
Trainable parameters: 0. Max/average pooling has no learned filter weights or biases.
Layer 5 — convolution
Spatial positions share the filter weights. Output depth equals the number of filters.
Input: 61 × 61 × 64. Use 128 filters of spatial size 5 × 5, stride 1, and padding 1 on each side.
1. Calculate height and width.
N_{\mathrm{out}}=\left\lfloor\frac{61+2(1)-5}{1}\right\rfloor+1=592. Determine output channels. Each filter produces one feature map. Therefore the output is 59 × 59 × 128.
3. Count trainable values. Each filter receives all 64 input channels. Its weights are reused at every spatial position.
N_{\mathrm{weights}}=5\times5\times64\times128=204{,}800N_{\mathrm{biases}}=128P_5=204{,}800+128=204{,}928Layer 6 — pooling
Spatial dimensions change; the channel count stays the same. Pooling learns no weights.
Input: 59 × 59 × 128. Use a 2 × 2 window, stride 2, and zero padding.
N_{\mathrm{out}}=\left\lfloor\frac{59-2}{2}\right\rfloor+1=29Pooling acts independently on each channel. The output is 29 × 29 × 128.
Trainable parameters: 0. Max/average pooling has no learned filter weights or biases.
The division gives 28.5; take its floor before adding 1. A partial window outside the unpadded input is not included.
Layer 7 — flatten
The same activation values are rearranged into one vector. No parameter is learned.
Input: 29 × 29 × 128. Rearrange all activation values into one vector.
n_{\mathrm{flat}}=29\times29\times128=107{,}648Output: a vector with 107,648 entries. Flattening changes the arrangement, so it adds 0 trainable parameters.
Layer 8 — fully connected layer
Every output neuron receives every input feature, with one bias per output neuron.
Input: 107,648 features. Output: 84 neurons. Every output neuron connects to every input feature and has one bias.
N_{\mathrm{weights}}=107{,}648\times84=9{,}042{,}432N_{\mathrm{biases}}=84P_8=9{,}042{,}432+84=9{,}042{,}516Layer 9 — fully connected layer
Every output neuron receives every input feature, with one bias per output neuron.
Input: 84 features. Output: 32 neurons. Every output neuron connects to every input feature and has one bias.
N_{\mathrm{weights}}=84\times32=2{,}688N_{\mathrm{biases}}=32P_9=2{,}688+32=2{,}720Layer 10 — fully connected layer
Every output neuron receives every input feature, with one bias per output neuron.
Input: 32 features. Output: 1 neurons. Every output neuron connects to every input feature and has one bias.
N_{\mathrm{weights}}=32\times1=32N_{\mathrm{biases}}=1P_10=32+1=33Summary of the complete network
| Layer | Spatial or flatten calculation | Output | Weights | Biases | Total |
|---|---|---|---|---|---|
| 1 conv | \left\lfloor\frac{256+2 \times 0-5}{1}\right\rfloor+1 | 252 x 252 x 32 | 2,400 | 32 | 2,432 |
| 2 pool | \left\lfloor\frac{252-2}{2}\right\rfloor+1 | 126 x 126 x 32 | 0 | 0 | 0 |
| 3 conv | \left\lfloor\frac{126+2 \times 0-5}{1}\right\rfloor+1 | 122 x 122 x 64 | 51,200 | 64 | 51,264 |
| 4 pool | \left\lfloor\frac{122-2}{2}\right\rfloor+1 | 61 x 61 x 64 | 0 | 0 | 0 |
| 5 conv | \left\lfloor\frac{61+2 \times 1-5}{1}\right\rfloor+1 | 59 x 59 x 128 | 204,800 | 128 | 204,928 |
| 6 pool | \left\lfloor\frac{59-2}{2}\right\rfloor+1 | 29 x 29 x 128 | 0 | 0 | 0 |
| 7 flat | 29 × 29 × 128 | 107648 | 0 | 0 | 0 |
| 8 dense | 107648 → 84 | 84 | 9,042,432 | 84 | 9,042,516 |
| 9 dense | 84 → 32 | 32 | 2,688 | 32 | 2,720 |
| 10 dense | 32 → 1 | 1 | 32 | 1 | 33 |
Parameter substitutions, in layer order:
Layer 1: (5 x 5 x 3 + 1) x 32 = 2,432.
Layer 3: (5 x 5 x 32 + 1) x 64 = 51,264.
Layer 5: (5 x 5 x 64 + 1) x 128 = 204,928.
Layer 8: (107648 + 1) x 84 = 9,042,516.
Layer 9: (84 + 1) x 32 = 2,720.
Layer 10: (32 + 1) x 1 = 33.
Total: 2,432 + 51,264 + 204,928 + 9,042,516 + 2,720 + 33 = 9,303,893 trainable parameters. Pooling changes activation size; it does not itself add learned weights. Always floor noninteger spatial dimensions.
Correction: Conv 3 has (5 × 5 × 64+1) × 128 = 204,928 parameters, not 73,856 as printed in the key. Correct total is 9,303,893. If binary classification is instead implemented with two softmax outputs, the last dense layer has(32+1) × 2 = 66 parameters and the total becomes 9,303,926; explicitly state the output convention.