Convolution Shape and Parameter Calculator

easy
Unit 5 · Losses0/3 to milestone

Shape mismatches are the most common deep learning bug. Implement PyTorch's formulas:

  • conv_out(n, k, stride=1, padding=0, dilation=1): ⌊n+2p−d(k−1)−1s⌋+1\left\lfloor\frac{n + 2p - d(k-1) - 1}{s}\right\rfloor + 1
  • conv_transpose_out(n, k, stride=1, padding=0, dilation=1, output_padding=0): (n−1)s−2p+d(k−1)+output_padding+1(n-1)s - 2p + d(k-1) + \text{output\_padding} + 1
  • same_padding(k, dilation=1): the padding that keeps the size unchanged at stride 1, for odd effective kernel sizes: d(k−1)/2d(k-1)/2
  • conv_params(c_in, c_out, k, groups=1, bias=True): weights cout⋅cingroups⋅k2c_{out} \cdot \frac{c_{in}}{groups} \cdot k^2, plus coutc_{out} biases
  • separable_params(c_in, c_out, k): a depthwise k×k convolution (groups = c_in, with bias) followed by a pointwise 1×1 convolution (with bias)
  • trace_shapes(n, layers): apply a list of (k, stride, padding) conv layers to a size n and return the list of sizes after each layer
#cnns#architectures#shapes2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Convolution Shape and Parameter Calculator · TechGradify