Convolution Output Dimensions
Problem statement
Calculate the spatial output dimensions of a two-dimensional convolution layer. The integer arrays inputShape, kernelSize, stride, padding, and dilation each contain two entries in [height, width] order.
For each axis:
padding[axis]positions are added on each side of the input axis.- A kernel has
kernelSize[axis]taps. The distance from one tap to the next isdilation[axis], so tapjlies atanchor + j * dilation[axis]in the padded axis. - The first anchor is
0in the padded axis, and subsequent anchors advance bystride[axis]. - Count a placement only when every kernel tap lies inside the padded axis. Discard an incomplete trailing placement.
Return [outputHeight, outputWidth]. Batch size, channel counts, and tensor values are outside this exercise. Each input is valid and allows at least one complete placement on both axes.
What the interview report shared
The report describes a simple calculation of a convolution layer's output size during a technical phone interview. It does not give the tensor dimensions, kernel parameters, padding, dilation, or rounding rule.
Function
convolutionOutputShape(inputShape: int[], kernelSize: int[], stride: int[], padding: int[], dilation: int[]) → int[]Examples
Example 1
inputShape = [8,9]kernelSize = [3,3]stride = [2,2]padding = [1,1]dilation = [1,1]return = [4,5]The padded height is 10. Valid height anchors are 0, 2, 4, 6; anchor 8 would place the final tap at 10, outside the padded axis. The padded width is 11, with valid anchors 0, 2, 4, 6, 8.
Example 2
inputShape = [7,7]kernelSize = [3,3]stride = [1,1]padding = [0,0]dilation = [2,2]return = [3,3]Each kernel uses offsets 0, 2, 4, spanning 5 positions. Anchors 0, 1, 2 fit on each length-7 axis.
Example 3
inputShape = [3,5]kernelSize = [3,5]stride = [5,7]padding = [0,0]dilation = [1,1]return = [1,1]The kernel exactly covers the input. Anchor 0 fits on each axis, and the next stride would extend past the edge. A stride larger than the axis still allows the initial placement.
Constraints
- All five input arrays have length
2. 1 <= inputShape[axis] <= 1000.1 <= kernelSize[axis], stride[axis], dilation[axis] <= 100.0 <= padding[axis] <= 1000.- For both axes,
(kernelSize[axis] - 1) * dilation[axis] + 1 <= inputShape[axis] + 2 * padding[axis].