diff options
Diffstat (limited to 'tensorflow/python/ops/math_ops.py')
-rw-r--r-- | tensorflow/python/ops/math_ops.py | 258 |
1 files changed, 83 insertions, 175 deletions
diff --git a/tensorflow/python/ops/math_ops.py b/tensorflow/python/ops/math_ops.py index e2e23dccef..4c400423b6 100644 --- a/tensorflow/python/ops/math_ops.py +++ b/tensorflow/python/ops/math_ops.py @@ -170,13 +170,14 @@ from tensorflow.python.ops import state_ops from tensorflow.python.ops.gen_math_ops import * # pylint: enable=wildcard-import from tensorflow.python.util import compat -from tensorflow.python.util import deprecation +from tensorflow.python.util.deprecation import deprecated +from tensorflow.python.util.deprecation import deprecated_args # Aliases for some automatically-generated names. linspace = gen_math_ops.lin_space -arg_max = deprecation.deprecated(None, "Use `argmax` instead")(arg_max) # pylint: disable=used-before-assignment -arg_min = deprecation.deprecated(None, "Use `argmin` instead")(arg_min) # pylint: disable=used-before-assignment +arg_max = deprecated(None, "Use `argmax` instead")(arg_max) # pylint: disable=used-before-assignment +arg_min = deprecated(None, "Use `argmin` instead")(arg_min) # pylint: disable=used-before-assignment def _set_doc(doc): @@ -189,8 +190,7 @@ def _set_doc(doc): # pylint: disable=redefined-builtin -@deprecation.deprecated_args(None, "Use the `axis` argument instead", - "dimension") +@deprecated_args(None, "Use the `axis` argument instead", "dimension") @_set_doc( gen_math_ops.arg_max.__doc__.replace("dimensions", "axes").replace( "dimension", "axis")) @@ -208,8 +208,7 @@ def argmax(input, return gen_math_ops.arg_max(input, axis, name=name, output_type=output_type) -@deprecation.deprecated_args(None, "Use the `axis` argument instead", - "dimension") +@deprecated_args(None, "Use the `axis` argument instead", "dimension") @_set_doc( gen_math_ops.arg_min.__doc__.replace("dimensions", "axes").replace( "dimension", "axis")) @@ -325,7 +324,7 @@ multiply.__doc__ = gen_math_ops._mul.__doc__.replace("Mul", "`tf.multiply`") # TODO(aselle): put deprecation in after another round of global code changes -@deprecation.deprecated( +@deprecated( "2016-12-30", "`tf.mul(x, y)` is deprecated, please use `tf.multiply(x, y)` or `x * y`") def _mul(x, y, name=None): @@ -344,7 +343,7 @@ subtract.__doc__ = gen_math_ops._sub.__doc__.replace("`Sub`", "`tf.subtract`") # TODO(aselle): put deprecation in after another round of global code changes -@deprecation.deprecated( +@deprecated( "2016-12-30", "`tf.sub(x, y)` is deprecated, please use `tf.subtract(x, y)` or `x - y`") def _sub(x, y, name=None): @@ -382,9 +381,8 @@ def negative(x, name=None): # pylint: disable=g-docstring-has-escape -@deprecation.deprecated( - "2016-12-30", - "`tf.neg(x)` is deprecated, please use `tf.negative(x)` or `-x`") +@deprecated("2016-12-30", + "`tf.neg(x)` is deprecated, please use `tf.negative(x)` or `-x`") def _neg(x, name=None): """Computes numerical negative value element-wise. @@ -1271,27 +1269,24 @@ def _ReductionDims(x, axis, reduction_indices): return range(0, array_ops.rank(x)) -def _may_reduce_to_scalar(keepdims, axis, reduction_indices, output): +def _may_reduce_to_scalar(keep_dims, axis, reduction_indices, output): """Set a reduction's output's shape to be a scalar if we are certain.""" - if (not output.shape.is_fully_defined()) and (not keepdims) and ( + if (not output.shape.is_fully_defined()) and (not keep_dims) and ( axis is None) and (reduction_indices is None): output.set_shape(()) return output -@deprecation.deprecated_args( - None, "keep_dims is deprecated, use keepdims instead", "keep_dims") def reduce_sum(input_tensor, axis=None, - keepdims=None, + keep_dims=False, name=None, - reduction_indices=None, - keep_dims=None): + reduction_indices=None): """Computes the sum of elements across dimensions of a tensor. Reduces `input_tensor` along the dimensions given in `axis`. - Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each - entry in `axis`. If `keepdims` is true, the reduced dimensions + Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each + entry in `axis`. If `keep_dims` is true, the reduced dimensions are retained with length 1. If `axis` has no entries, all dimensions are reduced, and a @@ -1304,7 +1299,7 @@ def reduce_sum(input_tensor, 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, keepdims=True) # [[3], [3]] + tf.reduce_sum(x, 1, keep_dims=True) # [[3], [3]] tf.reduce_sum(x, [0, 1]) # 6 ``` @@ -1313,10 +1308,9 @@ def reduce_sum(input_tensor, axis: The dimensions to reduce. If `None` (the default), reduces all dimensions. Must be in the range `[-rank(input_tensor), rank(input_tensor))`. - keepdims: If true, retains reduced dimensions with length 1. + keep_dims: If true, retains reduced dimensions with length 1. name: A name for the operation (optional). reduction_indices: The old (deprecated) name for axis. - keep_dims: Deprecated alias for `keepdims`. Returns: The reduced tensor. @@ -1325,34 +1319,26 @@ def reduce_sum(input_tensor, Equivalent to np.sum @end_compatibility """ - keepdims = deprecation.deprecated_argument_lookup("keepdims", keepdims, - "keep_dims", keep_dims) - if keepdims is None: - keepdims = False - - return _may_reduce_to_scalar(keepdims, axis, reduction_indices, + return _may_reduce_to_scalar(keep_dims, axis, reduction_indices, gen_math_ops._sum( input_tensor, _ReductionDims(input_tensor, axis, reduction_indices), - keepdims, + keep_dims, name=name)) -@deprecation.deprecated_args( - None, "keep_dims is deprecated, use keepdims instead", "keep_dims") def count_nonzero(input_tensor, axis=None, - keepdims=None, + keep_dims=False, dtype=dtypes.int64, name=None, - reduction_indices=None, - keep_dims=None): + reduction_indices=None): """Computes number of nonzero elements across dimensions of a tensor. Reduces `input_tensor` along the dimensions given in `axis`. - Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each - entry in `axis`. If `keepdims` is true, the reduced dimensions + Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each + entry in `axis`. If `keep_dims` is true, the reduced dimensions are retained with length 1. If `axis` has no entries, all dimensions are reduced, and a @@ -1369,7 +1355,7 @@ def count_nonzero(input_tensor, tf.count_nonzero(x) # 3 tf.count_nonzero(x, 0) # [1, 2, 0] tf.count_nonzero(x, 1) # [1, 2] - tf.count_nonzero(x, 1, keepdims=True) # [[1], [2]] + tf.count_nonzero(x, 1, keep_dims=True) # [[1], [2]] tf.count_nonzero(x, [0, 1]) # 3 ``` @@ -1378,20 +1364,14 @@ def count_nonzero(input_tensor, axis: The dimensions to reduce. If `None` (the default), reduces all dimensions. Must be in the range `[-rank(input_tensor), rank(input_tensor))`. - keepdims: If true, retains reduced dimensions with length 1. + keep_dims: If true, retains reduced dimensions with length 1. dtype: The output dtype; defaults to `tf.int64`. name: A name for the operation (optional). reduction_indices: The old (deprecated) name for axis. - keep_dims: Deprecated alias for `keepdims`. Returns: The reduced tensor (number of nonzero values). """ - keepdims = deprecation.deprecated_argument_lookup("keepdims", keepdims, - "keep_dims", keep_dims) - if keepdims is None: - keepdims = False - with ops.name_scope(name, "count_nonzero", [input_tensor]): input_tensor = ops.convert_to_tensor(input_tensor, name="input_tensor") zero = input_tensor.dtype.as_numpy_dtype() @@ -1400,24 +1380,21 @@ def count_nonzero(input_tensor, # int64 reduction happens on GPU to_int64(gen_math_ops.not_equal(input_tensor, zero)), axis=axis, - keepdims=keepdims, + keep_dims=keep_dims, reduction_indices=reduction_indices), dtype=dtype) -@deprecation.deprecated_args( - None, "keep_dims is deprecated, use keepdims instead", "keep_dims") def reduce_mean(input_tensor, axis=None, - keepdims=None, + keep_dims=False, name=None, - reduction_indices=None, - keep_dims=None): + reduction_indices=None): """Computes the mean of elements across dimensions of a tensor. Reduces `input_tensor` along the dimensions given in `axis`. - Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each - entry in `axis`. If `keepdims` is true, the reduced dimensions + Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each + entry in `axis`. If `keep_dims` is true, the reduced dimensions are retained with length 1. If `axis` has no entries, all dimensions are reduced, and a @@ -1437,58 +1414,36 @@ def reduce_mean(input_tensor, axis: The dimensions to reduce. If `None` (the default), reduces all dimensions. Must be in the range `[-rank(input_tensor), rank(input_tensor))`. - keepdims: If true, retains reduced dimensions with length 1. + keep_dims: If true, retains reduced dimensions with length 1. name: A name for the operation (optional). reduction_indices: The old (deprecated) name for axis. - keep_dims: Deprecated alias for `keepdims`. Returns: The reduced tensor. @compatibility(numpy) Equivalent to np.mean - - Please note that `np.mean` has a `dtype` parameter that could be used to - specify the output type. By default this is `dtype=float64`. On the other - hand, `tf.reduce_mean` has an aggressive type inference from `input_tensor`, - for example: - - ```python - x = tf.constant([1, 0, 1, 0]) - tf.reduce_mean(x) # 0 - y = tf.constant([1., 0., 1., 0.]) - tf.reduce_mean(y) # 0.5 - ``` - @end_compatibility """ - keepdims = deprecation.deprecated_argument_lookup("keepdims", keepdims, - "keep_dims", keep_dims) - - if keepdims is None: - keepdims = False - return _may_reduce_to_scalar(keepdims, axis, reduction_indices, + return _may_reduce_to_scalar(keep_dims, axis, reduction_indices, gen_math_ops._mean( input_tensor, _ReductionDims(input_tensor, axis, reduction_indices), - keepdims, + keep_dims, name=name)) -@deprecation.deprecated_args( - None, "keep_dims is deprecated, use keepdims instead", "keep_dims") def reduce_prod(input_tensor, axis=None, - keepdims=None, + keep_dims=False, name=None, - reduction_indices=None, - keep_dims=None): + reduction_indices=None): """Computes the product of elements across dimensions of a tensor. Reduces `input_tensor` along the dimensions given in `axis`. - Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each - entry in `axis`. If `keepdims` is true, the reduced dimensions + Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each + entry in `axis`. If `keep_dims` is true, the reduced dimensions are retained with length 1. If `axis` has no entries, all dimensions are reduced, and a @@ -1499,10 +1454,9 @@ def reduce_prod(input_tensor, axis: The dimensions to reduce. If `None` (the default), reduces all dimensions. Must be in the range `[-rank(input_tensor), rank(input_tensor))`. - keepdims: If true, retains reduced dimensions with length 1. + keep_dims: If true, retains reduced dimensions with length 1. name: A name for the operation (optional). reduction_indices: The old (deprecated) name for axis. - keep_dims: Deprecated alias for `keepdims`. Returns: The reduced tensor. @@ -1511,33 +1465,25 @@ def reduce_prod(input_tensor, Equivalent to np.prod @end_compatibility """ - keepdims = deprecation.deprecated_argument_lookup("keepdims", keepdims, - "keep_dims", keep_dims) - - if keepdims is None: - keepdims = False - return _may_reduce_to_scalar(keepdims, axis, reduction_indices, + return _may_reduce_to_scalar(keep_dims, axis, reduction_indices, gen_math_ops._prod( input_tensor, _ReductionDims(input_tensor, axis, reduction_indices), - keepdims, + keep_dims, name=name)) -@deprecation.deprecated_args( - None, "keep_dims is deprecated, use keepdims instead", "keep_dims") def reduce_min(input_tensor, axis=None, - keepdims=None, + keep_dims=False, name=None, - reduction_indices=None, - keep_dims=None): + reduction_indices=None): """Computes the minimum of elements across dimensions of a tensor. Reduces `input_tensor` along the dimensions given in `axis`. - Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each - entry in `axis`. If `keepdims` is true, the reduced dimensions + Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each + entry in `axis`. If `keep_dims` is true, the reduced dimensions are retained with length 1. If `axis` has no entries, all dimensions are reduced, and a @@ -1548,10 +1494,9 @@ def reduce_min(input_tensor, axis: The dimensions to reduce. If `None` (the default), reduces all dimensions. Must be in the range `[-rank(input_tensor), rank(input_tensor))`. - keepdims: If true, retains reduced dimensions with length 1. + keep_dims: If true, retains reduced dimensions with length 1. name: A name for the operation (optional). reduction_indices: The old (deprecated) name for axis. - keep_dims: Deprecated alias for `keepdims`. Returns: The reduced tensor. @@ -1560,32 +1505,25 @@ def reduce_min(input_tensor, Equivalent to np.min @end_compatibility """ - keepdims = deprecation.deprecated_argument_lookup("keepdims", keepdims, - "keep_dims", keep_dims) - if keepdims is None: - keepdims = False - return _may_reduce_to_scalar(keepdims, axis, reduction_indices, + return _may_reduce_to_scalar(keep_dims, axis, reduction_indices, gen_math_ops._min( input_tensor, _ReductionDims(input_tensor, axis, reduction_indices), - keepdims, + keep_dims, name=name)) -@deprecation.deprecated_args( - None, "keep_dims is deprecated, use keepdims instead", "keep_dims") def reduce_max(input_tensor, axis=None, - keepdims=None, + keep_dims=False, name=None, - reduction_indices=None, - keep_dims=None): + reduction_indices=None): """Computes the maximum of elements across dimensions of a tensor. Reduces `input_tensor` along the dimensions given in `axis`. - Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each - entry in `axis`. If `keepdims` is true, the reduced dimensions + Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each + entry in `axis`. If `keep_dims` is true, the reduced dimensions are retained with length 1. If `axis` has no entries, all dimensions are reduced, and a @@ -1596,10 +1534,9 @@ def reduce_max(input_tensor, axis: The dimensions to reduce. If `None` (the default), reduces all dimensions. Must be in the range `[-rank(input_tensor), rank(input_tensor))`. - keepdims: If true, retains reduced dimensions with length 1. + keep_dims: If true, retains reduced dimensions with length 1. name: A name for the operation (optional). reduction_indices: The old (deprecated) name for axis. - keep_dims: Deprecated alias for `keepdims`. Returns: The reduced tensor. @@ -1608,32 +1545,25 @@ def reduce_max(input_tensor, Equivalent to np.max @end_compatibility """ - keepdims = deprecation.deprecated_argument_lookup("keepdims", keepdims, - "keep_dims", keep_dims) - if keepdims is None: - keepdims = False - return _may_reduce_to_scalar(keepdims, axis, reduction_indices, + return _may_reduce_to_scalar(keep_dims, axis, reduction_indices, gen_math_ops._max( input_tensor, _ReductionDims(input_tensor, axis, reduction_indices), - keepdims, + keep_dims, name=name)) -@deprecation.deprecated_args( - None, "keep_dims is deprecated, use keepdims instead", "keep_dims") def reduce_all(input_tensor, axis=None, - keepdims=None, + keep_dims=False, name=None, - reduction_indices=None, - keep_dims=None): + reduction_indices=None): """Computes the "logical and" of elements across dimensions of a tensor. Reduces `input_tensor` along the dimensions given in `axis`. - Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each - entry in `axis`. If `keepdims` is true, the reduced dimensions + Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each + entry in `axis`. If `keep_dims` is true, the reduced dimensions are retained with length 1. If `axis` has no entries, all dimensions are reduced, and a @@ -1653,10 +1583,9 @@ def reduce_all(input_tensor, axis: The dimensions to reduce. If `None` (the default), reduces all dimensions. Must be in the range `[-rank(input_tensor), rank(input_tensor))`. - keepdims: If true, retains reduced dimensions with length 1. + keep_dims: If true, retains reduced dimensions with length 1. name: A name for the operation (optional). reduction_indices: The old (deprecated) name for axis. - keep_dims: Deprecated alias for `keepdims`. Returns: The reduced tensor. @@ -1665,32 +1594,25 @@ def reduce_all(input_tensor, Equivalent to np.all @end_compatibility """ - keepdims = deprecation.deprecated_argument_lookup("keepdims", keepdims, - "keep_dims", keep_dims) - if keepdims is None: - keepdims = False - return _may_reduce_to_scalar(keepdims, axis, reduction_indices, + return _may_reduce_to_scalar(keep_dims, axis, reduction_indices, gen_math_ops._all( input_tensor, _ReductionDims(input_tensor, axis, reduction_indices), - keepdims, + keep_dims, name=name)) -@deprecation.deprecated_args( - None, "keep_dims is deprecated, use keepdims instead", "keep_dims") def reduce_any(input_tensor, axis=None, - keepdims=None, + keep_dims=False, name=None, - reduction_indices=None, - keep_dims=None): + reduction_indices=None): """Computes the "logical or" of elements across dimensions of a tensor. Reduces `input_tensor` along the dimensions given in `axis`. - Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each - entry in `axis`. If `keepdims` is true, the reduced dimensions + Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each + entry in `axis`. If `keep_dims` is true, the reduced dimensions are retained with length 1. If `axis` has no entries, all dimensions are reduced, and a @@ -1710,10 +1632,9 @@ def reduce_any(input_tensor, axis: The dimensions to reduce. If `None` (the default), reduces all dimensions. Must be in the range `[-rank(input_tensor), rank(input_tensor))`. - keepdims: If true, retains reduced dimensions with length 1. + keep_dims: If true, retains reduced dimensions with length 1. name: A name for the operation (optional). reduction_indices: The old (deprecated) name for axis. - keep_dims: Deprecated alias for `keepdims`. Returns: The reduced tensor. @@ -1722,32 +1643,25 @@ def reduce_any(input_tensor, Equivalent to np.any @end_compatibility """ - keepdims = deprecation.deprecated_argument_lookup("keepdims", keepdims, - "keep_dims", keep_dims) - if keepdims is None: - keepdims = False - return _may_reduce_to_scalar(keepdims, axis, reduction_indices, + return _may_reduce_to_scalar(keep_dims, axis, reduction_indices, gen_math_ops._any( input_tensor, _ReductionDims(input_tensor, axis, reduction_indices), - keepdims, + keep_dims, name=name)) -@deprecation.deprecated_args( - None, "keep_dims is deprecated, use keepdims instead", "keep_dims") def reduce_logsumexp(input_tensor, axis=None, - keepdims=None, + keep_dims=False, name=None, - reduction_indices=None, - keep_dims=None): + reduction_indices=None): """Computes log(sum(exp(elements across dimensions of a tensor))). Reduces `input_tensor` along the dimensions given in `axis`. - Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each - entry in `axis`. If `keepdims` is true, the reduced dimensions + Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each + entry in `axis`. If `keep_dims` is true, the reduced dimensions are retained with length 1. If `axis` has no entries, all dimensions are reduced, and a @@ -1764,7 +1678,7 @@ def reduce_logsumexp(input_tensor, 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, keepdims=True) # [[log(3)], [log(3)]] + tf.reduce_logsumexp(x, 1, keep_dims=True) # [[log(3)], [log(3)]] tf.reduce_logsumexp(x, [0, 1]) # log(6) ``` @@ -1773,24 +1687,19 @@ def reduce_logsumexp(input_tensor, axis: The dimensions to reduce. If `None` (the default), reduces all dimensions. Must be in the range `[-rank(input_tensor), rank(input_tensor))`. - keepdims: If true, retains reduced dimensions with length 1. + keep_dims: If true, retains reduced dimensions with length 1. name: A name for the operation (optional). reduction_indices: The old (deprecated) name for axis. - keep_dims: Deprecated alias for `keepdims`. Returns: The reduced tensor. """ - keepdims = deprecation.deprecated_argument_lookup("keepdims", keepdims, - "keep_dims", keep_dims) - if keepdims is None: - keepdims = False with ops.name_scope(name, "ReduceLogSumExp", [input_tensor]) as name: raw_max = reduce_max( input_tensor, axis=axis, reduction_indices=reduction_indices, - keepdims=True) + keep_dims=True) my_max = array_ops.stop_gradient( array_ops.where( gen_math_ops.is_finite(raw_max), raw_max, @@ -1799,13 +1708,13 @@ def reduce_logsumexp(input_tensor, reduce_sum( gen_math_ops.exp(input_tensor - my_max), axis, - keepdims=True, + keep_dims=True, reduction_indices=reduction_indices)) + my_max - if not keepdims: + if not keep_dims: if isinstance(axis, int): axis = [axis] result = array_ops.squeeze(result, axis) - return _may_reduce_to_scalar(keepdims, axis, reduction_indices, result) + return _may_reduce_to_scalar(keep_dims, axis, reduction_indices, result) def trace(x, name=None): @@ -2307,10 +2216,9 @@ def bincount(arr, maxlength = ops.convert_to_tensor( maxlength, name="maxlength", dtype=dtypes.int32) output_size = gen_math_ops.minimum(maxlength, output_size) - if weights is not None: - weights = ops.convert_to_tensor(weights, name="weights") - return gen_math_ops.unsorted_segment_sum(weights, arr, output_size) - weights = constant_op.constant([], dtype) + weights = ( + ops.convert_to_tensor(weights, name="weights") + if weights is not None else constant_op.constant([], dtype)) return gen_math_ops.bincount(arr, output_size, weights) @@ -2473,7 +2381,7 @@ def reduced_shape(input_shape, axes): input_shape: 1-D Tensor, the shape of the Tensor being reduced. axes: 1-D Tensor, the reduction axes. Returns: - A 1-D Tensor, the output shape as if keepdims were set to True. + A 1-D Tensor, the output shape as if keep_dims were set to True. """ # Example: # cast needed for SparseTensor reductions |