简而言之,reduce系列的函数都可在张量指定的维度上操作

目录

输入参数

tf.reduce_all   在boolean张量的维度上计算元素的 "逻辑和"

tf.reduce_any 在boolean张量的维度上计算元素的 "逻辑或"

tf.reduce_max 计算张量的各个维度上元素的最大值

tf.reduce_min  计算张量的各个维度上元素的最小值

tf.reduce_mean 计算张量的各个维度上的元素的平均值

tf.reduce_sum 计算张量的各个维度上元素的总和

tf.reduce_prod 计算张量的各个维度上元素的乘积

tf.reduce_logsumexp 计算log(sum(exp(张量的各维数的元素)))

tf.reduce_join 在给定的维度上加入一个字符串张量


输入参数

  1. tf.reduce_all/reduce_any/reduce_max/reduce_min/reduce_mean/reduce_sum/reduce_prod/reduce_logsumexp(
  2. input_tensor,
  3. axis=None,
  4. keepdims=None,
  5. name=None,
  6. reduction_indices=None,
  7. keep_dims=None
  8. )
  • input_tensor:输入的张量。
  • axis:要操作的维度,如果为None(默认),则操作所有维度。必须在范围[-rank(input_tensor), rank(input_tensor))内。
  • keepdims:如果为 true,则保留长度为1的缩小维度。
  • name:操作的名称(可选)。
  • reduction_indices:参数axis的已弃用的别名名称。(为了兼容性)
  • keep_dims:参数keepdims的已弃用的别名名称。(为了兼容性)

tf.reduce_all   在boolean张量的维度上计算元素的 "逻辑和"

按照axis给定的维度减少input_tensor (boolean Tensor) 。除非keepdims为 true,否则张量的秩将在轴的每个条目中减少1。如果keep_dims为 true,则减小的维度将保留为长度1。

如果axis=None,则会减少所有维度,并返回具有单个元素的张量。

  1. x = tf.constant([[True, True], [False, False]])
  2. tf.reduce_all(x) # False
  3. tf.reduce_all(x, 0) # [False, False]
  4. tf.reduce_all(x, 1) # [True, False]

tf.reduce_any 在boolean张量的维度上计算元素的 "逻辑或"

  1. x = tf.constant([[True,  True], [False, False]])
  2. tf.reduce_any(x)  # True
  3. tf.reduce_any(x, 0)  # [True, True]
  4. tf.reduce_any(x, 1)  # [True, False]

tf.reduce_max 计算张量的各个维度上元素的最大值

  1. x = tf.constant([[1, 2, 3], [4, 5, 6]])
  2. tf.reduce_max(x) # 6
  3. tf.reduce_max(x, 0) # [4, 5, 6]
  4. tf.reduce_max(x, 1) # [3, 6]
  5. tf.reduce_max(x, 1, keepdims=True) # [[3],
  6. # [6]]
  7. tf.reduce_max(x, [0, 1]) # 6

tf.reduce_min  计算张量的各个维度上元素的最小值

  1. x = tf.constant([[1, 2, 3], [4, 5, 6]])
  2. tf.reduce_min(x) # 1
  3. tf.reduce_min(x, 0) # [1, 2, 3]
  4. tf.reduce_min(x, 1) # [1, 4]
  5. tf.reduce_min(x, 1, keepdims=True) # [[1],
  6. # [4]]
  7. tf.reduce_min(x, [0, 1]) # 1

tf.reduce_mean 计算张量的各个维度上的元素的平均值

  1. x = tf.constant([[1., 2., 3], [4., 5., 6.]])
  2. tf.reduce_mean(x) # 3.5
  3. tf.reduce_mean(x, 0) # [2.5, 3.5, 4.5]
  4. tf.reduce_mean(x, 1) # [2., 5.]
  5. tf.reduce_mean(x, 1, keepdims=True) # [[2.],
  6. # [5.]]
  7. tf.reduce_mean(x, [0, 1]) # 3.5

要注意数据类型的兼容性

  1. x = tf.constant([1, 0, 1, 0])
  2. tf.reduce_mean(x) # 0
  3. y = tf.constant([1., 0., 1., 0.])
  4. tf.reduce_mean(y) # 0.5

tf.reduce_sum 计算张量的各个维度上元素的总和

  1. x = tf.constant([[1, 2, 3], [4, 5, 6]])
  2. tf.reduce_sum(x)  # 21
  3. tf.reduce_sum(x, 0)  # [5, 7, 9]
  4. tf.reduce_sum(x, 1)  # [6, 15]
  5. tf.reduce_sum(x, 1, keepdims=True)  # [[ 6],
  6. # [15]]
  7. tf.reduce_sum(x, [0, 1])  # 21

tf.reduce_prod 计算张量的各个维度上元素的乘积

  1. x = tf.constant([[1, 2, 3], [4, 5, 6]])
  2. tf.reduce_prod(x) # 720
  3. tf.reduce_prod(x, 0) # [4, 10, 18]
  4. tf.reduce_prod(x, 1) # [6, 120]
  5. tf.reduce_prod(x, 1, keepdims=True) # [[ 6],
  6. # [120]]
  7. tf.reduce_prod(x, [0, 1]) # 720

tf.reduce_logsumexp 计算log(sum(exp(张量的各维数的元素)))

  1. x = tf.constant([[0., 0., 0.], [0., 0., 0.]])
  2. tf.reduce_logsumexp(x)  # log(6)
  3. tf.reduce_logsumexp(x, 0)  # [log(2), log(2), log(2)]
  4. tf.reduce_logsumexp(x, 1)  # [log(3), log(3)]
  5. tf.reduce_logsumexp(x, 1, keepdims=True)  # [[log(3)], [log(3)]]
  6. tf.reduce_logsumexp(x, [0, 1])  # log(6)

这个函数在数值上比 更稳定。它避免了大量输入的 exp 引起的溢出和小输入日志带来的下溢。

tf.reduce_join 在给定的维度上加入一个字符串张量

  1. tf.reduce_join(
  2. inputs,
  3. axis=None,
  4. keep_dims=False,
  5. separator='',
  6. name=None,
  7. reduction_indices=None
  8. )
  1. # tensor `a` is [["a", "b"], ["c", "d"]]
  2. tf.reduce_join(a, 0) # ==> ["ac", "bd"]
  3. tf.reduce_join(a, 1) # ==> ["ab", "cd"]
  4. tf.reduce_join(a, -2) # = tf.reduce_join(a, 0) ==> ["ac", "bd"]
  5. tf.reduce_join(a, -1) # = tf.reduce_join(a, 1) ==> ["ab", "cd"]
  6. tf.reduce_join(a, 0, keep_dims=True) # ==> [["ac", "bd"]]
  7. tf.reduce_join(a, 1, keep_dims=True) # ==> [["ab"], ["cd"]]
  8. tf.reduce_join(a, 0, separator=".") # ==> ["a.c", "b.d"]
  9. tf.reduce_join(a, [0, 1]) # ==> "acbd"
  10. tf.reduce_join(a, [1, 0]) # ==> "abcd"
  11. tf.reduce_join(a, []) # ==> [["a", "b"], ["c", "d"]]
  12. tf.reduce_join(a) # = tf.reduce_join(a, [1, 0]) ==> "abcd"

在具有给定形状的 字符串张量中计算跨维度的字符串连接。返回一个新的Tensor,它由输入字符串与给定的分隔符separator(默认:空字符串)连接创建的。axis为负则从末端向后数,-1相当于n - 1。

  • separator:可选的string。默认为""。加入时要使用的分隔符。

tensorflow reduce系列函数(tf.reduce_mean, tf.reduce_sum, tf.reduce_prod, tf.reduce_max, tf.reduce_min)相关推荐

  1. 对tf.reduce_mean API的理解就是求平均值,reduce指的是一串数据求平均值后维数降低了,可不是吗,一串向量变成了一个数,维数自然降低了

    tf.math.reduce_mean(input_tensor, axis=None, keepdims=False, name=None ) 对tf.reduce_mean的理解就是求平均值,re ...

  2. tensorflow的tf.reduce_mean函数

    tf.reduce_mean函数的作用是求平均值.第一个参数是一个集合,可以是列表.二维数组和多维数组.第二个参数指定在哪个维度上面求平均值.默认对所有的元素求平均.tf.reduce_mean 比如 ...

  3. tensorflow学习之常用函数总结:tensorflow官方例子中的诸如tf.reduce_mean()这类函数

    前言 tensorflow官网给的例子用到了很多函数,然后并没有具体说明,还要自己去翻文档,有些函数是很常用的,下面来一一总结. 正文 一,tensorflow中有一类在tensor的某一维度上求值的 ...

  4. tensorflow常用数据函数总结(tf.tile()、tf.expand_dims())

    tf.tile() tensorflow中的tile()函数是用来对张量(Tensor)进行扩展的,其特点是对当前张量内的数据进行一定规则的复制.最终的输出张量维度不变.也就是说tile可以某一维度的 ...

  5. tf.segment_xxx系列函数测试

    先看一段百度的上搜索到的结果: TensorFlow提供了一些操作,你可以使用基本的算术运算来分割输入的tensor.这里的分割操作是沿着第一个维度的一个分区,等价于这里定义了一个从第一个维度到第se ...

  6. 深度学习框架tensorflow学习与应用6(防止过拟合dropout,keep_prob =tf.placeholder(tf.float32))

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data# In[3]:# 载入数据集 mn ...

  7. tf.reduce_mean()

    版权声明:本文为博主原创文章,欢迎大家交流    https://blog.csdn.net/he_min/article/details/78694383 在tensorflow中经常见到reduc ...

  8. tf.reduce_mean

    tf.reduce_mean(x) ==> 2.5 #如果不指定第二个参数,那么就在所有的元素中取平均值 tf.reduce_mean(x, 0) ==> [2., 3.] #指定第二个参 ...

  9. TensorFlow 辨异 —— tf.add(a, b) 与 a+b(tf.assign 与 =)、tf.nn.bias_add 与 tf.add

    1. tf.add(a, b) 与 a+b 在神经网络前向传播的过程中,经常可见如下两种形式的代码: tf.add(tf.matmul(x, w), b) tf.matmul(x, w) + b 简而 ...

最新文章

  1. 来自程序员的福利!用Python做一款翻译软件
  2. Swift: 可变参数
  3. sata接口_固态硬盘应该怎么选?是SATA接口还是M.2接口好
  4. 《那些年啊,那些事——一个程序员的奋斗史》——111
  5. linux共享内存与信号量的使用
  6. Newton Method in Maching Learning
  7. windows下的_mkdir函数
  8. Java消息服务思维导图笔记
  9. python并发处理机制_Python并发编程—同步互斥
  10. 三度其三——矢量场的旋度
  11. next数组_数据结构之数组与链表
  12. html静态网页实例一(附完整代码)
  13. 在微型计算机汉字系统中一个汉字机内码,一个汉字的机内码在计算机中用2个字节表示。...
  14. 画出清明上河图的代码_【高清】清明上河图(代码)
  15. 论文浏览(45) MiCT: Mixed 3D/2D Convolutional Tube for Human Action Recognition
  16. 计算机毕设(附源码)JAVA-SSM京津冀畅游网设计
  17. c语言预处理命令12个,C语言编译预处理和预处理命令
  18. Vuforia下载详解
  19. 华为支付购买token的verify接口报错Token is expired or invalid
  20. Revers root

热门文章

  1. hbase分页查询实现
  2. ISE_FIFO_IP核接口测试(一)
  3. ue html乱码,UE UTF8 乱码
  4. 邮件怎发送HTML,请问怎么发送HTML电子邮件
  5. add函数python怎么用_Python add()函数是如何使用呢?
  6. Linux+Apache+PHP+MySQL服务器环境(CentOS篇)
  7. JAVA读取EMF文件并转化为PNG,JPG,GIF格式
  8. TThread类详解转
  9. LeetCode简单题目(#27 #28 #35 #38)-2019.10.23-4道
  10. python 柱状图设置样式_python数据可视化之图表样式调整(三)