How to pad one side in pytorch

username: twangnh
cooked: <p>By default pytorch only support padding on both side, but for example, I have a feature of 1x512x37x56(NCHW) and I want to pad on one side to 1x512x38x57, how can I do it?</p>

username: 0phoff
cooked: <p><strong>EDIT</strong><br>
Hmm, seems they added a lot of padding classes since I looked last time and this is not needed anymore…<br>
<a href="https://pytorch.org/docs/stable/nn.html#padding-layers" rel="nofollow noopener">This page</a> has all the padding you might want <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<hr>
<p>Padding, whilst copying the values of the tensor is doable with the <em>Functional</em> interface of PyTorch.<br>
You can read more about the different padding modes <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.functional.pad" rel="nofollow noopener">here</a>.</p>
<pre><code class="lang-python">import torch.nn.functional as F

# Pad last 2 dimensions of tensor with (0, 1) -&gt; Adds extra column/row to the right and bottom, whilst copying the values of the current last column/row
padded_tensor = F.pad(input_tensor, (0,1,0,1), mode='replicate')
</code></pre>
<p>You can wrap this functional interface in a module:</p>
<pre><code class="lang-python">import torch
import torch.nn.functional as F

class CustomPad(torch.nn.module):
  def __init__(self, padding):
    self.padding = padding
  def forward(self, x):
    return F.pad(x. self.padding, mode='replicate')
</code></pre>

username: twangnh
cooked: <p>Thank you! I also found nn.ConstantPad2d can do the job</p>

username: Arjun100
cooked: <aside class="quote no-group" data-username="twangnh" data-post="1" data-topic="21212">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/49beb7/48.png" class="avatar"> twangnh:</div>
<blockquote>
<p>1x512x37x56(NCHW) and I want to pad on one side to 1x512x38x57,</p>
</blockquote>
</aside>
<p>Here is an example how I have used padding! Please check on forward() method.</p>
<pre><code class="lang-auto">class Up (nn.Module):
    def __init__(self, in_ch, out_ch, bilinear= True):
        super(self, Up).__init__()
# bilinear is the upsample algorithm given torch.nn.Up algo.
#check more here: https://s0pytorch0org.icopy.site/docs/0.4.0/_modules/torch/nn/modules/upsampling.html
        if bilinear:
            #align_corners (bool, optional): if True, the corner pixels of the input
            #and output tensors are aligned, and thus preserving the values at
            #those pixels. This only has effect when :attr:`mode` is `linear`,
            #`bilinear`, or `trilinear`. Default: False
            self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)

            # Bilinear vs ConvTrabspose2d
            #ConvTranspose is a convolution and has trainable kernels 
            # while Upsample is a simple interpolation (bilinear, nearest etc.)
        else:
            self.up = nn.ConvTranspose2d(in_ch//2, in_ch//2, 2, stride=2)
        
        def forward(self, x1, x2= None):
            x1 = self.up(x1)

            # input in channel height and width

            
            #x2 tensor shape = [batch, channel, H, W]
            diffy = x2.size()[2] - x1.size()[2]
            diffx = x2.size()[3] -x1.size()[3]

            # add padding --PADDING ADDED HERE 
            x1 = F.pad(x1, (diffX // 2, diffX - diffX//2,
                        diffY // 2, diffY - diffY//2))
    
        
            if x2 is not None:
                x = torch.cat([x2, x1], dim=1)
            else:
                x = x1
            x = self.conv(x)
            return x

</code></pre>