diff --git a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.mean.rst b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.mean.rst index c5df83fd0e4..6e3e296dc69 100644 --- a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.mean.rst +++ b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.mean.rst @@ -1,6 +1,6 @@ mindspore.Tensor.mean ===================== -.. py:method:: mindspore.Tensor.mean(axis=(), keep_dims=False) +.. py:method:: mindspore.Tensor.mean(axis=None, keep_dims=False) 详情请参考 :func:`mindspore.ops.mean`。 diff --git a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.prod.rst b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.prod.rst index 66478376e1a..e9e5584a436 100644 --- a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.prod.rst +++ b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.prod.rst @@ -1,6 +1,6 @@ mindspore.Tensor.prod ===================== -.. py:method:: mindspore.Tensor.prod(axis=(), keep_dims=False) +.. py:method:: mindspore.Tensor.prod(axis=None, keep_dims=False) 详情请参考 :func:`mindspore.ops.prod`。 diff --git a/docs/api/api_python/ops/mindspore.ops.func_mean.rst b/docs/api/api_python/ops/mindspore.ops.func_mean.rst index fd4b3759c1a..d6de0b49218 100644 --- a/docs/api/api_python/ops/mindspore.ops.func_mean.rst +++ b/docs/api/api_python/ops/mindspore.ops.func_mean.rst @@ -1,19 +1,19 @@ mindspore.ops.mean ================== -.. py:function:: mindspore.ops.mean(x, axis=(), keep_dims=False) +.. py:function:: mindspore.ops.mean(x, axis=None, keep_dims=False) 默认情况下,移除输入所有维度,返回 `x` 中所有元素的平均值。也可仅缩小指定维度 `axis` 大小至1。 `keep_dims` 控制输出和输入的维度是否相同。 参数: - **x** (Tensor[Number]) - 输入Tensor,其数据类型为数值型。shape: :math:`(N, *)` ,其中 :math:`*` 表示任意数量的附加维度。秩应小于8。 - - **axis** (Union[int, tuple(int), list(int)]) - 要减少的维度。默认值: (),缩小所有维度。只允许常量值。假设 `x` 的秩为r,取值范围[-r,r)。 + - **axis** (Union[int, tuple(int), list(int)]) - 要减少的维度。默认值: None,缩小所有维度。只允许常量值。假设 `x` 的秩为r,取值范围[-r,r)。 - **keep_dims** (bool) - 如果为True,则保留缩小的维度,大小为1。否则移除维度。默认值:False。 返回: Tensor。 - - 如果 `axis` 为(),且 `keep_dims` 为False,则输出一个零维Tensor,表示输入Tensor中所有元素的平均值。 + - 如果 `axis` 为None,且 `keep_dims` 为False,则输出一个零维Tensor,表示输入Tensor中所有元素的平均值。 - 如果 `axis` 为int,取值为1,并且 `keep_dims` 为False,则输出的shape为 :math:`(x_0, x_2, ..., x_R)` 。 - 如果 `axis` 为tuple(int)或list(int),取值为(1, 2),并且 `keep_dims` 为False,则输出Tensor的shape为 :math:`(x_0, x_3, ..., x_R)` 。 diff --git a/docs/api/api_python/ops/mindspore.ops.func_prod.rst b/docs/api/api_python/ops/mindspore.ops.func_prod.rst index e832cdd2e71..ca771be8d7a 100644 --- a/docs/api/api_python/ops/mindspore.ops.func_prod.rst +++ b/docs/api/api_python/ops/mindspore.ops.func_prod.rst @@ -1,19 +1,19 @@ mindspore.ops.prod ================== -.. py:function:: mindspore.ops.prod(x, axis=(), keep_dims=False) +.. py:function:: mindspore.ops.prod(x, axis=None, keep_dims=False) 默认情况下,使用指定维度的所有元素的乘积代替该维度的其他元素,以移除该维度。也可仅缩小该维度大小至1。 `keep_dims` 控制输出和输入的维度是否相同。 参数: - **x** (Tensor[Number]) - 输入Tensor,其数据类型为数值型。shape: :math:`(N, *)` ,其中 :math:`*` 表示任意数量的附加维度。秩应小于8。 - - **axis** (Union[int, tuple(int), list(int)]) - 要减少的维度。默认值: (),缩小所有维度。只允许常量值。假设 `x` 的秩为r,取值范围[-r,r)。 + - **axis** (Union[int, tuple(int), list(int)]) - 要减少的维度。默认值: None,缩小所有维度。只允许常量值。假设 `x` 的秩为r,取值范围[-r,r)。 - **keep_dims** (bool) - 如果为True,则保留缩小的维度,大小为1。否则移除维度。默认值:False。 返回: Tensor。 - - 如果 `axis` 为(),且 `keep_dims` 为False,则输出一个零维Tensor,表示输入Tensor中所有元素的乘积。 + - 如果 `axis` 为None,且 `keep_dims` 为False,则输出一个零维Tensor,表示输入Tensor中所有元素的乘积。 - 如果 `axis` 为int,取值为1,并且 `keep_dims` 为False,则输出的shape为 :math:`(x_0, x_2, ..., x_R)` 。 - 如果 `axis` 为tuple(int)或list(int),取值为(1, 2),并且 `keep_dims` 为False,则输出Tensor的shape为 :math:`(x_0, x_3, ..., x_R)` 。 diff --git a/mindspore/python/mindspore/_extends/parse/standard_method.py b/mindspore/python/mindspore/_extends/parse/standard_method.py index b9ff2bd65bc..62165272b85 100644 --- a/mindspore/python/mindspore/_extends/parse/standard_method.py +++ b/mindspore/python/mindspore/_extends/parse/standard_method.py @@ -68,7 +68,7 @@ itemsize_map = {mstype.bool_: 1, mstype.int8: 1, mstype.uint8: 1, nan_tensor = Tensor(float('nan'), dtype=mstype.float32) -def mean(x, axis=(), keep_dims=False): +def mean(x, axis=None, keep_dims=False): """ Reduces a dimension of a tensor by averaging all elements in the dimension. @@ -102,14 +102,14 @@ def ndimension(x): return len(x.shape) -def prod(x, axis=(), keep_dims=False): +def prod(x, axis=None, keep_dims=False): """ Reduces a dimension of a tensor by product all elements in the dimension. Args: x (Tensor): Input Tensor. axis (Union[None, int, tuple(int), list(int)]): Dimensions of reduction, - when axis is None or empty tuple, reduce all dimensions. Default: (). + when axis is None or empty tuple, reduce all dimensions. Default: None. keep_dims (bool): Whether to keep the reduced dimensions. Default: False. Returns: diff --git a/mindspore/python/mindspore/common/tensor.py b/mindspore/python/mindspore/common/tensor.py index 676ab3b5738..4536a0a2f15 100644 --- a/mindspore/python/mindspore/common/tensor.py +++ b/mindspore/python/mindspore/common/tensor.py @@ -1423,7 +1423,7 @@ class Tensor(Tensor_, metaclass=_TensorMeta): self._init_check() return tensor_operator_registry.get('log2')(self) - def mean(self, axis=(), keep_dims=False): + def mean(self, axis=None, keep_dims=False): """ For details, please refer to :func:`mindspore.ops.mean`. """ @@ -1437,8 +1437,6 @@ class Tensor(Tensor_, metaclass=_TensorMeta): For details, please refer to :func:`mindspore.ops.amin`. """ self._init_check() - if axis is None: - axis = () return tensor_operator_registry.get('amin')(self, axis, keep_dims) def reverse(self, axis): @@ -1453,8 +1451,6 @@ class Tensor(Tensor_, metaclass=_TensorMeta): For details, please refer to :func:`mindspore.ops.amax`. """ self._init_check() - if axis is None: - axis = () return tensor_operator_registry.get('amax')(self, axis, keep_dims) def reverse_sequence(self, seq_lengths, seq_dim=0, batch_dim=0): @@ -1464,7 +1460,7 @@ class Tensor(Tensor_, metaclass=_TensorMeta): self._init_check() return tensor_operator_registry.get("reverse_sequence")(seq_dim, batch_dim)(self, seq_lengths) - def prod(self, axis=(), keep_dims=False): + def prod(self, axis=None, keep_dims=False): """ For details, please refer to :func:`mindspore.ops.prod`. """ diff --git a/mindspore/python/mindspore/ops/function/math_func.py b/mindspore/python/mindspore/ops/function/math_func.py index a7c33132053..552aa4389cb 100644 --- a/mindspore/python/mindspore/ops/function/math_func.py +++ b/mindspore/python/mindspore/ops/function/math_func.py @@ -7132,7 +7132,7 @@ def amax(input, axis=None, keep_dims=False): return _get_cache_prim(P.ReduceMax)(keep_dims)(input, axis) -def mean(x, axis=(), keep_dims=False): +def mean(x, axis=None, keep_dims=False): r""" Reduces all dimension of a tensor by averaging all elements in the dimension, by default. And reduce a dimension of `x` along the specified `axis`. `keep_dims` @@ -7141,7 +7141,7 @@ def mean(x, axis=(), keep_dims=False): Args: x (Tensor[Number]): The input tensor. The dtype of the tensor to be reduced is number. :math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8. - axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Default: (), reduce all dimensions. + axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Default: None, reduce all dimensions. Only constant value is allowed. Assume the rank of `x` is r, and the value range is [-r,r). keep_dims (bool): If true, keep these reduced dimensions and the length is 1. If false, don't keep these dimensions. Default: False. @@ -7149,7 +7149,7 @@ def mean(x, axis=(), keep_dims=False): Returns: Tensor, has the same data type as input tensor. - - If `axis` is (), and `keep_dims` is False, + - If `axis` is None, and `keep_dims` is False, the output is a 0-D tensor representing the product of all elements in the input tensor. - If `axis` is int, set as 1, and `keep_dims` is False, the shape of output is :math:`(x_0, x_2, ..., x_R)`. @@ -7206,11 +7206,12 @@ def mean(x, axis=(), keep_dims=False): [ 8.] [10.]]] """ - + if axis is None: + axis = () return _get_cache_prim(P.ReduceMean)(keep_dims)(x, axis) -def prod(x, axis=(), keep_dims=False): +def prod(x, axis=None, keep_dims=False): r""" Reduces a dimension of a tensor by multiplying all elements in the dimension, by default. And also can reduce a dimension of `x` along the axis. Determine whether the dimensions of the output and input are the same by @@ -7219,7 +7220,7 @@ def prod(x, axis=(), keep_dims=False): Args: x (Tensor[Number]): The input tensor. The dtype of the tensor to be reduced is number. :math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8. - axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Default: (), reduce all dimensions. + axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Default: None, reduce all dimensions. Only constant value is allowed. Assume the rank of `x` is r, and the value range is [-r,r). keep_dims (bool): If true, keep these reduced dimensions and the length is 1. If false, don't keep these dimensions. Default: False. @@ -7227,7 +7228,7 @@ def prod(x, axis=(), keep_dims=False): Returns: Tensor, has the same data type as input tensor. - - If `axis` is (), and `keep_dims` is False, + - If `axis` is None, and `keep_dims` is False, the output is a 0-D tensor representing the product of all elements in the input tensor. - If `axis` is int, set as 1, and `keep_dims` is False, the shape of output is :math:`(x_0, x_2, ..., x_R)`. @@ -7283,6 +7284,8 @@ def prod(x, axis=(), keep_dims=False): [2.62144e+05] [5.31441e+05]]] """ + if axis is None: + axis = () return _get_cache_prim(P.ReduceProd)(keep_dims)(x, axis)