Write a function returning the receptive field of a conv stack from kernels and strides.
Given a list of layers described by kernel size and stride, compute the receptive field of one output unit with respect to the network input.
Constraints:
- layers are ordered input → output; each entry is a (kernel, stride) pair
- no dilation, no branching, no padding effects
- return a single integer, in input pixels
def receptive_field(layers):
"""layers: [(kernel, stride), ...] ordered input -> output. Returns an int."""
...
Complete the implementation.
Walk the layers front to back keeping a running jump, the product of the strides so far. Start with rf = 1 and jump = 1; for each layer do rf += (k − 1) × jump, then jump ×= s. Each layer adds k − 1 pixels scaled by the preceding downsampling.
- ✗Ignoring stride and simply multiplying the kernel sizes
- ✗Adding k instead of k − 1 for each layer
- ✗Applying the current layer's stride before adding its kernel contribution
- →How does the formula change when a layer uses dilation?
- →Why is the running jump the product of all preceding strides?
The key is a running jump, the product of the strides of all layers passed so far. A layer's contribution is measured in input pixels, so it must be scaled by the downsampling accumulated BEFORE it.
def receptive_field(layers):
"""layers: [(kernel, stride), ...] ordered input -> output. Returns an int."""
rf, jump = 1, 1
for kernel, stride in layers:
rf += (kernel - 1) * jump
jump *= stride
return rf
Order matters: add the kernel contribution first, then update jump. Three 3×3 convolutions with stride 1 give 1 + 2 + 2 + 2 = 7. Make the second layer stride 2 and the third now adds 2 × 2 = 4, so the field grows to 9.