From c4701eb9d47cd41845c97cefb1574190ab3d3ac7 Mon Sep 17 00:00:00 2001 From: huangmengxi Date: Fri, 5 Mar 2021 15:54:11 +0800 Subject: [PATCH] bug fix --- mindspore/numpy/array_creations.py | 2 +- mindspore/numpy/array_ops.py | 6 ++++-- mindspore/numpy/logic_ops.py | 13 +++++++++++-- mindspore/numpy/math_ops.py | 4 ++++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/mindspore/numpy/array_creations.py b/mindspore/numpy/array_creations.py index 4f4b85a2f9..082f1c13aa 100644 --- a/mindspore/numpy/array_creations.py +++ b/mindspore/numpy/array_creations.py @@ -1596,7 +1596,7 @@ def ix_(*args): Boolean masks are not supported. Args: - *args (Tensor): 1-D, each sequence should be of integer type. + *args (Tensor): 1-D sequences. Returns: Tuple of Tensor, `N` arrays with `N` dimensions each, with `N` the diff --git a/mindspore/numpy/array_ops.py b/mindspore/numpy/array_ops.py index 78d1cccce1..3c904b8ed8 100644 --- a/mindspore/numpy/array_ops.py +++ b/mindspore/numpy/array_ops.py @@ -1898,15 +1898,17 @@ def repeat(a, repeats, axis=None): [3 4]] """ _check_input_tensor(a) + if not isinstance(repeats, (tuple, list)): + repeats = (repeats,) + _check_element_int(repeats) if axis is None: a = ravel(a) axis = 0 ndim = F.rank(a) _check_axis_in_range(axis, ndim) axis = axis + ndim if axis < 0 else axis - if isinstance(repeats, (tuple, list)) and len(repeats) == 1: + if len(repeats) == 1: repeats = repeats[0] - if isinstance(repeats, int): if repeats == 0: return _empty(F.dtype(a), (0,)) return C.repeat_elements(a, repeats, axis) diff --git a/mindspore/numpy/logic_ops.py b/mindspore/numpy/logic_ops.py index 754dca4432..fc412e8eff 100644 --- a/mindspore/numpy/logic_ops.py +++ b/mindspore/numpy/logic_ops.py @@ -17,7 +17,9 @@ from .math_ops import _apply_tensor_op from ..ops import functional as F +from ..ops.primitive import constexpr from ..common import dtype as mstype +from ..common import Tensor from .._c_expression import typing from .array_creations import zeros, ones @@ -530,6 +532,13 @@ def isneginf(x): return _is_sign_inf(x, F.tensor_lt) +@constexpr +def _isscalar(x): + """Returns True if x is a scalar type""" + return isinstance(x, (typing.Number, typing.Int, typing.UInt, typing.Float, + typing.Bool, typing.String)) + + def isscalar(element): """ Returns True if the type of element is a scalar type. @@ -565,5 +574,5 @@ def isscalar(element): >>> print(output) True """ - return isinstance(F.typeof(element), (typing.Number, typing.Int, typing.UInt, - typing.Float, typing.Bool, typing.String)) + obj_type = F.typeof(element) + return not isinstance(obj_type, Tensor) and _isscalar(obj_type) diff --git a/mindspore/numpy/math_ops.py b/mindspore/numpy/math_ops.py index 78a629d047..5ac989026a 100644 --- a/mindspore/numpy/math_ops.py +++ b/mindspore/numpy/math_ops.py @@ -2237,6 +2237,10 @@ def _reduce(a, reduce_fn, cmp_fn, axis=None, keepdims=False, initial=None, where ndim = F.rank(a) dtype = F.dtype(a) axes = _check_axis_valid(axis, ndim) + if initial is not None: + if ((isinstance(initial, Tensor) and F.rank(initial) > 0) or + not isinstance(initial, (int, float, bool, Tensor))): + _raise_type_error('initial should be scalar') if _is_shape_empty(shape): if not axes: