tensorflow reduce_mean VS numpy的意思
问题描述:
据我了解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
任何猜测,为什么?
答
If the argument dtype is not specified, then the type is inferred from the type of value.
的的[1, 2, 3, 4]
dtype
是int
,而np.mean([1, 2, 3])
它转换到默认的的float
列数组。
尝试tf.constant(np.arange(3.0))
。
谢谢你的回复。我无法理解为什么数据类型很重要。你认为tensorflow是4.625还是4? – user1700890
是的,整数除法(并且取多个整数的平均值是首先求和整数,然后在这些整数的总数上跳跃)通常会[floor division](http://python-history.blogspot.in/2010/08/ why-pythons-integer-division-floors.html),例如'27 // 10 == 2'尽管'2.7'似乎更接近'3'。 –