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[0],size)
        for i, pair in enumerate(offset):
            ii = np.add(current, pair)
            if ii[0] < 0 or ii[1] < 0:
            if ii[0] >= size[0] or ii[1] >= size[1]:
            a[idx[0],np.ravel_multi_index(ii,size)] = np.abs(current_value - values[i])
        idx[0] += 1
        return 0

def getSimilarityMatrix(gt,radius, func):
    index = [0]
    h,w = gt.shape
    foot = np.array(generate_binary_structure(2, 1),dtype=int)
    footprint = np.array(iterate_structure(foot , radius),dtype=int)
    shape = footprint.shape
    num_cols = np.where(footprint==1)[1].shape[0] #int(0.5 * radius*(4 + 2*(radius-1)))# n/2(2a+(n-1)d)
    x,y = np.where(footprint==1)
    offsets = zip(x-shape[0]/2,y-shape[1]/2)
    affinity = np.zeros((h**2,w**2),dtype=np.uint8)

    results = ndimage.generic_filter(gt, func,footprint=footprint,mode='constant',
    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)


What do you mean by “L1 Distance Matrix”? Does nn.L1Loss do what you’re looking for?

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],
L1 = (X - X.t()) * indexMat
L1 = [[0,1,3,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.