Using the fold function to return many separate channels

Can the fold function not sum over each filtered bit of image to return a reconstructed image, but return each filtered bit of image in its own separate channel?
I wrote some code that does this by abusing the batch argument. But, I still have to use a for loop. I want it to work in my training loop so I want it to do the same thing but in parallel. (assume the ones are a placeholder for the filter) If there is no way to do it I will just use this.


z = torch.zeros((16,4,16))
for i in range(16):
    z[i,:,i] = torch.ones((4,)) # our "ones" filter
print(f.fold(z, (5,5,), kernel_size=2).shape)
print(f.fold(z, (5,5,), kernel_size=2))
torch.Size([16, 1, 5, 5])
tensor([[[[1., 1., 0., 0., 0.],
          [1., 1., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.]]],


        [[[0., 1., 1., 0., 0.],
          [0., 1., 1., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.]]],


        [[[0., 0., 1., 1., 0.],
          [0., 0., 1., 1., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.]]],


        [[[0., 0., 0., 1., 1.],
          [0., 0., 0., 1., 1.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.]]],


        [[[0., 0., 0., 0., 0.],
          [1., 1., 0., 0., 0.],
          [1., 1., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.]]],
...etc

ok. i think you can do this with scatter What does the scatter_ function do in layman terms?

I’ll try it in the morning, but if anyone wants to do it feel free :slight_smile:

Not sure exactly what you want to do here. But you can remove the for loop because in a weird way, you set the diagonal of a bunch of matrices to 1. You can do this with: z.diagonal(dim1=0, dim2=-1).fill_(1)

1 Like