Search code examples
tensorflowkernelcovariancegaussiantensor

Evaluating the pairwise euclidean distance between multi-dimensional inputs in TensorFlow


I have two 2-D tensors of shape say m X d and n X d. What is the optimized(i.e. without for loops) or the tensorflow way of evaluating the pairwise euclidean distance between these two tensors so that I get an output tensor of shape m X n. I need it for creating the squared term of a Gaussian kernel for ultimately having a covariance matrix of size m x n.enter image description here

The equivalent unoptimized numpy code would look like this

difference_squared = np.zeros((x.shape[0], x_.shape[0]))
for row_iterator in range(difference_squared.shape[0]):
    for column_iterator in range(difference_squared.shape[1]):
        difference_squared[row_iterator, column_iterator] = np.sum(np.power(x[row_iterator]-x_[column_iterator], 2))

Solution

  • I found the answer by taking help from here. Assuming the two tensors are x1 and x2, and their dimensions are m X d and n X d, their pair-wise Euclidean distance is given by

    tile_1 = tf.tile(tf.expand_dims(x1, 0), [n, 1, 1])
    tile_2 = tf.tile(tf.expand_dims(x2, 1), [1, m, 1])
    pairwise_euclidean_distance = tf.reduce_sum(tf.square(tf.subtract(tile_1, tile_2)), 2))