Pooling and fully connected layers
Notes on pooling layers (max/average), their forward and backward rules, and fully connected (dense) layers: definitions, output-size and parameter formulas, worked examples, common exam mistakes and short questions.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Pooling layers and fully connected layers appear in almost every Convolutional Neural Network (CNN) design. An engineer uses pooling to reduce spatial size, compute cheaper feature maps and gain some translation invariance; an engineer uses fully connected (dense) layers to combine features into final predictions (classification, regression) or logits for softmax.
Key ideas
Pooling layer: a layer that reduces each feature map's spatial dimensions by applying a local aggregation function (commonly max or average) over non-overlapping or overlapping windows.
Max pooling: pooling that outputs the maximum value inside the window.
Average pooling: pooling that outputs the arithmetic mean inside the window.
Stride (S): the number of pixels the pooling window moves each step.
Window (filter) size (F): the height and width of the pooling window (often square); e.g., F=2 means a 2×2 window.
Padding (P): adding zeros (or other values) around the border before pooling; usually P=0 for pooling but frameworks can support 'same' pooling.
Receptive field: the region of the input image that affects a given activation later in the network.
Fully connected (dense) layer: a layer where every output unit is a linear combination of all input units followed by an optional activation; represented by a weight matrix W and bias vector b.
Output size formula (common university exam derivation): assume input height H, width W, padding P, filter/window size F, stride S. The output height H_out and width W_out are:
- H_out = floor((H - F + 2·P) / S) + 1
- W_out = floor((W - F + 2·P) / S) + 1 Provide the proof by counting how many positions a window can take while staying within padded input boundaries: start position 0, last start at index H - F + 2P, step size S; number of starts = floor((H - F + 2P)/S) + 1. For 'same' padding with stride S, many frameworks define H_out = ceil(H/S) and W_out = ceil(W/S).
Pooling forward:
- For max pooling, each output element equals the maximum of the values in the window from the corresponding input channel. It is non-linear but parameter-free.
- For average pooling, each output equals the mean of window values.
- Pooling is applied independently per channel (no cross-channel mixing).
Pooling backward (gradient) rules (exam-style derivation): assume upstream gradient dL/dY for an output Y of a pooling layer. We want dL/dX for input X.
- Max pooling backward: only the input element(s) that attained the maximum in the forward pass receive the upstream gradient; others get zero. If there are ties, the gradient can be split among the maxima or assigned arbitrarily, but consistent frameworks usually pick one maximal index.
- Average pooling backward: upstream gradient is divided equally among all elements in the pooling window. If window size is F×F, each input in that window gets (1/(F·F))·(dL/dY).
- Derive by writing Y = max(X_i) or Y = (1/n)·sum X_i and applying chain rule dL/dX_i = dL/dY · dY/dX_i. If pooling windows overlap (S < F), an input may be covered by multiple windows; gradients from all covering windows sum at that input.
Fully connected forward and backward:
- Flatten the feature maps into a vector x of length N_in = H·W·C (height, width, channels) unless global pooling is used. Global average pooling replaces flattening by averaging each channel over H×W to produce a length-C vector.
- For an FC layer with N_out outputs, compute y = W·x + b where W is N_out × N_in and b is N_out. Activation functions then apply element-wise.
- Backprop: dL/dx = W^T · dL/dy, dL/dW = dL/dy · x^T (outer product summed over batch), dL/db = dL/dy summed over batch.
Parameters and compute cost:
- Number of parameters in FC = N_in × N_out + N_out (biases).
- Pooling has no learnable parameters (unless using learned pooling, e.g., gated pooling — outside scope). This reduces overfitting risk and computation.
Diagrams to draw in exam answers (describe exactly):
- For a 4×4 single-channel input and 2×2 max-pooling with stride 2: draw the 4×4 square with entries labelled (row, col) from (0,0) to (3,3). Draw 2×2 windows at top-left covering (0,0)-(1,1), top-right (0,2)-(1,3), bottom-left (2,0)-(3,1), bottom-right (2,2)-(3,3). Label outputs as a 2×2 grid. Indicate which input element is selected for each output (use arrows).
- For a fully connected layer: draw input tensor 3×3×2, show flattening to a vector of size 18, draw weight matrix W of size N_out × 18, show matrix multiplication producing output vector of size N_out, and add bias b.
Assumptions used: input values are finite reals, pooling windows fit within padded boundaries (so the output-size formula gives integer counts), and activations are treated per sample (batch dimension omitted in single-sample derivations).
Formulas
H_out = floor((H - F + 2·P) / S) + 1 — H_out: output height (pixels), H: input height (pixels), F: filter/window size (pixels), P: padding (pixels), S: stride (pixels). Applies when pooling or convolution windows move by S.
W_out = floor((W - F + 2·P) / S) + 1 — W_out: output width (pixels); symbols as above.
N_in = H · W · C — N_in: number of inputs to a fully connected layer (dimensionless count), H,W in pixels, C number of channels (dimensionless).
Parameters_FC = N_in · N_out + N_out — number of scalar parameters in a fully connected layer (weights plus biases), unit: count.
y = W · x + b — W: weight matrix of shape (N_out × N_in), x: input column vector (N_in × 1), b: bias vector (N_out × 1), y: output vector (N_out × 1). Units: dimensionless values (e.g., activation units).
dL/dx = W^T · dL/dy — backprop through FC, shapes consistent with above, units same as activations.
For average pooling: dL/dx_i = (1/(F·F)) · dL/dy for each x_i in the pooling window. Units: same as activations.
For max pooling: dL/dx_i = dL/dy if x_i was the argmax in the forward pass, else 0. Units: same as activations.
Worked examples
Compute output size after pooling (numerical). Given: input feature map size H=32 px, W=32 px, channels C=16, pooling window F=2, stride S=2, padding P=0. Formula: H_out = floor((32 - 2 + 0)/2) + 1 = floor(30/2) + 1 = 15 + 1 = 16. W_out = same => 16. Output tensor size = 16 × 16 × 16. Final answer: 16 × 16 × 16 (height × width × channels).
Parameters in a fully connected layer (numerical). Given: previous layer produces 8 × 8 × 32 feature maps. FC layer has N_out = 1000 units. Compute N_in = 8 · 8 · 32 = 64 · 32 = 2048. Parameters_FC = 2048 · 1000 + 1000 = 2,048,000 + 1,000 = 2,049,000. Final answer: 2,049,000 parameters.
Backprop through a 2×2 max pooling (small matrix example). Forward input (single channel): X = [[1.0, 2.0], [4.0, 3.0]] Pooling F=2, S=2 yields one output Y = max(1.0,2.0,4.0,3.0) = 4.0. Suppose upstream gradient dL/dY = 5.0. Backprop rule: gradient routes to the argmax element (4.0 at position (1,0)). So dL/dX = [[0, 0], [5.0, 0]]. Final answer: dL/dX = [[0, 0], [5.0, 0]].
Common mistakes
- Counting biases twice or forgetting them when computing parameters of FC layers.
- Assuming pooling has learnable parameters; standard max/average pooling do not.
- In backprop for max pooling, distributing gradient to all inputs instead of only the max position.
- Flattening incorrectly (mixing order of H, W, C); always state and keep consistent the flattening order (commonly row-major: vary width fastest, then height, then channels — but frameworks differ; specify convention in your answer).
- Ignoring padding when applying the output-size formula.
For your exam
Typical question forms and what full-marks answers include:
- Definitions: define pooling, max pooling, average pooling, fully connected layer. Include brief statement of purpose (dimensionality reduction, translation invariance, classification head).
- Short notes: behaviour of pooling in forward/backward pass, parameter count of FC layers; include formulas and one small numeric example.
- Derivations: derive output-size formula for pooling/convolution (show counting argument), derive dL/dx for average pooling from chain rule, and for max pooling explain argmax routing.
- Numericals: compute output shape after pooling / compute parameters in FC; show each arithmetic step and units (pixels, channels, counts).
- Diagrams: draw sliding windows and indicate pooling mappings; label indices and arrows showing gradient flow for max pooling.
- Programs: rarely asked for pure pooling/FC code in exams, but you may be asked to show pseudo-code for forward/backward loops (show indexing clearly).
A full-marks answer always states assumptions (e.g., padding used, stride, window size), shows substitution into formulas, and labels diagrams precisely.
Quick check
- For input 28×28×8, pooling F=3, S=2, P=0, what is output height and width?
- If a fully connected layer has 500 inputs and 10 outputs, how many weights and biases are there?
- In a 2×2 average pooling with upstream gradient 8.0, what gradient does each input in the window receive?
Answers: 1) H_out = floor((28-3)/2)+1 = floor(25/2)+1 = 12+1 = 13, so 13×13; 2) weights = 500·10 = 5,000; biases = 10; total = 5,010; 3) Each input gets 8.0 / (2·2) = 2.0.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?