据我所知,张量流reduce_mean和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
猜猜为什么?
来自tf.constant
文档:
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))
.