python tensorflow l2 loss over axis

2019-06-10 07:19发布

问题:

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.

回答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.        ]]


回答2:

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

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)))