转载:https://www.cnblogs.com/yuzhuwei/p/6986171.html

1.概述

深度学习里研究的物体的关系,都是比较复杂的。比如一个图片32X32大小的,它的像素信息就有1024个点,如果考虑RGB三种颜色,就是1024X3了。对于目前还没有办法构造140亿个神经元的计算机来说,只能干一件事情,就是简化,化繁为简。为了简化,就需要使用算法来进行,在数学上最简单的处理,就是求平均值。这个道理非常简单,如果10个数,只要把它们相加,再除以10即可。对于0维数字,可以这样来计算,如果对于N维的张量,就不是这么简单的计算了。就拿二维的矩阵来说,它可以横着算,还可以是竖着算,还可以全部加到一起,再除以总数。可见对于N维的张量来说,有更多的可能,那么就得抽像一个数轴出来。数轴在初中的代数就学习了,可见数轴在高等数学里也会使用到的,N维张量,就是N个数轴的表示,如果沿着不同的数轴进行计算,就会产生不同的作用。其实平均值也可以算是降维思想的一种。

在TF里,就提供了这样一个算法来计算张量的平均值:

2.tf.reduce_mean

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

沿着张量不同的数轴进行计算平均值。

input_tensor: 被计算的张量,确保为数字类型。

axis: 方向数轴,如果没有指明,默认是所有数轴都减小为1。

keep_dims: 如果定义true, 则保留维数,但数量个数为0.

name: 操作过程的名称。

reduction_indices: 为了旧函数兼容的数轴。

返回值:

降低维数的平均值。

3.代码实现

import tensorflow as tf
#创建张量
x = tf.Variable([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]);
#显示
init = tf.global_variables_initializer();
with tf.Session() as sess:
sess.run(init);
#tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)
y = tf.reduce_mean(x);
y01 = tf.reduce_mean(x, axis=0, keep_dims=False);
y02 = tf.reduce_mean(x, axis=0, keep_dims=True);
y1 = tf.reduce_mean(x, axis=1); print("x = ", x.eval());
print("tf.reduce_mean(x) = ", y.eval());
print("tf.reduce_mean(x, axis=0, keep_dims=False) = ", y01.eval());
print("tf.reduce_mean(x, axis=0, keep_dims=True) = ", y02.eval())
print("tf.reduce_mean(x, axis=1) = ", y1.eval());

执行结果:

('x = ', array([[ 1.,  2.,  3.],
[ 4., 5., 6.],
[ 7., 8., 9.]], dtype=float32))
('tf.reduce_mean(x) = ', 5.0)
('tf.reduce_mean(x, axis=0, keep_dims=False) = ', array([ 4., 5., 6.], dtype=float32))
('tf.reduce_mean(x, axis=0, keep_dims=True) = ', array([[ 4., 5., 6.]], dtype=float32))
('tf.reduce_mean(x, axis=1) = ', array([ 2., 5., 8.], dtype=float32))

总结:

tf.reduce_mean(x)表示计算全局平均值;
tf.reduce_mean(x, axis=0)表示计算y轴平均值;
tf.reduce_mean(x, axis=1)表示计算x轴平均值;

TensorFlow:tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)的更多相关文章

  1. tensorflow中 tf.reduce_mean函数

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

  2. 1、求loss:tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None))

    1.求loss: tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None)) 第一个参数log ...

  3. 【tensorflow基础】tensorflow中 tf.reduce_mean函数

    参考 1. tensorflow中 tf.reduce_mean函数: 完

  4. TensorFlow:tf.train.Saver()模型保存与恢复

    1.保存 将训练好的模型参数保存起来,以便以后进行验证或测试.tf里面提供模型保存的是tf.train.Saver()模块. 模型保存,先要创建一个Saver对象:如 saver=tf.train.S ...

  5. TensorFlow:tf.nn.max_pool实现池化操作

    tf.nn.max_pool(value, ksize, strides, padding, name=None) 参数是四个,和卷积很类似: 第一个参数value:需要池化的输入,一般池化层接在卷积 ...

  6. tf.reduce_mean

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

  7. TensorFlow函数:tf.reduce_sum

    tf.reduce_sum 函数 reduce_sum ( input_tensor , axis = None , keep_dims = False , name = None , reducti ...

  8. tf.reduce_mean函数用法及有趣区别

    sess=tf.Session() a=np.array([1,2,3,5.]) # 此代码保留为浮点数 a1=np.array([1,2,3,5]) # 此代码保留为整数 c=tf.reduce_m ...

  9. tensorflow笔记3:CRF函数:tf.contrib.crf.crf_log_likelihood()

    在分析训练代码的时候,遇到了,tf.contrib.crf.crf_log_likelihood,这个函数,于是想简单理解下: 函数的目的:使用crf 来计算损失,里面用到的优化方法是:最大似然估计 ...

随机推荐

  1. python unicode to str and str to unicode

    @staticmethod def unicode2str(p_unicode): v = p_unicode.encode('unicode-escape').decode('string_esca ...

  2. C语言 · 集合运算

    算法训练 集合运算   时间限制:1.0s   内存限制:512.0MB    问题描述 给出两个整数集合A.B,求出他们的交集.并集以及B在A中的余集. 输入格式 第一行为一个整数n,表示集合A中的 ...

  3. java - 分页类

    pager.java package com.jspnews.util; import java.io.Serializable; import java.util.List; /** * * < ...

  4. python使用pymongo访问MongoDB的基本操作,以及CSV文件导出

    1. 环境. Python:3.6.1 Python IDE:pycharm 系统:win7 2. 简单示例 import pymongo # mongodb服务的地址和端口号mongo_url = ...

  5. jQuery&CSS 顶部和底部固定浮动工具栏 兼容IE6

    http://www.cnblogs.com/lhj588/archive/2013/04/02/2994639.html —————————————————————————————————————— ...

  6. 很简单的在Ubuntu系统下安装字体和切换默认字体的方法

    摘要: Ubuntu系统安装好后,默认字体对于中文的支持看上去不太美丽,于是很多朋友可能需要设置系统的默认字体为自己喜欢的字体.本文主要介绍如何解决这两个问题. 说明:测试系统是Ubuntu14.04 ...

  7. 使用cssQuery选择器语法来查找元素

    使用选择器语法来查找元素 问题 你想使用类似于CSS或jQuery的语法来查找和操作元素. 方法 可以使用Element.select(String selector) 和 Elements.sele ...

  8. Python模拟Linux的Crontab, 写个任务计划需求

    Python模拟Linux的Crontab, 写个任务计划需求 来具体点 需求: 执行一个程序, 程序一直是运行状态, 这里假设是一个函数 当程序运行30s的时候, 需要终止程序, 可以用python ...

  9. qcom wlan kernel 解读 WCNSS_qcom_cfg.ini 文件

    CORE/HDD/src/wlan_hdd_main.c 模块初始化: static int __init hdd_module_init ( void) { return hdd_driver_in ...

  10. 【转载】Linux命令行常用光标移动快捷键

    声明:下面内容来自:http://www.linuxidc.com/Linux/2016-10/136027.htm, 来源:linux社区  作者:aslongas 我转载于此处,为了作个笔记,方便 ...