参考网站:https://stackoverflow.com/questions/34236252/what-is-the-difference-between-np-mean-and-tf-reduce-mean
tf.reduce_mean()与np.mean()实现的计算是一样的,但是相互的区别,通过以下实验可知:
# tensorflow.__version__=='1.15.4'
import tensorflow as tf
import numpy as np
###########################a
print('a:')
a = np.array([[3.,4], [5.,6], [6.,7]])
print(np.mean(a,1))
Meana = tf.reduce_mean(a,1)
with tf.Session() as sess:
resulta = sess.run(Meana)
print(resulta)
###########################b
print('b:')
b = np.array([[3.,4], [5.,6], [6.,7]])
print(np.mean(b))
Meanb = tf.reduce_mean(b)
with tf.Session() as sess:
resultb = sess.run(Meanb)
print(resultb)
###########################c
print('c:')
c = np.array([[3,4], [5,6], [6,7]])
print(np.mean(c))
print('the int output:',np.mean(c,dtype=np.int))
Meanc = tf.reduce_mean(c)
with tf.Session() as sess:
resultc = sess.run(Meanc)
print(resultc)
a:
[3.5 5.5 6.5]
[3.5 5.5 6.5]
b:
5.166666666666667
5.166666666666667
c:
5.166666666666667
the int output: 5
5
例a:实验axis的设置在求mean当中相同
例b:实验默认axis在求mean当中相同
例c:值没有小数点表示为int型,所以tf.reduce_mean结果不同
通过查看tf.reduce_mean的文档可知:
在np.mean当中默认的输出数值类型dtype是float64,所以输入是int型时,输出不受影响。
在tf.reduce_mean当中,输出数值类型来自对输入数值的推断,如实验c当中所示,tf.reduce_mean通过推断得出输入数值类型为int型,所以输出类型也为int型。
在使用深度学习当中计算损失值的时候,由于输入数值的问题可能造成模型的生成失败。