Getting wrong output size from nn.Unfold for input size ([1, 1, 1, 15])


I am trying to use nn.Unfold to build a customized non-linear (max) convolution over 1-D tensors. This is my code:

import torch
import torch.nn as nn

xx = torch.Tensor([4,5,7,6,4,3,4,2,0,2,4,5,6,7,8])
xx_as4D = xx.view(1,1,1,-1)
unfold = nn.Unfold(kernel_size=(1,4), dilation=(1,1), padding=(1,3), stride=(1,1))
xx_unf = unfold(xx_as4D)

print(f'xx_unf.shape = {xx_unf.shape}')
print(f'xx_as4D.shape = {xx_as4D.shape}')

and the output

xx_unf.shape = torch.Size([1, 4, 54])
xx.shape = torch.Size([15])
xx_as4D.shape = torch.Size([1, 1, 1, 15])

Following the docs, I was expecting that the output of unfold() would be of size [1,4,18], but I am getting the size [1,4,54]. Could some please help me figure out where is my flaw?

In addition, if you have suggestions or references about how to accomplish my goal in an efficient way on Pytorch, those will be welcome! :slight_smile: Thanks.

I found the error! :slight_smile: … the right padding is padding=(0,3).