tf.compat.v1.reduce_mean用法

tf.compat.v1.reduce_mean:计算跨张量维度的元素的均值。

tf.compat.v1.reduce_mean(
    input_tensor, axis=None, keepdims=None, name=None, reduction_indices=None,
    keep_dims=None
)

通过计算各个维度axis的元素平均值,沿给定的维度axis减少input_tensor。除非keepdims为true,否则对于axis中的每个条目,张量的等级都会降低1。如果keepdims为true,则减小的维度将保留为长度1。

如果axis为None,则缩小所有维度,并返回带有单个元素的张量。

import tensorflow as tf

with tf.Session() as sess:
    x = tf.constant([[1., 1.], [2., 2.]])
    a = tf.reduce_mean(x)
    print(a)
    print(sess.run(a))
    b = tf.reduce_mean(x, 0)
    print(b)
    print(sess.run(b))
    c = tf.reduce_mean(x, 0)
    print(c)
    print(sess.run(c))

# 输出结果:
# Tensor("Mean:0", shape=(), dtype=float32)
# 1.5
# Tensor("Mean_1:0", shape=(2,), dtype=float32)
# [1.5 1.5]
# Tensor("Mean_2:0", shape=(2,), dtype=float32)
# [1.5 1.5]
import tensorflow as tf

with tf.Session() as sess:
    x = tf.constant([[1., 1.], [2., 2.]])
    print(tf.reduce_mean(x))
    print(sess.run(tf.reduce_mean(x)))
    print(tf.reduce_mean(x, 0))
    print(sess.run(tf.reduce_mean(x, 0)))
    print(tf.reduce_mean(x, 1))
    print(sess.run(tf.reduce_mean(x, 1)))

# 输出结果:
# Tensor("Mean:0", shape=(), dtype=float32)
# 1.5
# Tensor("Mean_2:0", shape=(2,), dtype=float32)
# [1.5 1.5]
# Tensor("Mean_4:0", shape=(2,), dtype=float32)
# [1. 2.]

 

Args

input_tensor要减少的张量。 应为数字类型。
axis

要减少的维度。如果为None(默认),减少所有的维度。必须在范围

[-rank(input_tensor),rank(input_tensor)]

keepdims如果为true,则保留长度为1的减少维度。
name操作的名称(可选)。
reduction_indices轴的旧名称(不建议使用)。
keep_dims不推荐使用的别名keepdims

Returns

缩减的张量。

numpy兼容性:相当于np.mean

请注意,np.mean具有一个可用于指定输出类型的dtype参数。默认情况下为dtype=float64。另一方面,tf.reduce_mean具有来自input_tensor的激进类型推断,例如:

import tensorflow as tf

with tf.Session() as sess:
    x = tf.constant([1, 0, 1, 0])
    x_ = tf.reduce_mean(x)
    print(x_)
    print(sess.run(x_))

    y = tf.constant([1., 0., 1., 0.])
    y_ = tf.reduce_mean(y)
    print(y_)
    print(sess.run(y_))


# 输出结果:
# Tensor("Mean:0", shape=(), dtype=int32)
# 0
# Tensor("Mean_1:0", shape=(), dtype=float32)
# 0.5

即,tf.reduce_mean返回与input_tensor相同的数据类型。

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值