问题描述
限时送ChatGPT账号..我有两个系列.一个包含 m1 个 k 维的点,另一个包含 m2k 维中的点.我需要计算两个集合中每对之间的成对距离.
I have two collections. One consists of m1 points in k dimensions and another one of m2 points in k dimensions. I need to calculate pairwise distance between each pair of the two collections.
基本上有两个矩阵 Am1, k 和 Bm2, k 我需要得到一个矩阵 Cm1, m2.
Basically having two matrices Am1, k and Bm2, k I need to get a matrix Cm1, m2.
我可以在 scipy 中使用 distance.sdist 并选择许多距离度量之一,我也可以在循环中在 TF 中执行此操作,但我无法弄清楚如何使用矩阵操作来执行此操作,即使对于欧几里德距离也是如此.
I can easily do this in scipy by using distance.sdist and select one of many distance metrics, and I also can do this in TF in a loop, but I can't figure out how to do this with matrix manipulations even for Eucledian distance.
推荐答案
几个小时后,我终于找到了如何在 Tensorflow 中做到这一点.我的解决方案仅适用于欧几里得距离并且非常冗长.我也没有数学证明(只是大量挥手,希望能更严谨一点):
After a few hours I finally found how to do this in Tensorflow. My solution works only for Eucledian distance and is pretty verbose. I also do not have a mathematical proof (just a lot of handwaving, which I hope to make more rigorous):
import tensorflow as tf
import numpy as np
from scipy.spatial.distance import cdist
M1, M2, K = 3, 4, 2
# Scipy calculation
a = np.random.rand(M1, K).astype(np.float32)
b = np.random.rand(M2, K).astype(np.float32)
print cdist(a, b, 'euclidean'), '\n'
# TF calculation
A = tf.Variable(a)
B = tf.Variable(b)
p1 = tf.matmul(
tf.expand_dims(tf.reduce_sum(tf.square(A), 1), 1),
tf.ones(shape=(1, M2))
)
p2 = tf.transpose(tf.matmul(
tf.reshape(tf.reduce_sum(tf.square(B), 1), shape=[-1, 1]),
tf.ones(shape=(M1, 1)),
transpose_b=True
))
res = tf.sqrt(tf.add(p1, p2) - 2 * tf.matmul(A, B, transpose_b=True))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print sess.run(res)
这篇关于计算 TensorFlow 中两个输入集合中每对之间的成对距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论