Deep Learning · TEE · section

Stride padding and floor

Convolutional neural networks

Definition. Stride is the spacing between consecutive kernel positions. Padding extends the input boundary. Together with kernel size and dilation, they determine the spatial dimensions of a convolutional output.

Explanation and working

Stride is how far the filter moves each time. Stride 2 visits starting positions 0, 2, 4 and so on. Padding adds border values, usually zeros. Padding 1 means one new row or column on each side, adding 2 to a spatial dimension.

The border adds to both sides of the input. The stride changes the next starting position of the filter window. Tap the diagram to enlarge.
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 input size; P is padding per side; K is kernel size; S is stride. Floor means take the greatest integer not above the result: \left\lfloor 2.5\right\rfloorLaTeX: \left\lfloor 2.5\right\rfloor = 2. It counts only positions where the entire window fits. The formula assumes dilation 1, the standard setting in these papers. Apply it separately to height and width for rectangular images or kernels.

Why plus one? Even when the kernel exactly fits the image, there is one placement. If N = K, P = 0, S = 1, the formula is 0 + 1 = 1.

Output dimension with a fractional intermediate result

Question. A convolution has input side N = 6, kernel side K = 3, stride S = 2 and padding P = 0. Find its output side.

Given

N = 6; K = 3; S = 2; P = 0; dilation = 1.

To find

The integer output side length.

Formula — which one and why

The formula counts complete kernel placements. Flooring removes an incomplete final placement.

N is input side; P is padding on each side; K is kernel side; S is stride.

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

Yaad rakhne ka cue: Padding dono side; stride se divide; floor; phir +1.

Solution — step by step

Step 1: include padding and subtract kernel size.

6+2(0)-3=3LaTeX: 6+2(0)-3=3

Step 2: divide by the stride.

\frac32=1.5LaTeX: \frac32=1.5

Step 3: take the floor.

\lfloor1.5\rfloor=1LaTeX: \lfloor1.5\rfloor=1

Step 4: count the first placement.

N_{\mathrm{out}}=1+1=2LaTeX: N_{\mathrm{out}}=1+1=2

Final answer: Output side = 2; a square input gives a 2 × 2 map per filter.

Padding on both sides

Question. A square convolution input has side 5, kernel side 3, stride 1 and padding 1 on each side. Find the padded-input size and output size.

Given

N = 5; K = 3; S = 1; P = 1 on each side.

To find

Padded input and output dimensions.

Formula — which one and why

Padding adds P entries on each of two opposite sides; the output counts complete kernel placements.

N = unpadded side; K = kernel side; S = stride; P = per-side padding.

N_{\mathrm{padded}}=N+2PLaTeX: N_{\mathrm{padded}}=N+2P
N_{\mathrm{out}}=\lfloor(N+2P-K)/S\rfloor+1LaTeX: N_{\mathrm{out}}=\lfloor(N+2P-K)/S\rfloor+1

Yaad rakhne ka cue: Padded input aur output ka size alag cheez hain.

Solution — step by step

Step 1: count both padding edges.

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

Step 2: calculate the padded input side.

N+2P=5+2=7LaTeX: N+2P=5+2=7

Step 3: subtract kernel width.

7-3=4LaTeX: 7-3=4

Step 4: divide by stride and take the floor.

\lfloor4/1\rfloor=4LaTeX: \lfloor4/1\rfloor=4

Step 5: include the first placement.

N_{out}=4+1=5LaTeX: N_{out}=4+1=5

Final answer: Padded input = 7 × 7; output = 5 × 5 per filter.

“Valid” usually means P = 0. For odd K and S = 1, padding (K − 1)/2 preserves the spatial size. For S greater than 1, software “same” padding can be asymmetric and commonly gives ceiling(N/S); do not force the stride-1 rule into that case.