前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[ Tensorflow]Tensorflow Reduction operations

[ Tensorflow]Tensorflow Reduction operations

作者头像
用户1622570
发布2018-04-11 16:01:51
1K0
发布2018-04-11 16:01:51
举报

reduce系列在平时工程中是经常使用的,其中reduce_sum是使用最频繁的一个。主要用在计算loss的时候,当我们定义好loss之后,我们一般要求loss最小,这时候就需要reduce系列。下面通过文档,我给大家简单介绍一下,不是特别难,但是不细心考虑一下,你可能永远觉得它是黑的。

其实人工智能AI,或者深度学习DL,我觉得并不是什么传说的黑科技,真正最后落在技术上的,是纯粹的计算,也没多么的高深莫测。(./不喜轻拍.sh).我有个想法就是把复杂的问题,简单化,简单的问题,通俗化。其实每次写文章,我都是想着尽量简单,就想聊天一样,通过文字,你能真真切切的感受到,到底是怎么回事。无奈,才疏学浅,每次写完都不是很满意!囧!

恩,装13完毕,下面开始正题!

阅读指南:斜体表示文档,红色表示小标题,橙色表示比较重要的点。#和""" 中间的内容"""表示注释。>>>表示代码,>>>下面的表示输出。

# Tensorflow Reduction operations

"""

Reduction,翻译为简化,约简比较合适。Reduction操作主要包括很多简化张量维度的op。

区别在于不同的Reduction,所约简的维度不同。今天主要是对文档的翻译,并举例说明。

"""

TensorFlow provides several operations that you can use to perform common math computations

that reduce various dimensions of a tensor.

# tf提供很多常见的数学计算的操作,这些操作可以减少张量维度。

# 1. reduce_sum-通过计算和来减少张量维度。

# 函数原型:

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

Computes the sum of elements across dimensions of a tensor.

# 计算张量中所遇元素的和

Reduces input_tensor along the dimensions given in reduction_indices.

Unless keep_dims is true, the rank of the tensor is reduced by 1 for each entry

in reduction_indices. If keep_dims is true, the reduced dimensions are retained with length 1.

If reduction_indices has no entries, all dimensions are reduced, and a tensor with a single element is returned.

"""

reduce_sum的第一个参数input_tensor就是输入的张量,第二个指定要约简的维度,如果不指定,则直接计算所有元素的和。

keep_dims如果为true,则reduce之后的张量中的每个元素都保留1个维度,下面看栗子比较清楚。name表示约简之后的名字。

"""

For example:

>>> import tensorflow as tf

>>> import numpy as np

>>> tensor_1 = tf.constant([[1,1,1],[1,1,1]])

>>> tensor_1_reduce = tf.reduce_mean(x)

>>> sess = tf.InteractiveSession()

>>> print(tensor_1_reduce.eval(session=sess))

6

>>> tensor_1_reduce = tf.reduce_sum(x,1)

>>> tensor_1_reduce.get_shape().as_list()

[2]

# 计算一下reduce之后的值,eval()函数用来计算,需要指定使用的session。

>>> print(tensor_1_reduce.eval(session=sess))

[3 3]

>>> tensor_1_reduce = tf.reduce_sum(x,1,True,name="reduce_sum_1")

>>> print(tensor_1_reduce.eval(session=sess))

[[3]

[3]]

>>> tensor_1_reduce.get_shape().as_list()

[2, 1]

# reduce之后,仍然是一个张量

>>> print(type(tensor_1_reduce))

<class 'tensorflow.python.framework.ops.Tensor'>

Args:

input_tensor: The tensor to reduce. Should have numeric type.

reduction_indices: The dimensions to reduce. If None (the defaut), reduces all dimensions.

keep_dims: If true, retains reduced dimensions with length 1.

name: A name for the operation (optional).

Returns:

The reduced tensor.

# 2. reduce_prod,就是reduce product,是根据元素的乘积来约简维度

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

# reduce_prod的文档和reduce_mean完全相同。下面直接看栗子:

>>> tensor_2 = tf.constant([[1,2,3],[1,2,3]], name="tensor_2")

# 先来看一下tensor 2的维度,[2,3],两行三列,还记得之前教大家的方法吗,

# 这里你不用去想2行3列的张量是什么样子的,你只要知道它有2个维度,维度从 0开始编号,所以是0,1。然后你只要想从哪个维度开始计算就好了。

>>> tensor_2.get_shape().as_list()

[2, 3]

>>> tensor_2_reduce_1 = tf.reduce_prod(tensor_2)

>>> tensor_2_reduce_1

<tf.Tensor 'Prod:0' shape=() dtype=int32>

>>> print(tensor_2_reduce_1.eval(session=sess))

36

>>> tensor_2_reduce_2 = tf.reduce_prod(tensor_2, reduction_indices=1, keep_dims=True, name="reduce_2")

>>> print(tensor_2_reduce_2.eval(session=sess))

[[6]

[6]]

# 3. reduce_min,剩下几个都是类似的。直接看文档

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

# 4. reduce_max,计算指定维度最大值,不指定则计算第0个维度的最大值。

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

# 5. reduce_mean, 平均值

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

# 6. reduce_all,稍微和其他几个不太一样,但是好像不常用。参数的意义是相同的,主要区别在于输入的tensor不一样。

# reduce_all是计算逻辑和,reduce_any是计算逻辑与。

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

Computes the "logical and" of elements across dimensions of a tensor.

# 根据指定的维度计算元素的逻辑和.

Reduces input_tensor along the dimensions given in reduction_indices.

Unless keep_dims is true, the rank of the tensor is reduced by 1 for

each entry in reduction_indices. If keep_dims is true, the reduced dimensions are retained with length 1.

# 其他参数意义相同

If reduction_indices has no entries, all dimensions are reduced, and a tensor with a single element is returned.

For example:

# 'x' is [[True, True]]

# [False, False]]

# 如果不指定维度,则遍历每个元素,如果全为True,返回值才为True,

tf.reduce_all(x) ==> False

tf.reduce_all(x, 0) ==> [False, False]

# 如果从第一个维度计算,True and true ->True,False and False -> False

tf.reduce_all(x, 1) ==> [True, False]

Args:

input_tensor: The boolean tensor to reduce.

reduction_indices: The dimensions to reduce. If None (the defaut), reduces all dimensions.

keep_dims: If true, retains reduced dimensions with length 1.

name: A name for the operation (optional).

Returns:

The reduced tensor.

# 如果我们reduce一下之前定义的tensor_2,看下会怎么样。

>>> tensor_2_reduce_all = tf.reduce_all(tensor_2)

TypeError: Input 'input' of 'All' Op has type int32 that does not match expected type of bool.

# 果然出错了!它说输入的是整型,不是所期望的布尔类型。所以这就是说,reduce_all的输入张量,必须为布尔值。

# 最后reduce_any留给大家喽。。。

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

Computes the "logical or" of elements across dimensions of a tensor.

Reduces input_tensor along the dimensions given in reduction_indices. Unless keep_dims is true, the rank of the tensor is reduced by 1 for each entry in reduction_indices. If keep_dims is true, the reduced dimensions are retained with length 1.

If reduction_indices has no entries, all dimensions are reduced, and a tensor with a single element is returned.

For example:

# 'x' is [[True, True]]

# [False, False]]

tf.reduce_any(x) ==> True

tf.reduce_any(x, 0) ==> [True, True]

tf.reduce_any(x, 1) ==> [True, False]

Args:

input_tensor: The boolean tensor to reduce.

reduction_indices: The dimensions to reduce. If None (the defaut), reduces all dimensions.

keep_dims: If true, retains reduced dimensions with length 1.

name: A name for the operation (optional).

Returns:

The reduced tensor.

tf.accumulate_n(inputs, shape=None, tensor_dtype=None, name=None)

Returns the element-wise sum of a list of tensors.

Optionally, pass shape and tensor_dtype for shape and type checking, otherwise, these are inferred.

For example:

# tensor 'a' is [[1, 2], [3, 4]

# tensor `b` is [[5, 0], [0, 6]]

tf.accumulate_n([a, b, a]) ==> [[7, 4], [6, 14]]

# Explicitly pass shape and type

tf.accumulate_n([a, b, a], shape=[2, 2], tensor_dtype=tf.int32)

==> [[7, 4], [6, 14]]

Args:

inputs: A list of Tensor objects, each with same shape and type.

shape: Shape of elements of inputs.

tensor_dtype: The type of inputs.

name: A name for the operation (optional).

Returns:

A Tensor of same shape and type as the elements of inputs.

Raises:

ValueError: If inputs don't all have same shape and dtype or the shape cannot be inferred.

[参考文献]

1. https://www.tensorflow.org/versions/r0.10/api_docs/python/math_ops/reduction.

本文为作者原创,如有雷同,必然是别人抄我的。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-05-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器学习和数学 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档