zoukankan      html  css  js  c++  java
  • tf.reduce_mean()函数解析(最清晰的解释)

    欢迎关注WX公众号:【程序员管小亮】

    最近学习中碰到了以前学过的tf.placeholder()函数,特此记录。

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

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

    参数:

    • input_tensor: 输入的待降维的tensor
    • axis: 指定的轴,如果不指定,则计算所有元素的均值
    • keep_dims:是否降维度,默认False。设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度
    • name: 操作的名称
    • reduction_indices:在以前版本中用来指定轴,已弃用

    例子1:

    import tensorflow as tf
    
    x = [[1,2,3],
         [4,5,6]]
    y = tf.cast(x, tf.float32)
    
    mean_all = tf.reduce_mean(y)
    mean_0 = tf.reduce_mean(y, axis=0)
    mean_1 = tf.reduce_mean(y, axis=1)
    
    with tf.Session() as sess:
        m_a,m_0,m_1 = sess.run([mean_all, mean_0, mean_1])
     
    print(m_a)
    print(m_0)
    print(m_1)
    
    > 3.5
    > [2.5 3.5 4.5]
    > [2. 5.]
    

    例子2:

    import tensorflow as tf
    
    x = [[1,2,3],
         [4,5,6]]
    y = tf.cast(x, tf.float32)
    
    mean_all = tf.reduce_mean(y, keep_dims=True)
    mean_0 = tf.reduce_mean(y, axis=0, keep_dims=True)
    mean_1 = tf.reduce_mean(y, axis=1, keep_dims=True)
    
    with tf.Session() as sess:
        m_a,m_0,m_1 = sess.run([mean_all, mean_0, mean_1])
     
    print(m_a)
    print(m_0)
    print(m_1)
    
    > [[3.5]]
    > [[2.5 3.5 4.5]]
    > [[2.]
       [5.]]
    

    如果要设置保持原来的张量的维度,那么keep_dims=True

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

    python课程推荐。
    在这里插入图片描述

  • 相关阅读:
    3. 无重复字符的最长子串
    字节跳动 最小栈
    排序
    线程的优先级
    线程的操作方法
    线程的生命周期
    实现线程的方式:Thread类重写run();Runnable类重写run();Callable类重写call();实现线程的方式
    Java thread run() start() 是干什么的以及区别
    Java thread 多线程
    助教工作学期总结
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13302842.html
Copyright © 2011-2022 走看看