Deep Learning · MT2 · section

Even-sized kernels: SAME padding is not always symmetric

Convolutional neural networks

Definition. A numerical padding value specifies how many entries to add at each boundary. SAME is an output-size convention. At stride 1, SAME preserves height and width; an even-sized kernel can require unequal padding on opposite sides.

For a 2 by 2 kernel at stride 1, preserving the input size requires one padding entry in total per axis, not one on each side. For example, add no row above and one row below, and no column on the left and one on the right. Reversing which side receives the extra entry preserves the output size too, but changes which input cells contribute at each output location; follow the question's convention.

Hinglish cue: P = 1 means dono taraf ek; SAME means output ka size bachao. Dono instructions ko same mat samjho.

Example 1: one entry on every side

Question. A 5 × 5 single-channel input is processed by one 2 × 2 convolution filter. The stride is 1. Exactly one zero is padded on each of the four sides. Find the padded-input size and output size. (Practice: 3 marks)

Given

Input height and width N = 5; kernel size K = 2; stride S = 1; padding P = 1 on each side; dilation = 1; one output filter.

To find

The padded input dimensions and the convolution output dimensions.

Formula — which one and why

The two opposite borders contribute P entries each. The output formula counts complete filter placements after padding.

N_{\mathrm{padded}}=N+2PLaTeX: N_{\mathrm{padded}}=N+2P
N_{\mathrm{out}}=\left\lfloor\frac{N+2P-K}{S}\right\rfloor+1LaTeX: N_{\mathrm{out}}=\left\lfloor\frac{N+2P-K}{S}\right\rfloor+1

N is one input dimension; P is padding on each side; K is kernel size; S is stride. Apply the same calculation separately to height and width. One filter produces one output channel.

Solution — step by step

Step 1: count the two padding borders.

2P=2(1)=2LaTeX: 2P=2(1)=2

Step 2: find the padded side length.

N_{\mathrm{padded}}=5+2=7LaTeX: N_{\mathrm{padded}}=5+2=7

The padded input is 7 × 7 × 1.

Step 3: subtract the kernel size.

7-2=5LaTeX: 7-2=5

Step 4: divide by stride, take the floor and count the first placement.

N_{\mathrm{out}}=\left\lfloor\frac{5}{1}\right\rfloor+1=5+1=6LaTeX: N_{\mathrm{out}}=\left\lfloor\frac{5}{1}\right\rfloor+1=5+1=6

Step 5: write height, width and channels.

Both spatial dimensions equal 6. There is one filter, so there is one output channel.

Final answer: padded input = 7 × 7 × 1; output = 6 × 6 × 1. This explicitly specified padding does not implement SAME for this even kernel.

Example 2: preserve the size with SAME padding

Question. The same 5 × 5 single-channel input is processed by one 2 × 2 convolution filter with stride 1 and SAME padding. Find the total padding required along each axis and the output size. Use zero padding above and to the left, with any required extra padding below and to the right. (Practice: 3 marks)

Given

Input side N = 5; kernel side K = 2; stride S = 1; desired output side Nout = 5; dilation = 1; one filter. Padding before the input is 0; padding after the input must be found.

To find

Padding above, below, left and right; padded-input shape; final output shape.

Formula — which one and why

For unequal padding, replace 2P by the sum of the before and after padding. Pbefore and Pafter are the entries added on the two opposite sides of one axis.

N_{\mathrm{out}}=\left\lfloor\frac{N+P_{\mathrm{before}}+P_{\mathrm{after}}-K}{S}\right\rfloor+1LaTeX: N_{\mathrm{out}}=\left\lfloor\frac{N+P_{\mathrm{before}}+P_{\mathrm{after}}-K}{S}\right\rfloor+1

At stride 1, the quantities are integers. To keep Nout = N, the total padding must equal K − 1.

P_{\mathrm{total}}=P_{\mathrm{before}}+P_{\mathrm{after}}=K-1LaTeX: P_{\mathrm{total}}=P_{\mathrm{before}}+P_{\mathrm{after}}=K-1

Solution — step by step

Step 1: calculate total padding along the height.

P_{\mathrm{total}}=2-1=1LaTeX: P_{\mathrm{total}}=2-1=1

Only one extra row in total is needed.

Step 2: place the row using the stated convention.

Padding above = 0; padding below = 1.

Step 3: apply the same calculation to width.

Only one extra column in total is needed. Padding left = 0; padding right = 1.

Step 4: find the padded-input shape.

H_{\mathrm{padded}}=5+0+1=6LaTeX: H_{\mathrm{padded}}=5+0+1=6
W_{\mathrm{padded}}=5+0+1=6LaTeX: W_{\mathrm{padded}}=5+0+1=6

The padded input is 6 × 6 × 1.

Step 5: count the output positions along each axis.

N_{\mathrm{out}}=\left\lfloor\frac{5+0+1-2}{1}\right\rfloor+1=4+1=5LaTeX: N_{\mathrm{out}}=\left\lfloor\frac{5+0+1-2}{1}\right\rfloor+1=4+1=5

Step 6: include the output channel.

One filter creates one output channel.

Final answer: add 0 above, 1 below, 0 left and 1 right; padded input = 6 × 6 × 1; output = 5 × 5 × 1.

The two questions have the same input and kernel but different padding instructions. Read the stated boundary padding before applying the formula.