Deep Learning · MT2 · section
R24 Q4b
R24 selected MT2 practice
Question
Question (10 marks). Calculate weights, biases and feature-map sizes of the CNN: Input 227 × 227 × 3. → Conv K = 9, 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 = 64, stride = 1, padding = 0. → Pool K = 2, stride = 2, padding = 0. → Flatten. → Dense 120 units. → Dense 84 units. → Dense 10 units.
Answer — step by step
Given
Input 227 × 227 × 3.
Conv K = 9, 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 = 64, stride = 1, padding = 0.
Pool K = 2, stride = 2, padding = 0.
Dense 120 units.
Dense 84 units.
Dense 10 units.
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: 227 × 227 × 3. Use 32 filters of spatial size 9 × 9, stride 1, and padding 0 on each side.
1. Calculate height and width.
N_{\mathrm{out}}=\left\lfloor\frac{227+2(0)-9}{1}\right\rfloor+1=2192. Determine output channels. Each filter produces one feature map. Therefore the output is 219 × 219 × 32.
3. Count trainable values. Each filter receives all 3 input channels. Its weights are reused at every spatial position.
N_{\mathrm{weights}}=9\times9\times3\times32=7{,}776N_{\mathrm{biases}}=32P_1=7{,}776+32=7{,}808Layer 2 — pooling
Spatial dimensions change; the channel count stays the same. Pooling learns no weights.
Input: 219 × 219 × 32. Use a 2 × 2 window, stride 2, and zero padding.
N_{\mathrm{out}}=\left\lfloor\frac{219-2}{2}\right\rfloor+1=109Pooling acts independently on each channel. The output is 109 × 109 × 32.
Trainable parameters: 0. Max/average pooling has no learned filter weights or biases.
The division gives 108.5; take its floor before adding 1. A partial window outside the unpadded input is not included.
Layer 3 — convolution
Spatial positions share the filter weights. Output depth equals the number of filters.
Input: 109 × 109 × 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{109+2(0)-5}{1}\right\rfloor+1=1052. Determine output channels. Each filter produces one feature map. Therefore the output is 105 × 105 × 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: 105 × 105 × 64. Use a 2 × 2 window, stride 2, and zero padding.
N_{\mathrm{out}}=\left\lfloor\frac{105-2}{2}\right\rfloor+1=52Pooling acts independently on each channel. The output is 52 × 52 × 64.
Trainable parameters: 0. Max/average pooling has no learned filter weights or biases.
The division gives 51.5; take its floor before adding 1. A partial window outside the unpadded input is not included.
Layer 5 — convolution
Spatial positions share the filter weights. Output depth equals the number of filters.
Input: 52 × 52 × 64. 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{52+2(0)-5}{1}\right\rfloor+1=482. Determine output channels. Each filter produces one feature map. Therefore the output is 48 × 48 × 64.
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\times64=102{,}400N_{\mathrm{biases}}=64P_5=102{,}400+64=102{,}464Layer 6 — pooling
Spatial dimensions change; the channel count stays the same. Pooling learns no weights.
Input: 48 × 48 × 64. Use a 2 × 2 window, stride 2, and zero padding.
N_{\mathrm{out}}=\left\lfloor\frac{48-2}{2}\right\rfloor+1=24Pooling acts independently on each channel. The output is 24 × 24 × 64.
Trainable parameters: 0. Max/average pooling has no learned filter weights or biases.
Layer 7 — flatten
The same activation values are rearranged into one vector. No parameter is learned.
Input: 24 × 24 × 64. Rearrange all activation values into one vector.
n_{\mathrm{flat}}=24\times24\times64=36{,}864Output: a vector with 36,864 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: 36,864 features. Output: 120 neurons. Every output neuron connects to every input feature and has one bias.
N_{\mathrm{weights}}=36{,}864\times120=4{,}423{,}680N_{\mathrm{biases}}=120P_8=4{,}423{,}680+120=4{,}423{,}800Layer 9 — fully connected layer
Every output neuron receives every input feature, with one bias per output neuron.
Input: 120 features. Output: 84 neurons. Every output neuron connects to every input feature and has one bias.
N_{\mathrm{weights}}=120\times84=10{,}080N_{\mathrm{biases}}=84P_9=10{,}080+84=10{,}164Layer 10 — fully connected layer
Every output neuron receives every input feature, with one bias per output neuron.
Input: 84 features. Output: 10 neurons. Every output neuron connects to every input feature and has one bias.
N_{\mathrm{weights}}=84\times10=840N_{\mathrm{biases}}=10P_10=840+10=850Summary of the complete network
| Layer | Spatial or flatten calculation | Output | Weights | Biases | Total |
|---|---|---|---|---|---|
| 1 conv | \left\lfloor\frac{227+2 \times 0-9}{1}\right\rfloor+1 | 219 x 219 x 32 | 7,776 | 32 | 7,808 |
| 2 pool | \left\lfloor\frac{219-2}{2}\right\rfloor+1 | 109 x 109 x 32 | 0 | 0 | 0 |
| 3 conv | \left\lfloor\frac{109+2 \times 0-5}{1}\right\rfloor+1 | 105 x 105 x 64 | 51,200 | 64 | 51,264 |
| 4 pool | \left\lfloor\frac{105-2}{2}\right\rfloor+1 | 52 x 52 x 64 | 0 | 0 | 0 |
| 5 conv | \left\lfloor\frac{52+2 \times 0-5}{1}\right\rfloor+1 | 48 x 48 x 64 | 102,400 | 64 | 102,464 |
| 6 pool | \left\lfloor\frac{48-2}{2}\right\rfloor+1 | 24 x 24 x 64 | 0 | 0 | 0 |
| 7 flat | 24 × 24 × 64 | 36864 | 0 | 0 | 0 |
| 8 dense | 36864 → 120 | 120 | 4,423,680 | 120 | 4,423,800 |
| 9 dense | 120 → 84 | 84 | 10,080 | 84 | 10,164 |
| 10 dense | 84 → 10 | 10 | 840 | 10 | 850 |
Parameter substitutions, in layer order:
Layer 1: (9 x 9 x 3 + 1) x 32 = 7,808.
Layer 3: (5 x 5 x 32 + 1) x 64 = 51,264.
Layer 5: (5 x 5 x 64 + 1) x 64 = 102,464.
Layer 8: (36864 + 1) x 120 = 4,423,800.
Layer 9: (120 + 1) x 84 = 10,164.
Layer 10: (84 + 1) x 10 = 850.
Total: 7,808 + 51,264 + 102,464 + 4,423,800 + 10,164 + 850 = 4,596,350 trainable parameters. Pooling changes activation size; it does not itself add learned weights. Always floor noninteger spatial dimensions.