def comp_conv2d(conv2d, X):
"""Apply a convolutional layer to a single-channel 2D input."""
Y = conv2d(X.reshape((1, 1) + X.shape)) # input (1, 1, h, w)
return Y.reshape(Y.shape[2:])Dive into Deep Learning · §6.3
Padding, Stride, and Dilation
boundary control · spatial downsampling · dilated footprints · receptive-field growth
Motivation & Spatial Trade-Offs
Boundary Pixel Utilization
Even in a single unpadded k \times k layer, pixels near the edge of the frame participate in far fewer sliding windows than interior pixels:
Symmetric Per-Side Padding
Surrounding the input with p_\textrm{h} rows of zeros on both top and bottom and p_\textrm{w} columns on both left and right expands the effective input grid to (n_\textrm{h} + 2p_\textrm{h}) \times (n_\textrm{w} + 2p_\textrm{w}):
\textrm{Output shape} = (n_\textrm{h} + 2p_\textrm{h} - k_\textrm{h} + 1) \times (n_\textrm{w} + 2p_\textrm{w} - k_\textrm{w} + 1)
Grid Alignment Under Symmetric Padding
Batch-and-Channel Wrapper Helper
Built-in 2D convolutional layers expect 4D tensors carrying batch and channel axes. Our helper comp_conv2d wraps a 2D matrix (H, W) with singleton batch/channel dimensions and returns the 2D spatial output:
torch.Size([8, 8])
Implicit Positional Leakage vs. Reflect/Replicate Padding
tensor([[4., 6., 6., 6., 4.],
[6., 9., 9., 9., 6.],
[6., 9., 9., 9., 6.],
[6., 9., 9., 9., 6.],
[4., 6., 6., 6., 4.]])
Stepping the Sliding Window by s > 1 Pixels
Instead of evaluating the kernel at every pixel (s = 1), a vertical stride s_\textrm{h} and horizontal stride s_\textrm{w} shift the window by s_\textrm{h} rows and s_\textrm{w} columns per step:
Counting Valid Steps Plus the Starting Position
h_\textrm{o} = \left\lfloor \frac{n_\textrm{h} + 2p_\textrm{h} - k_\textrm{h}}{s_\textrm{h}} \right\rfloor + 1
Three Canonical Padding-and-Stride Regimes
How Early Strides Multiply All Downstream Kernels
r = 1 + \sum_{i=1}^{L} (k_i - 1) \prod_{j=1}^{i-1} s_j
Spacing Kernel Taps Apart by Dilation Rate d
When a task demands a wide receptive field without losing spatial resolution (e.g., per-pixel semantic segmentation), dilation inserts spaces of d - 1 pixels between adjacent kernel taps:
Substituting Effective Kernel Size k’ = d(k - 1) + 1
Replacing k with k' = d(k - 1) + 1 in the strided output-shape formula yields one master equation governing every convolutional layer along an axis:
n_\textrm{out} = \left\lfloor \frac{n + 2p - d(k - 1) - 1}{s} \right\rfloor + 1
(torch.Size([4, 4]), torch.Size([4, 4]))
Comparing Strides (2, 2, 1) Against Dilations (1, 2, 4)
Replacing k_i - 1 with k'_i - 1 = d_i(k_i - 1) in our receptive-field formula shows that doubling dilation rates d_i = 2^{i-1} at stride 1 grows the receptive field exponentially (r = 2^{L+1} - 1 for 3 \times 3 kernels):
Why Constant Dilation Schedules Create Blind Lattices
Concept Check
Test your mastery of the unified shape and receptive-field equations:
Answers: (1) Each unpadded 5 \times 5 layer subtracts 4 pixels: 28 \to 24 \to 20 \to 16 (16 \times 16). Padding p = (5-1)/2 = 2 on each side keeps 28 \times 28. (2) Layer 1 output: \lfloor(224+6-7)/2\rfloor + 1 = 112 \times 112. Layer 2 output: \lfloor(112+2-3)/2\rfloor + 1 = 56 \times 56. Receptive field after Layer 2: r = 1 + (7-1)(1) + (3-1)(2) = 1 + 6 + 4 = 11 pixels. (3) The effective kernel size is k' = 4(3-1)+1 = 9, so shape-preserving padding requires p = (k'-1)/2 = 4 pixels per side.
Section Synthesis
| Hyperparameter | Effect on Output Size n_\textrm{out} | Effect on Receptive Field r | Systems & Architectural Role |
|---|---|---|---|
| Padding (p per side) | Adds 2p to effective input length | None (r is independent of p) | Prevents border shrinkage; p = (k-1)/2 preserves H \times W at s=1 |
| Stride (s) | Divides output length by s (\approx \lceil n/s \rceil) | Multiplies reach of all subsequent layers by s | Cuts activations & MACs by s^2; grows RF geometrically |
| Dilation (d) | Expands kernel k \to k' = d(k-1)+1 | Adds d(k-1) \prod_{j<i} s_j to r | Exponential RF growth at full resolution (0 extra params) |
| Master Formula | \lfloor (n + 2p - d(k-1) - 1)/s \rfloor + 1 | 1 + \sum_{i=1}^L d_i(k_i - 1)\prod_{j<i} s_j | Governs all 1D, 2D, and 3D convolutional & pooling layers |
Next (§6.4): Extending convolutions from single-channel 2D grids to 3D stacks of input and output channels, accounting for the c_\textrm{i} c_\textrm{o} compute/memory cost, and building 1 \times 1 bottleneck layers.