0

I am using python 3 with tensorflow I have a matrix, each row is a vector, I want to get a distance matrix - that is computer using the l2 norm loss, each value in the matrix will be a distance between two vectors

e.g

Dij = l2_distance(M(i,:), Mj(j,:)) 

Thanks

edit: this is not a duplicate of that other question is about computing the norm for the each row of a matrix, I need the pairwise norm distance between each row to every other row.

thebeancounter
  • 4,261
  • 8
  • 61
  • 109
  • Possible duplicate of [Compute pairwise distance in a batch without replicating tensor in Tensorflow?](https://stackoverflow.com/questions/37009647/compute-pairwise-distance-in-a-batch-without-replicating-tensor-in-tensorflow) – fuglede Jan 27 '19 at 14:56

2 Answers2

1

This answer shows how to compute the pair-wise sum of squared differences between a collection of vectors. By simply post-composing with the square root, you arrive at your desired pair-wise distances:

M = tf.constant([[0, 0], [2, 2], [5, 5]], dtype=tf.float64)
r = tf.reduce_sum(M*M, 1)
r = tf.reshape(r, [-1, 1])
D2 = r - 2*tf.matmul(M, tf.transpose(M)) + tf.transpose(r)
D = tf.sqrt(D2)

with tf.Session() as sess:
    print(sess.run(D))

# [[0.         2.82842712 7.07106781]
#  [2.82842712 0.         4.24264069]
#  [7.07106781 4.24264069 0.        ]]
fuglede
  • 17,388
  • 2
  • 54
  • 99
0

You can write a TensorFlow operation based on the formula of Euclidean distance (L2 loss).

enter image description here

distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(x1, x2))))

Sample would be

import tensorflow as tf

x1 = tf.constant([1, 2, 3], dtype=tf.float32)
x2 = tf.constant([4, 5, 6], dtype=tf.float32)

distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(x1, x2))))

with tf.Session() as sess:
  print(sess.run(distance))

As pointed out by @fuglede, if you want to output the pairwise distances, then we can use

tf.sqrt(tf.square(tf.subtract(x1, x2)))
afagarap
  • 650
  • 2
  • 10
  • 22
  • That only gives you a single number; if that's what they wanted, they'd probably just use `tf.norm` directly. Rather, I imagine they want to know how to perform the broadcasting you'd do in NumPy to get all pairwise distances at once. – fuglede Jan 27 '19 at 14:33
  • If that's the case, then we can simply remove the `tf.reduce_mean` to output the pairwise distances. – afagarap Jan 27 '19 at 14:36
  • If you remove `tf.reduce_mean`, what you get is just the element-wise differences between the two vectors. Their input is an matrix, and they want the pair-wise distances between the rows in that, so that e.g. the diagonal in the result vanishes, i.e. what amounts to `scipy.spatial.distance.pdist`. – fuglede Jan 27 '19 at 14:40
  • what will that output? a matrix that is the pairwise distance between each row to each other row (as vectors)? – thebeancounter Jan 27 '19 at 15:13
  • could you please provide an example that will work with a matrix? as stated in the op I have a matrix, each row is a vector, I need the pairwise distance between each vector to every other vector. e.g a 32*100 matrix will provide us with a 32*32 matrix. Thanks. – thebeancounter Jan 27 '19 at 15:40
  • @thebeancounter: See the accepted answer in the existing question: If you take that verbatim and apply square-roots element-wise, you'll get your pair-wise distances. – fuglede Jan 27 '19 at 15:54
  • @fuglede - sorry, can't see that, could you post a complete answer so i'll accept it? – thebeancounter Jan 27 '19 at 15:55