import tensorflow as tf
a = tf.constant([[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]])
b = tf.constant([[5,4,3,2,1],[1,2,3,4,5],[1,2,3,4,5]])
product =tf.mul(a,b)
product_sum =tf.reduce_sum(tf.mul(a,b))
with tf.Session() as sess:
print product.eval()
print product_sum.eval()结果是:
[[ 5 8 9 8 5]
[ 1 4 9 16 25]
[ 1 4 9 16 25]]
145但这不是我想要的答案。
我想要得到答案
5+8+9+8+5,1+4+9+16+25,1+4+9+16+25 =35,55,55
发布于 2017-02-18 03:31:35
正如xxi在their comment中提到的,这里的正确解决方案是在调用tf.reduce_sum()时使用可选的axis参数。在您的示例中,您希望沿列轴减少,因此以下代码将起作用:
product = tf.multiply(a, b)
product_sum = tf.reduce_sum(product, axis=1)(另请注意,在TensorFlow 1.0中,tf.mul()现在是tf.multiply()。)
https://stackoverflow.com/questions/42303336
复制相似问题