title: Understanding the PyTorch implementation of Conv2DTranspose
body: <p>I am trying to understand an example snippet that makes use of the PyTorch transposed convolution function, with documentation <a href="https://pytorch.org/docs/stable/generated/torch.nn.ConvTranspose2d.html" rel="nofollow noreferrer">here</a>, where in the docs the author writes:</p>
  <blockquote>
  <p>&quot;The padding argument effectively adds dilation * (kernel_size - 1) -
  padding amount of zero padding to both sizes of the input.&quot;</p>
  </blockquote>
  <p>Consider the snippet below where a <code>[1, 1, 4, 4]</code> sample image of all ones is input to a <code>ConvTranspose2D</code> operation with arguments <code>stride=2</code> and <code>padding=1</code> with a weight matrix of shape <code>(1, 1, 4, 4)</code> that has entries from a range between <code>1</code> and <code>16</code> (in this case <code>dilation=1</code> and <code>added_padding = 1*(4-1)-1 = 2</code>)</p>
  <pre><code>sample_im = torch.ones(1, 1, 4, 4).cuda()
  sample_deconv2 = nn.ConvTranspose2d(1, 1, 4, 2, 1, bias=False).cuda()
  sample_deconv2.weight = torch.nn.Parameter(
      torch.tensor([[[[ 1.,  2.,  3.,  4.], 
                      [ 5.,  6.,  7.,  8.], 
                      [ 9., 10., 11., 12.], 
                      [13., 14., 15., 16.]]]]).cuda())
  </code></pre>
  <p>Which yields:</p>
  <pre><code>&gt;&gt;&gt; sample_deconv2(sample_im)
  tensor([[[[ 6., 12., 14., 12., 14., 12., 14.,  7.],
            [12., 24., 28., 24., 28., 24., 28., 14.],
            [20., 40., 44., 40., 44., 40., 44., 22.],
            [12., 24., 28., 24., 28., 24., 28., 14.],
            [20., 40., 44., 40., 44., 40., 44., 22.],
            [12., 24., 28., 24., 28., 24., 28., 14.],
            [20., 40., 44., 40., 44., 40., 44., 22.],
            [10., 20., 22., 20., 22., 20., 22., 11.]]]], device='cuda:0',
         grad_fn=&lt;CudnnConvolutionTransposeBackward&gt;)
  </code></pre>
  <p>Now I have seen simple examples of transposed convolution without stride and padding. For instance, if the input is a <code>2x2</code> image  <code>[[2, 4], [0, 1]]</code>, and the convolutional filter with one output channel is <code>[[3, 1], [1, 5]]</code>, then the resulting tensor of shape <code>(1, 1, 3, 3)</code> can be seen as the sum of the rightmost four matrices in the image below:</p>
  <p><a href="https://i.stack.imgur.com/vSZgS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vSZgS.png" alt="enter image description here" /></a></p>
  <p>The problem is I can't seem to find examples that use strides and/or padding in the same visualization. As per my snippet, I am having a very difficult time understanding how the padding is applied to the sample image, or how the stride works to get this output. Any insights appreciated, even just understanding how the <code>6</code> in the <code>(0,0)</code> entry or the <code>12</code> in the <code>(0,1)</code> entry of the resulting matrix are computed would be very helpful.</p>