# How to compute L1 distance matrix on tensors?

I am trying to calculate L1 Distance matrix on images and neural network features. I wrote a naive to calculate this using scipy operations on 2d arrays. If any one can suggest fast and efficient approach to calculate the same if I have 4d Tensor (N x C x H x W). I don’t want to iterate over N and C to calculate L1 Distance matrix, as it will slow down the training process.

``````import numpy as np
from scipy.ndimage import iterate_structure, generate_binary_structure
import scipy.ndimage as ndimage
from itertools import repeat

def nearestPixelDifference(values,a,idx,offset,size):
current_value = values[len(values)/2]
current = np.unravel_index(idx,size)
for i, pair in enumerate(offset):
if ii < 0 or ii < 0:
continue
if ii >= size or ii >= size:
continue
a[idx,np.ravel_multi_index(ii,size)] = np.abs(current_value - values[i])
idx += 1
return 0

index = 
h,w = gt.shape
foot = np.array(generate_binary_structure(2, 1),dtype=int)
shape = footprint.shape
x,y = np.where(footprint==1)
offsets = zip(x-shape/2,y-shape/2)
affinity = np.zeros((h**2,w**2),dtype=np.uint8)

results = ndimage.generic_filter(gt, func,footprint=footprint,mode='constant',
extra_arguments=(affinity,index,offsets,gt.shape))
return affinity,footprint
``````

Given an array x, algorithm computes L1 distance between nearest pixels within radius 2. Following is simple code for the same.

``````x = np.array([[1., 1., 1., 0.],
[1., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 1., 1., 1.]])
pixel_affinity, struct = getSimilarityMatrix(x,2,nearestPixelDifference)
``````

Thanks

What do you mean by “L1 Distance Matrix”? Does `nn.L1Loss` do what you’re looking for? https://pytorch.org/docs/master/nn.html?highlight=l1loss#torch.nn.L1Loss

Sorry @richard for not good explanation. I was looking for L1 Pairwise Distance matrix of images. e.g. I have 2x2 image and its L1 Pairwise Distance matrix (within radius distance) will be 4x4
I found one way as follows

``````X = [[2,3],
[-1,5]]
``````
``````L1 = (X - X.t()) * indexMat
L1 = [[0,1,3,0],
[1,0,0,2],
[3,0,0,6],
[0,2,6,0]]
``````

The same I would like to calculate for 4D CNN features of network. Any efficient way? One I think of calculate full distance matrix and multiply with nearest neighbor indices? But that matrix will be sparse and how to reduce computations?

Did you find a solution for your problem? I have the same question. Please let me know. thanks in advance.