2024-04-26 06:22:45 发布
网友
我有一个张量a,大小为(1,L),和一个三维张量B,大小为(N,K,L)。显然,在B中存在大小为(K,L)的N阵列,这里称之为C。你知道吗
现在,我如何计算A和C之间的平均欧氏距离(平均A和C的每一行的距离),而不迭代C中的每一行,最后返回一个大小为(1,N)的向量?你知道吗
您可以使用^{}和^{}来实现这一点:
import tensorflow as tf with tf.Graph().as_default(), tf.Session() as sess: a = tf.placeholder(tf.float32, [1, None]) b = tf.placeholder(tf.float32, [None, None, None]) dist = tf.reduce_mean(tf.norm(b - a, axis=2), axis=1) print(sess.run(dist, feed_dict={a: [[1, 2, 3]], b: [[[ 4, 5, 6], [ 7, 8, 9]], [[10, 11, 12], [13, 14, 15]]]})) # [ 7.7942286 18.186533 ]
编辑:在a中有几个向量的情况下的变体:
a
import tensorflow as tf with tf.Graph().as_default(), tf.Session() as sess: a = tf.placeholder(tf.float32, [None, None]) b = tf.placeholder(tf.float32, [None, None, None]) a_exp = tf.expand_dims(tf.expand_dims(a, 1), 1) dist = tf.reduce_mean(tf.norm(b - a_exp, axis=3), axis=2) print(sess.run(dist, feed_dict={a: [[1, 2, 3], [4, 5, 6]], b: [[[ 4, 5, 6], [ 7, 8, 9]], [[10, 11, 12], [13, 14, 15]]]})) # [[ 7.7942286 18.186533 ] # [ 2.598076 12.990381 ]]
您可以使用^{} 和^{} 来实现这一点:
编辑:在
a
中有几个向量的情况下的变体:相关问题 更多 >
编程相关推荐