美文网首页
TensorFlow 中的 tf.reduce_xxx() 函数

TensorFlow 中的 tf.reduce_xxx() 函数

作者: 酷酷滴小爽哥 | 来源:发表于2019-01-10 11:12 被阅读0次
tf.reduce_mean 函数用于计算张量 tensor 沿着指定的数轴( tensor 的某一维度)上的的平均值,主要用作降维或者计算 tensor(图像)的平均值。
reduce_mean(input_tensor,
                axis=None,
                keepdims=False,
                name=None,
                reduction_indices=None)

第一个参数 input_tensor: 输入的待降维的 tensor;

第二个参数 axis: 指定的轴,如果不指定,则计算所有元素的均值;

第三个参数 keepdims:是否降维度,设置为 True,输出的结果保持输入 tensor 的形状,设置为 False,输出结果会降低维度;

第四个参数 name: 操作的名称;

第五个参数 reduction_indices:在以前版本中用来指定轴,已弃用;

举例:

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.]

类似函数还有:

tf.reduce_sum :计算 tensor 指定轴方向上的所有元素的累加和;
tf.reduce_max : 计算 tensor 指定轴方向上的各个元素的最大值;
tf.reduce_all : 计算 tensor 指定轴方向上的各个元素的逻辑和(and 运算);
tf.reduce_any: 计算 tensor 指定轴方向上的各个元素的逻辑或(or 运算);

相关文章

网友评论

      本文标题:TensorFlow 中的 tf.reduce_xxx() 函数

      本文链接:https://www.haomeiwen.com/subject/ddiorqtx.html