2017-02-17 70 views
1
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() 

结果是:Tensorflow数学运算reduce_sum

[[ 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]

+2

'product_sum = tf.reduce_sum (产品,轴= 1)' – xxi

+0

是的,你是对的,太棒了!谢谢! –

回答

1

由于xxx在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()。)