计算 TensorFlow 中两个输入集合中每对之间的成对距离

编程入门 行业动态 更新时间:2024-10-25 06:30:00
本文介绍了计算 TensorFlow 中两个输入集合中每对之间的成对距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有两个系列.一个包含 m1k 维的点,另一个包含 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, kBm2, 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:关键词]

本文发布于:2023-05-01 05:02:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1403898.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:距离   两个   TensorFlow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!