如何使用tensorflow中tf.reduce_mean函数

发布时间:2020-08-01 14:54:47 作者:小猪
来源:亿速云 阅读:192

这篇文章主要讲解了如何使用tensorflow中tf.reduce_mean函数,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。

reduce_mean(input_tensor,
        axis=None,
        keep_dims=False,
        name=None,
        reduction_indices=None)

以一个维度是2,形状是[2,3]的tensor举例:

import tensorflow as tf
 
x = [[1,2,3],
   [1,2,3]]
 
xx = tf.cast(x,tf.float32)
 
mean_all = tf.reduce_mean(xx, keep_dims=False)
mean_0 = tf.reduce_mean(xx, axis=0, keep_dims=False)
mean_1 = tf.reduce_mean(xx, axis=1, keep_dims=False)
 
 
with tf.Session() as sess:
  m_a,m_0,m_1 = sess.run([mean_all, mean_0, mean_1])
 
print m_a  # output: 2.0
print m_0  # output: [ 1. 2. 3.]
print m_1  #output: [ 2. 2.]

如果设置保持原来的张量的维度,keep_dims=True ,结果:

print m_a  # output: [[ 2.]]
print m_0  # output: [[ 1. 2. 3.]]
print m_1  #output: [[ 2.], [ 2.]]

类似函数还有:

看完上述内容,是不是对如何使用tensorflow中tf.reduce_mean函数有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 怎么使用Tensorflow中的降维函数tf.reduce_*
  2. tensorflow 分类损失函数使用小记

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

tensorflow ce

上一篇:php替换字符串中空格的方法

下一篇:TensorFlow如何打印输出tensor的值

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》