Padding in Pytorch
In PyTorch tensor, I would like to obtain the output from the input as follows:



How can I achieve this padding in Pytroch?

One way of doing this is

def my_odd_padding(list_of_2d_tensors, pad_value):
  # get the sizes of the matrices
  hs = [t_.shape[0] for t_ in list_of_2d_tensors]
  ws = [t_.shape[1] for t_ in list_of_2d_tensors]
  # allocate space for output
  result = torch.zeros(sum(hs), sum(ws))
  result.add_(pad_value)
  fh = 0
  fw = 0
  for i, t_ in enumerate(list_of_2d_tensors):
    result[fh:fh+hs[i], fw:fw+ws[i]] = t_
    fh += hs[i]
    fw += ws[i]
  return result 


assuming all tensors on list_of_2d_tensors are of the same dtype and on the same device you may explicitly set this dtype and device to result when you allocate it using torch.zeros
