2017-02-17 78 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()。)