我有一个尺寸为(1)的张量A和一个尺寸为(N,K,L)的三维张量B.显然,B中有大小为(K,L)的N个数组,这里称为C.
现在,我该怎么计算……
你可以这样做 tf.norm 和 tf.reduce_mean :
tf.norm
tf.reduce_mean
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 ]