3 / 24 · Concept
Shapes, stride, padding and dilation
For H=7,R=3,stride 2,top pad 0,bottom pad 1,dilation 2, calculate Ho and the input coordinate for h=1,r=2.
Lessons are free to read. Enroll to save your learning progress.
Shape calculation drives addressing
Stride controls the separation between output-window origins. Dilation controls separation between samples inside one kernel. They skip input positions in different index terms. A kernel of R samples with dilation D spans D(R−1)+1 positions. Specify top, bottom, left and right padding independently. These exercises use explicit padding and floor-based output sizes, rather than assuming an automatic SAME convention.
Replace height and vertical parameters by their horizontal counterparts for output width. Our teaching model returns zero output positions when the effective kernel exceeds the padded input. A production operator must separately define whether it accepts an empty tensor.
Worked examples
With H=5, R=3, stride 2, top and bottom padding one and dilation one, the effective height is three. Output height is floor((5+2−3)/2)+1=3. Changing dilation to two makes the effective height five and gives output height two. The stride did not change; each window now spans a larger region.
Map an output coordinate and kernel coordinate using ih=h×S−Pt+r×D. For h=1,r=2, the first configuration reads 3. The dilated configuration reads 5. Valid input coordinates are zero through four, so coordinate five supplies padding rather than reading beyond the array. Padding represents real zero; raw quantized storage may encode that zero with a nonzero code.
The NCHW linear address is ((nC+c)H+ih)W+iw. For N=1,C=2,H=W=3, coordinate (0,1,2,1) maps to 16 in an 18-element tensor. It is the middle sample of the last row in the second channel. Swapping channel and spatial axes preserves the element count but changes the computation.
With G groups, both input and output channel counts must divide by G. Each output reduces C/G input channels belonging to its group. Compute output sizes, generate addresses and check every input boundary in that order. The official ONNX Conv specification defines the corresponding explicit padding, stride, dilation and group attributes.
Try it yourself
For H=7,R=3,stride 2,top pad 0,bottom pad 1,dilation 2, calculate Ho and the input coordinate for h=1,r=2.
Read the explanation
Effective kernel height is five. Ho=floor((7+1−5)/2)+1=2. The input coordinate is 1×2+2×2=6, the last valid row.