[ Tensorflow]Tensorflow Reduction operations

原创
2017/05/18 21:50
阅读数 119

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.



有问题可以加我的微信: 

 

Alvin_2580


交流学习进步!!!



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


欢迎感兴趣的朋友扫描下面的二维码,或者点上面的公众号的名字,关注!







本文分享自微信公众号 - 机器学习和数学(ML_And_Maths)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部