2017-10-07 100 views
0

據我瞭解tensorflow reduce_mean和numpy的平均值應返回相同的值,但下面的例子返回的值不同:tensorflow reduce_mean VS numpy的意思

import numpy as np 
import tensorflow as tf 

t_1 = tf.constant([1,3,4,5]) 
t_2 = tf.constant([7,8,9,0]) 
list_t = [t_1, t_2] 
reduced_t_list = tf.reduce_mean(list_t) 
sess= tf.Session() 
print(sess.run(reduced_t_list)) 
print(np.mean([1,3,4,5,7,8,9,0])) 

output: 
4 
4.625 

任何猜測,爲什麼?

回答

1

tf.constant docs

If the argument dtype is not specified, then the type is inferred from the type of value. 

的的[1, 2, 3, 4]dtypeint,而np.mean([1, 2, 3])它轉換到默認的的float列數組。

嘗試tf.constant(np.arange(3.0))

+0

謝謝你的回覆。我無法理解爲什麼數據類型很重要。你認爲tensorflow是4.625還是4? – user1700890

+1

是的,整數除法(並且取多個整數的平均值是首先求和整數,然後在這些整數的總數上跳躍)通常會[floor division](http://python-history.blogspot.in/2010/08/ why-pythons-integer-division-floors.html),例如'27 // 10 == 2'儘管'2.7'似乎更接近'3'。 –