How to pad one side in pytorch

EDIT
Hmm, seems they added a lot of padding classes since I looked last time and this is not needed anymore…
This page has all the padding you might want :slight_smile:


Padding, whilst copying the values of the tensor is doable with the Functional interface of PyTorch.
You can read more about the different padding modes here.

import torch.nn.functional as F

# Pad last 2 dimensions of tensor with (0, 1) -> 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')

You can wrap this functional interface in a module:

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')
3 Likes