# Reduction

TensorFlow provides several operations that you can use to perform common math computations that reduce various dimensions of a tensor.

### 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.

For example:

# 'x' is [[1, 1, 1]
#         [1, 1, 1]]
tf.reduce_sum(x) ==> 6
tf.reduce_sum(x, 0) ==> [2, 2, 2]
tf.reduce_sum(x, 1) ==> [3, 3]
tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]
tf.reduce_sum(x, [0, 1]) ==> 6

##### Args:
• input_tensor: The tensor to reduce. Should have numeric type.
• reduction_indices: The dimensions to reduce. If None (the default), 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.reduce_prod(input_tensor, reduction_indices=None, keep_dims=False, name=None)

Computes the product 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.

##### Args:
• input_tensor: The tensor to reduce. Should have numeric type.
• reduction_indices: The dimensions to reduce. If None (the default), 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.reduce_min(input_tensor, reduction_indices=None, keep_dims=False, name=None)

Computes the minimum 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.

##### Args:
• input_tensor: The tensor to reduce. Should have numeric type.
• reduction_indices: The dimensions to reduce. If None (the default), 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.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None)

Computes the maximum 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.

##### Args:
• input_tensor: The tensor to reduce. Should have numeric type.
• reduction_indices: The dimensions to reduce. If None (the default), 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.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)

Computes the mean 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 [[1., 1.]
#         [2., 2.]]
tf.reduce_mean(x) ==> 1.5
tf.reduce_mean(x, 0) ==> [1.5, 1.5]
tf.reduce_mean(x, 1) ==> [1.,  2.]

##### Args:
• input_tensor: The tensor to reduce. Should have numeric type.
• reduction_indices: The dimensions to reduce. If None (the default), 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.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]]
tf.reduce_all(x) ==> False
tf.reduce_all(x, 0) ==> [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 default), 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.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 default), 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.reduce_logsumexp(input_tensor, reduction_indices=None, keep_dims=False, name=None)

Computes log(sum(exp(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.

This funciton is more numerically stable than log(sum(exp(input))). It avoids overflows caused by taking the exp of large inputs and underflows caused by taking the log of small inputs.

For example:

# 'x' is [[0, 0, 0]]
#         [0, 0, 0]]
tf.reduce_logsumexp(x) ==> log(6)
tf.reduce_logsumexp(x, 0) ==> [log(2), log(2), log(2)]
tf.reduce_logsumexp(x, 1) ==> [log(3), log(3)]
tf.reduce_logsumexp(x, 1, keep_dims=True) ==> [[log(3)], [log(3)]]
tf.reduce_logsumexp(x, [0, 1]) ==> log(6)

##### 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.

### 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.

NOTE: This operation is not differentiable and cannot be used if inputs depend on trainable variables. Please use tf.add_n for such cases.

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.

### tf.einsum(axes, *inputs)

A generalized contraction between tensors of arbitrary dimension.

Like numpy.einsum.