【问题标题】:tensorflow reduce_mean vs numpy meantensorflow reduce_mean vs numpy mean
【发布时间】:2018-03-19 10:25:31
【问题描述】:

据我了解,tensorflow reduce_mean 和 numpy mean 应该返回相同的值,但下面的示例返回不同的值:

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

你猜为什么?

【问题讨论】:

    标签: numpy tensorflow mean


    【解决方案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]) 默认将其转换为floats 的数组。

    试试tf.constant(np.arange(3.0))

    【讨论】:

    • 感谢您的回复。我无法理解为什么数据类型很重要。你认为 tensorflow 会从 4.625 轮到 4 轮吗?
    • 是的,整数除法(取多个整数的平均值是首先对整数求和,然后对这些整数的总数求和)默认情况下通常会执行floor division,例如27 // 10 == 2 尽管2.7 似乎更接近3
    猜你喜欢
    • 2019-10-05
    • 2018-05-17
    • 2018-05-16
    • 2018-08-23
    • 2016-10-13
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多