title: Is there really no padding=same option for PyTorch's Conv2d?
body: <p>I'm currently working on building a convolutional neural network (CNN) that will work on time series data.</p>
  <p>More specifically, the data is financial data of shape <code>(100, 40)</code>, with <code>100</code> meaning 100 time stamps and <code>40</code> meaning 40 features.</p>
  <p>The CNN that I'm using uses asymmetric kernel sizes (i.e. <code>1 x 2</code> and <code>4 x 1</code>) and also asymmetric strides (i.e. <code>1 x 2</code> for the <code>1 x 2</code> layers and <code>1 x 1</code> for the <code>4 x 1</code> layers).</p>
  <p>In order to maintain the height dimension to stay <code>100</code>, I needed to apply some padding to the data. I was looking into ways to do this and noticed that people who use TensorFlow or Keras simply do <code>padding='same'</code>, but this option is apparently unavailable in PyTorch according to many resources I've found including <a href="https://github.com/pytorch/pytorch/issues/3867" rel="noreferrer">this thread on Github</a>.</p>
  <p>What I've found is that according to <a href="https://stackoverflow.com/questions/37674306/what-is-the-difference-between-same-and-valid-padding-in-tf-nn-max-pool-of-t">some answers in this question</a> and also <a href="https://discuss.pytorch.org/t/is-asymmetric-padding-of-style-same-available-in-pytorch/4354/7" rel="noreferrer">this answer on the PyTorch discussion forum</a>, I can manually calculate how I need to pad my data and can use <a href="https://pytorch.org/docs/stable/nn.html#zeropad2d" rel="noreferrer"><code>torch.nn.ZeroPad2d</code></a> to solve my problem, as it seems that normal <a href="https://pytorch.org/docs/stable/nn.html#conv2d" rel="noreferrer"><code>torch.nn.Conv2d</code></a> layers don't support asymmetric padding (I believe that the total padding I need is 3 in height and 0 in width).</p>
  <p>The experimental code that I've written to test this out is like this:</p>
  <pre><code>import torch
  import torch.nn as nn
  
  conv = nn.Conv2d(1, 1, kernel_size=(4, 1))
  pad = nn.ZeroPad2d((0, 0, 2, 1)) # Add 2 to top and 1 to bottom.
  
  x = torch.randint(low=0, high=9, size=(100, 40))
  x = x.unsqueeze(0).unsqueeze(0)
  
  y = pad(x)
  
  x.shape # (1, 1, 100, 40)
  y.shape # (1, 1, 103, 40)
  
  print(conv(x.float()).shape)
  print(conv(y.float()).shape)
  
  # Output
  # x -&gt; (1, 1, 97, 40)
  # y -&gt; (1, 1, 100, 40)
  </code></pre>
  <p>As you can see, it does work in the sense that the size of dimensions is remaining the same. However, I've been wondering is there really no <code>padding='same'</code> option out there? Also, how do we know whether to apply the padding 2 to the top or to the bottom?</p>
  <p>Thank you.</p>
  <hr />
  <h3>Edit</h3>
  <p>This is a little late, but in case anyone's curious how I solved this issue I basically manually added padding in order to emulate the <code>padding=same</code> option.</p>