How to implement the following operations effectively in pytorch ?

Given a M×N×C matrix, I want to calculate the l2-norm of all sub-matrix with size m×n×C, m<M and n<N, then get a (M-m+1)×(N-n+1)×1 matrix. How to effectively implement it in pytorch? Thank you for your attention and answer.

This code should work for your matrix given the kernel size and stride:

kh, kw = 3, 3 # kernel size
dh, dw = 1, 1 # stride

C, M, N = 4, 7, 7
x = torch.randn(M, N, C)

patches = x.unfold(0, kh, dh).unfold(1, kw, dw)
print(patches.shape)
> torch.Size([5, 5, 4, 3, 3])
patches = patches.contiguous().view(*patches.size()[:-2], -1)
patches = torch.pow(torch.pow(patches, 2).sum(3), 0.5)
print(patches.shape)
> torch.Size([5, 5, 4])

You would have to permute the tensor or

Thank you for your answer, this help me much.