发布于 2019-01-27 16:09:18
这个答案展示了如何计算向量集合之间平方差的成对和。通过简单地用平方根进行后组合,您就可以达到所需的成对距离:
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. ]]发布于 2019-01-27 14:03:05
您可以根据欧氏距离公式( TensorFlow损失)编写L2操作。

distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(x1, x2))))样本将是
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))正如@fuglede所指出的,如果您想输出成对的距离,那么我们可以使用
tf.sqrt(tf.square(tf.subtract(x1, x2)))https://stackoverflow.com/questions/54388794
复制相似问题