is unfold a way to do this, but I get a bit different result when I use unfold
for example if I want all (2x3) blocks
import torch.nn as nn
x = torch.randn(1, 1, 5, 5)
x
tensor([[[[-0.0197, 1.0647, -0.2223, 0.6515, 0.4126],
[ 0.2328, 0.3959, -0.2910, -0.3321, -0.7934],
[-0.5428, -0.8354, -0.4103, -0.8781, 0.6794],
[ 0.0659, -1.7829, 0.0720, -0.3183, 2.2014],
[-0.8724, -0.1767, -1.3356, 1.0183, -0.0641]]]])
a = nn.Unfold((2, 3))
a(x)
it give me something like this,
tensor([[[-0.0197, 1.0647, -0.2223, 0.2328, 0.3959, -0.2910, -0.5428,
-0.8354, -0.4103, 0.0659, -1.7829, 0.0720],
[ 1.0647, -0.2223, 0.6515, 0.3959, -0.2910, -0.3321, -0.8354,
-0.4103, -0.8781, -1.7829, 0.0720, -0.3183],
[-0.2223, 0.6515, 0.4126, -0.2910, -0.3321, -0.7934, -0.4103,
-0.8781, 0.6794, 0.0720, -0.3183, 2.2014],
[ 0.2328, 0.3959, -0.2910, -0.5428, -0.8354, -0.4103, 0.0659,
-1.7829, 0.0720, -0.8724, -0.1767, -1.3356],
[ 0.3959, -0.2910, -0.3321, -0.8354, -0.4103, -0.8781, -1.7829,
0.0720, -0.3183, -0.1767, -1.3356, 1.0183],
[-0.2910, -0.3321, -0.7934, -0.4103, -0.8781, 0.6794, 0.0720,
-0.3183, 2.2014, -1.3356, 1.0183, -0.0641]]])
but this is not what I want,
how should I change this, to get 2x3 size blocks from input?
one way that I found, is to reshape this tensor,
a(x).reshape(1, 12, 6)
which gives
tensor([[[-0.0197, 1.0647, -0.2223, 0.2328, 0.3959, -0.2910],
[-0.5428, -0.8354, -0.4103, 0.0659, -1.7829, 0.0720],
[ 1.0647, -0.2223, 0.6515, 0.3959, -0.2910, -0.3321],
[-0.8354, -0.4103, -0.8781, -1.7829, 0.0720, -0.3183],
[-0.2223, 0.6515, 0.4126, -0.2910, -0.3321, -0.7934],
[-0.4103, -0.8781, 0.6794, 0.0720, -0.3183, 2.2014],
[ 0.2328, 0.3959, -0.2910, -0.5428, -0.8354, -0.4103],
[ 0.0659, -1.7829, 0.0720, -0.8724, -0.1767, -1.3356],
[ 0.3959, -0.2910, -0.3321, -0.8354, -0.4103, -0.8781],
[-1.7829, 0.0720, -0.3183, -0.1767, -1.3356, 1.0183],
[-0.2910, -0.3321, -0.7934, -0.4103, -0.8781, 0.6794],
[ 0.0720, -0.3183, 2.2014, -1.3356, 1.0183, -0.0641]]])
I think so this works, but when I add channel to input, then this fails.
edit:
I found one way,
x.unfold(2, 2, 1).unfold(3, 3, 1)
2 sized slices for each row, and 3 sized slices for each column
if I wanted (4, 4) slices
then,
x.unfold(2, 4, 1).unfold(3, 4, 1)