Python tf.reduce_均值的数值稳定性

Python tf.reduce_均值的数值稳定性,python,tensorflow,precision,Python,Tensorflow,Precision,在cpu和gpu上,tf.reduce_mean的数值稳定性比np.mean差 当总和超过浮点类型的限制时,tf.reduce\u mean是否存在数值问题 对于tensorflow中的float16数组,有没有更好的方法来计算平均值 结果(cpu、tf 1.13.1、linux): 结果(gpu、计算能力5.2、TF1.13.1、cuda 10.1、linux): 结果(gpu、计算能力7.0、TF1.13.1、cuda 9.0、linux): 测试: 发件人: 默认情况下,使用float32

在cpu和gpu上,
tf.reduce_mean
的数值稳定性比
np.mean

当总和超过浮点类型的限制时,
tf.reduce\u mean
是否存在数值问题

对于tensorflow中的
float16
数组,有没有更好的方法来计算平均值

结果(cpu、tf 1.13.1、linux):

结果(gpu、计算能力5.2、TF1.13.1、cuda 10.1、linux):

结果(gpu、计算能力7.0、TF1.13.1、cuda 9.0、linux):

测试:

发件人:

默认情况下,使用float32中间值计算float16结果以获得更高的精度

发件人:

请注意,np.mean有一个
dtype
参数,可用于指定输出类型。默认情况下,这是
dtype=float64
。另一方面,
tf.reduce\u mean
具有来自
input\u tensor
的攻击性类型推断

因此,可能没有比sess.run(tf.reduce_-mean(tf.cast(x,np.float32))更好的方法了。结果:

tf.reduce_mean, tree reduction: 0.5
测试:


我尝试了这个,结果是
0.5
。我正在使用Tensorflow 1.13.1和GPU

import numpy as np
import tensorflow as tf

x = np.random.random(size=10**8).astype(np.float16)
px = tf.placeholder(dtype=tf.float16, shape=(None,), name="x")

with tf.Session() as sess:
    print(sess.run(tf.reduce_mean(px), feed_dict={px: x}))

我正在使用来自anaconda的linux和tensorflow 1.13.1。你的gpu的计算能力是什么?我也是。不过我使用的是Windows 10。我的GPU的计算能力是5.0。当我在5.0 GPU上运行你的代码时,我得到了相同的结果。我听说feed\u dict比张量(以及tf.queue)慢。所以我不能一直使用它,实际上我不能运行你提供的代码。
tf.constant
调用被卡住(当它试图将巨大数组放入图形中时)。当我将数组大小减小到10**5时,
tf.constant
调用终止,
tf.reduce\u mean
得到一个0.0的结果。
np.mean 64: 0.4996047117607758
np.sum  64: 499604.7117607758
np.mean 16: 0.4995
np.sum  16: inf
tf.reduce_mean 16: nan
"""
Test numerical stability of reduce_mean
"""

import numpy as np
import tensorflow as tf


N = int(1e6)
dtype = np.float16

x = np.random.random(size=N)

print("np.mean 64:", np.mean(x))
print("np.sum  64:", np.sum(x))
x = x.astype(np.float16)
mean16 = np.mean(x)
print("np.mean 16:", np.mean(x))
print("np.sum  16:", np.sum(x))

with tf.Session() as sess:
    x = tf.constant(x, dtype=np.float16)
    print("tf.reduce_mean 16:",
          sess.run(tf.reduce_mean(x)))
tf.reduce_mean, tree reduction: 0.5
import numpy as np
import tensorflow as tf


N = int(1e8)
x = np.random.random(size=N).astype(np.float16)

with tf.Session() as sess:
    a = tf.reshape(x, (100, 100, 100, 100))
    for i in range(4):
        a = tf.reduce_mean(a, axis=-1)
    print("tf.reduce_mean, tree reduction:", sess.run(a))
import numpy as np
import tensorflow as tf

x = np.random.random(size=10**8).astype(np.float16)
px = tf.placeholder(dtype=tf.float16, shape=(None,), name="x")

with tf.Session() as sess:
    print(sess.run(tf.reduce_mean(px), feed_dict={px: x}))