简单介绍几种常见的距离度量,以及tensorflow中如何实现
欧氏距离很简单,以向量为例(x1, x2, x3,….,xn),(y1, y2, y3,….,yn),那么其欧氏距离的计算公式如下图所示:
import tensorflow as tf
x3 = tf.constant([[[[1], [2], [3], [4]],
[[5], [6], [7], [8]],
[[9], [10], [11], [12]]],
[[[1], [2], [3], [4]],
[[5], [6], [7], [8]],
[[9], [10], [11], [12]]]], tf.float32)
x4 = tf.constant([[[[3], [4], [1], [2]],
[[5], [7], [8], [6]],
[[9], [12], [11], [10]]],
[[[1], [2], [3], [4]],
[[5], [6], [7], [8]],
[[9], [10], [11], [12]]]], tf.float32)
with tf.Session() as sess:
dis = sess.run(tf.square(x3-x4))
dis1 = sess.run(tf.reduce_sum(tf.square(x3-x4), 2))
euclidean = sess.run(tf.sqrt(tf.reduce_sum(tf.square(x3-x4), 2)))
print dis, dis1, euclidean
dis:
[[[[ 4.]
[ 4.]
[ 4.]
[ 4.]]
[[ 0.]
[ 1.]
[ 1.]
[ 4.]]
[[ 0.]
[ 4.]
[ 0.]
[ 4.]]]
[[[ 0.]
[ 0.]
[ 0.]
[ 0.]]
[[ 0.]
[ 0.]
[ 0.]
[ 0.]]
[[ 0.]
[ 0.]
[ 0.]
[ 0.]]]]
dis1:
[[[ 16.]
[ 6.]
[ 8.]]
[[ 0.]
[ 0.]
[ 0.]]]
Euclidean:
[[[ 3.99999976]
[ 2.44948959]
[ 2.82842684]]
[[ 0. ]
[ 0. ]
[ 0. ]]]
所以Euclidean距离的计算方法就是:
euclidean = tf.sqrt(tf.reduce_sum(tf.square(x3-x4), 2))
跟Euclidean距离相似,余弦距离也可以用来表征两个向量之间的相似度。
在Dgcnn中,edge-conv中增加余弦距离:
# 计算每个点到中心点的向量a、k邻域点到中心点的向量b
a=point_cloud_central-cloud_center # B*N*K*num_dims
b=point_cloud_neighbors-cloud_center # B*N*K*num_dims
# 求模
a_norm=tf.sqrt(tf.reduce_sum(tf.square(a), axis=-1))
b_norm=tf.sqrt(tf.reduce_sum(tf.square(b), axis=-1))
# 内积
a_b = tf.reduce_sum(tf.multiply(a, b), axis=-1)
# 余弦距离
cosin = tf.divide(a_b, tf.multiply(a_norm, b_norm)) # B*N*K
# 对余弦距离扩维
cosin=tf.expand_dims(cosin,axis=-1)
使用TensorFlow实现余弦距离/欧氏距离(Euclidean distance)以及Attention矩阵的计算