使用TensorFlow批量计算欧几里德距离

时间:2017-03-23 13:57:00

标签: tensorflow scikit-learn euclidean-distance

fetch

以上代码效果很好。但是迭代每个向量需要花费太多时间。 那么有没有办法一次用多个向量计算相同的数据。就像我们说import tensorflow as tf import numpy as np dim = 1000 x1 = tf.placeholder('float32', shape=(None, dim)) x2 = tf.placeholder('float32', shape=(None, dim)) l2diff = tf.sqrt( tf.reduce_sum(tf.square(tf.sub(x1, x2)),reduction_indices=1)) vector1 = np.random.rand(1,1000) all_vectors = np.random.rand(500,1000) sess = tf.Session() sess.run(tf.global_variables_initializer()) distances = sess.run(l2diff, feed_dict = {x1: vector1, x2: all_vectors}) 我比 sklearn的欧几里德距离更喜欢这个,因为我想计算100k向量的相似度,并希望在 GPU 上运行它。

并且也不想复制all_vectors,因为all_vactors已经填满了我机器RAM的70%。

有没有办法通过传递一批矢量来计算距离?

0 个答案:

没有答案