Why is `nn.Unfold` not giving me the same results as `.unfold`
Why is this giving me two completely different answers? And how can I get the same result as in approach 1, using approach 2?
import torch
from torch import nn

kernel_size = 7
stride = 1

# approach 1
data = torch.rand(4, 64, 174, 120)
data1 = data.unfold(3, kernel_size * 2 + 1, stride)
print(data1.shape)

# approach 2
data = torch.rand(4, 64, 174, 120)
unfold = nn.Unfold(3, kernel_size * 2 + 1, stride)
data2 = unfold(data)
print(data2.shape)

Output:
torch.Size([4, 64, 174, 106, 15])
torch.Size([4, 576, 13432])

EDIT ------------------------------------------
I tried your approach @Shai. The shapes are the same, but the content is not. Any idea why?
import torch
from torch import nn

kernel_size = 7
stride = 1

# approach 1
data = torch.rand(4, 64, 174, 120)
data1 = data.unfold(3, kernel_size * 2 + 1, stride)
print(data1.shape)

# approach 2
data = torch.rand(4, 64, 174, 120)
b, c, h, w = data.shape
unfold = nn.Unfold(kernel_size=(1, 2*kernel_size + 1), dilation=1, stride=1, padding=0)
data2 = unfold(data.reshape(-1, 1, 1, w)).permute(0, 2, 1).reshape(b, c, h, -1, 2*kernel_size + 1)
print(data2.shape)

print(torch.equal(data1, data2))

Output:
torch.Size([4, 64, 174, 106, 15])
torch.Size([4, 64, 174, 106, 15])
False


torch.unfold &quot;unfolds&quot; along a certain dimension. In your example it takes 4x64x174 samples of dim 120 and extract all overlapping 15-windows, resulting with data1 of shape 4x64x174x106x15.
In contrast, nn.Unfold works on bxcx... tensors and extract spatial patches. In your example, nn.Unfold got kernel_size=3, dilation=kernel_size*2+1 and padding=1. Hence, it extracted 13,432 3x3 patches of 64 channels (3364=576), resulting with data2 of shape 4x576x13432.
To get the same output of torch.unfold from nn.Unfold you need to reshape and permute:
b, c, h, w = data.shape
unfold = nn.Unfold(kernel_size=(1, 2*kernel_size + 1), dilation=1, stride=1, padding=0)
data2 = unfold(data.reshape(-1, 1, 1, w)).permute(0, 2, 1).reshape(b, c, h, -1, 2*kernel_size + 1)

Please read carefully the doc of nn.Unfold as it works in a fundamentally different manner to torch.unfold. For more information about nn.Unfold and nn.Fold please see this thread.
