From 373c878118a0d74c15ac9fc67c4214138447c13e Mon Sep 17 00:00:00 2001 From: yide12 Date: Sat, 7 Jan 2023 14:54:35 +0800 Subject: [PATCH] add_raise --- .../api_python/mindspore/Tensor/mindspore.Tensor.view_as.rst | 3 +++ mindspore/python/mindspore/_extends/parse/standard_method.py | 2 ++ mindspore/python/mindspore/common/tensor.py | 5 +++++ mindspore/python/mindspore/ops/function/math_func.py | 4 ++++ 4 files changed, 14 insertions(+) diff --git a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.view_as.rst b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.view_as.rst index 735db49a103..2a9ac7fb221 100644 --- a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.view_as.rst +++ b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.view_as.rst @@ -10,3 +10,6 @@ mindspore.Tensor.view_as 返回: Tensor,和 `other` 具有相同的shape。 + + 异常: + - **TypeError** - `other` 不是Tensor。 diff --git a/mindspore/python/mindspore/_extends/parse/standard_method.py b/mindspore/python/mindspore/_extends/parse/standard_method.py index cefc9537422..39d86cd27f1 100644 --- a/mindspore/python/mindspore/_extends/parse/standard_method.py +++ b/mindspore/python/mindspore/_extends/parse/standard_method.py @@ -2778,6 +2778,8 @@ def view(x, *shape): def view_as(x, other): """View self Tensor as the same shape as `other` .""" + if not isinstance(other, (Tensor, Tensor_)): + raise TypeError(f"For view_as, the input other must be a Tensor, but got {type(other)}") return F.reshape(x, other.shape) diff --git a/mindspore/python/mindspore/common/tensor.py b/mindspore/python/mindspore/common/tensor.py index 5344b9ecb99..a4149129a3f 100644 --- a/mindspore/python/mindspore/common/tensor.py +++ b/mindspore/python/mindspore/common/tensor.py @@ -1032,6 +1032,9 @@ class Tensor(Tensor_): Returns: Tensor, has the same shape as `other`. + Raises: + TypeError: If `other` is not a Tensor. + Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` @@ -1043,6 +1046,8 @@ class Tensor(Tensor_): [1. 2. 3. 2. 3. 4.] """ self._init_check() + if not isinstance(other, (Tensor, Tensor_)): + raise TypeError(f"For view_as, the input other must be a Tensor, but got {type(other)}") return self.view(other.shape) def t(self): diff --git a/mindspore/python/mindspore/ops/function/math_func.py b/mindspore/python/mindspore/ops/function/math_func.py index 048e0f460e7..a81e8df9b26 100644 --- a/mindspore/python/mindspore/ops/function/math_func.py +++ b/mindspore/python/mindspore/ops/function/math_func.py @@ -9283,6 +9283,8 @@ def isposinf(x): >>> print(output) [False True False] """ + if not isinstance(x, (Tensor, Tensor_)): + raise TypeError(f"For isposinf, the input x must be a Tensor, but got {type(x)}") return _is_sign_inf(x, tensor_gt) @@ -9307,6 +9309,8 @@ def isneginf(x): >>> print(output) [ True False False] """ + if not isinstance(x, (Tensor, Tensor_)): + raise TypeError(f"For isneginf, the input x must be a Tensor, but got {type(x)}") return _is_sign_inf(x, tensor_lt)