I am trying to get neighbours of a cell of matrix in pytorch using below part of code. it works correctly but it is very time consumming. Have you any suggestion to to get it faster
def neighbour(x):
result=F.pad(input=x, pad=(1, 1, 1, 1), mode='constant', value=0)
for m in range(1,x.size(0)+1):
for n in range(1,x.size(1)+1):
y=torch.Tensor([result[m][n],result[m-1][n-1],result[m-1][n],result[m-1]
[n+1],result[m][n-1],result[m][n+1],result[m+1][n-1],result[m+1][n],result[m+1][n+1]])
x[m-1][n-1]=y.mean()
return x