# [ Tensorflow]Tensorflow Reduction operations

2017/05/18 21:50

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

# Tensorflow Reduction operations

"""

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

"""

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

0
0 收藏

0 评论
0 收藏
0