diff --git a/docs/api/api_python/mindspore.ops.rst b/docs/api/api_python/mindspore.ops.rst index 07a6244ff77..3c164a98b52 100644 --- a/docs/api/api_python/mindspore.ops.rst +++ b/docs/api/api_python/mindspore.ops.rst @@ -294,6 +294,7 @@ Reduction函数 mindspore.ops.maximum mindspore.ops.minimum mindspore.ops.ne + mindspore.ops.not_equal 线性代数函数 ^^^^^^^^^^^^^ diff --git a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.not_equal.rst b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.not_equal.rst new file mode 100644 index 00000000000..9341fa297fd --- /dev/null +++ b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.not_equal.rst @@ -0,0 +1,6 @@ +mindspore.Tensor.not_equal +=========================== + +.. py:method:: mindspore.Tensor.not_equal(other) + + 详情请参考 :func:`mindspore.ops.not_equal`。 diff --git a/docs/api/api_python/mindspore/mindspore.Tensor.rst b/docs/api/api_python/mindspore/mindspore.Tensor.rst index a3d7c534ed6..0e37ef3a79a 100644 --- a/docs/api/api_python/mindspore/mindspore.Tensor.rst +++ b/docs/api/api_python/mindspore/mindspore.Tensor.rst @@ -182,6 +182,7 @@ mindspore.Tensor mindspore.Tensor.numel mindspore.Tensor.nonzero mindspore.Tensor.norm + mindspore.Tensor.not_equal mindspore.Tensor.permute mindspore.Tensor.positive mindspore.Tensor.pow diff --git a/docs/api/api_python/ops/mindspore.ops.func_clamp.rst b/docs/api/api_python/ops/mindspore.ops.func_clamp.rst index 332ddbc499a..bfaca6cc188 100644 --- a/docs/api/api_python/ops/mindspore.ops.func_clamp.rst +++ b/docs/api/api_python/ops/mindspore.ops.func_clamp.rst @@ -20,7 +20,7 @@ mindspore.ops.clamp - 当 `min` 为None,`max` 不为None时,Tensor中大于 `max` 的元素会变为 `max`; - 当 `min` 不为None,`max` 为None时,Tensor中小于 `min` 的元素会变为 `min`; - 当 `min` 大于 `max` 时,Tensor中所有元素的值会被置为 `max`; - - :math:`x` , `min` 和 `max` 的数据类型需支持隐式类型转换,且不能为布尔型。 + - `x`,`min` 和 `max` 的数据类型需支持隐式类型转换,且不能为布尔型。 参数: - **x** (Union(Tensor, list[Tensor], tuple[Tensor])) - `clamp` 的输入,类型为Tensor、Tensor的列表或元组。支持任意维度的Tensor。 diff --git a/docs/api/api_python/ops/mindspore.ops.func_clip.rst b/docs/api/api_python/ops/mindspore.ops.func_clip.rst index 7c8464c82c1..2da469a6d63 100644 --- a/docs/api/api_python/ops/mindspore.ops.func_clip.rst +++ b/docs/api/api_python/ops/mindspore.ops.func_clip.rst @@ -3,4 +3,5 @@ mindspore.ops.clip .. py:function:: mindspore.ops.clip(x, min=None, max=None) - :func:`mindspore.ops.clamp` 的别名。 + ops.clamp()的别名。 + 详情请参考 :func:`mindspore.ops.clamp`。 diff --git a/docs/api/api_python/ops/mindspore.ops.func_clip_by_value.rst b/docs/api/api_python/ops/mindspore.ops.func_clip_by_value.rst index e25dc94327b..35839afec42 100644 --- a/docs/api/api_python/ops/mindspore.ops.func_clip_by_value.rst +++ b/docs/api/api_python/ops/mindspore.ops.func_clip_by_value.rst @@ -20,7 +20,7 @@ - 当 `clip_value_min` 为None,`clip_value_max` 不为None时,Tensor中大于 `clip_value_max` 的元素会变为 `clip_value_max`; - 当 `clip_value_min` 不为None,`clip_value_max` 为None时,Tensor中小于 `clip_value_min` 的元素会变为 `clip_value_min`; - 当 `clip_value_min` 大于 `clip_value_max` 时,Tensor中所有元素的值会被置为 `clip_value_max`; - - :math:`x` , `clip_value_min` 和 `clip_value_max` 的数据类型需支持隐式类型转换,且不能为布尔型。 + - `x`,`clip_value_min` 和 `clip_value_max` 的数据类型需支持隐式类型转换,且不能为布尔型。 参数: - **x** (Union(Tensor, list[Tensor], tuple[Tensor])) - `clip_by_value` 的输入,类型为Tensor、Tensor的列表或元组。支持任意维度的Tensor。 diff --git a/docs/api/api_python/ops/mindspore.ops.func_not_equal.rst b/docs/api/api_python/ops/mindspore.ops.func_not_equal.rst new file mode 100644 index 00000000000..1da5ef9e9bc --- /dev/null +++ b/docs/api/api_python/ops/mindspore.ops.func_not_equal.rst @@ -0,0 +1,7 @@ +mindspore.ops.not_equal +======================== + +.. py:function:: mindspore.ops.not_equal(x, other) + + ops.not_equal()的别名。 + 详情请参考 :func:`mindspore.ops.not_equal`。 diff --git a/docs/api/api_python_en/Tensor_list.rst b/docs/api/api_python_en/Tensor_list.rst index 23be2f76c93..14636e10d9c 100644 --- a/docs/api/api_python_en/Tensor_list.rst +++ b/docs/api/api_python_en/Tensor_list.rst @@ -188,6 +188,7 @@ mindspore.Tensor.numel mindspore.Tensor.nonzero mindspore.Tensor.norm + mindspore.Tensor.not_equal mindspore.Tensor.permute mindspore.Tensor.positive mindspore.Tensor.pow diff --git a/docs/api/api_python_en/mindspore.ops.rst b/docs/api/api_python_en/mindspore.ops.rst index 0786c174f5c..8b12aa3880a 100644 --- a/docs/api/api_python_en/mindspore.ops.rst +++ b/docs/api/api_python_en/mindspore.ops.rst @@ -294,6 +294,7 @@ Comparison Functions mindspore.ops.maximum mindspore.ops.minimum mindspore.ops.ne + mindspore.ops.not_equal Linear Algebraic Functions ^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/mindspore/ccsrc/pipeline/jit/resource.cc b/mindspore/ccsrc/pipeline/jit/resource.cc index 1e6d523eaf3..fa8055d7d35 100644 --- a/mindspore/ccsrc/pipeline/jit/resource.cc +++ b/mindspore/ccsrc/pipeline/jit/resource.cc @@ -411,6 +411,7 @@ BuiltInTypeMap &GetMethodMap() { {"nan_to_num", std::string("nan_to_num")}, // nan_to_num() {"neg", std::string("neg")}, // neg() {"ne", std::string("ne")}, // ne() + {"not_equal", std::string("not_equal")}, // not_equal() {"sinh", std::string("sinh")}, // sinh() {"sort", std::string("sort")}, // sort() {"trunc", std::string("trunc")}, // trunc() diff --git a/mindspore/python/mindspore/_extends/parse/standard_method.py b/mindspore/python/mindspore/_extends/parse/standard_method.py index 00dd5dce7cc..22014690290 100644 --- a/mindspore/python/mindspore/_extends/parse/standard_method.py +++ b/mindspore/python/mindspore/_extends/parse/standard_method.py @@ -3912,6 +3912,13 @@ def ne(input, other): return F.ne(input, other) +def not_equal(x, other): + r""" + Computes the non-equivalence of two tensors element-wise. + """ + return F.not_equal(x, other) + + def sinh(input): r""" Computes hyperbolic sine of the input element-wise. diff --git a/mindspore/python/mindspore/common/tensor.py b/mindspore/python/mindspore/common/tensor.py index 4de8490e120..11c42fbec10 100644 --- a/mindspore/python/mindspore/common/tensor.py +++ b/mindspore/python/mindspore/common/tensor.py @@ -4359,6 +4359,13 @@ class Tensor(Tensor_): self._init_check() return tensor_operator_registry.get('ne')(self, other) + def not_equal(self, other): + r""" + For details, please refer to :func:`mindspore.ops.not_equal`. + """ + self._init_check() + return tensor_operator_registry.get('not_equal')(self, other) + def sinh(self): r""" Computes hyperbolic sine of the input element-wise. diff --git a/mindspore/python/mindspore/ops/function/clip_func.py b/mindspore/python/mindspore/ops/function/clip_func.py index d23895f8a64..835425c33dc 100644 --- a/mindspore/python/mindspore/ops/function/clip_func.py +++ b/mindspore/python/mindspore/ops/function/clip_func.py @@ -206,8 +206,11 @@ def clamp(x, min=None, max=None): def clip(x, min=None, max=None): - """ + r""" Alias for ops.clamp. For details, please refer to :func:`mindspore.ops.clamp`. + + Supported Platforms: + ``Ascend`` ``GPU`` ``CPU`` """ return clamp(x, min, max) diff --git a/mindspore/python/mindspore/ops/function/math_func.py b/mindspore/python/mindspore/ops/function/math_func.py index 1e4b6742522..f59a2b1690b 100644 --- a/mindspore/python/mindspore/ops/function/math_func.py +++ b/mindspore/python/mindspore/ops/function/math_func.py @@ -108,7 +108,7 @@ tensor_lt = P.Less() tensor_le = P.LessEqual() tensor_gt = P.Greater() tensor_ge = P.GreaterEqual() -not_equal = P.NotEqual() +not_equal_ = P.NotEqual() size_ = P.Size() transpose_ = P.Transpose() @@ -3106,7 +3106,18 @@ def ne(x, y): >>> print(output) [False False True] """ - return not_equal(x, y) + return not_equal_(x, y) + + +def not_equal(x, other): + r""" + Alias for ops.ne. + For details, please refer to :func:`mindspore.ops.ne`. + + Supported Platforms: + ``Ascend`` ``GPU`` ``CPU`` + """ + return ne(x, other) def approximate_equal(x, y, tolerance=1e-5): diff --git a/mindspore/python/mindspore/ops/functional.py b/mindspore/python/mindspore/ops/functional.py index f1fff4756a1..fc0dc43f774 100644 --- a/mindspore/python/mindspore/ops/functional.py +++ b/mindspore/python/mindspore/ops/functional.py @@ -354,6 +354,7 @@ tensor_operator_registry.register('mul', mul) tensor_operator_registry.register('multiply', multiply) tensor_operator_registry.register('neg', neg) tensor_operator_registry.register('ne', ne) +tensor_operator_registry.register('not_equal', not_equal) tensor_operator_registry.register('sinh', sinh) tensor_operator_registry.register('sort', P.Sort) tensor_operator_registry.register('trunc', trunc) diff --git a/tests/st/ops/test_ops_not_equal.py b/tests/st/ops/test_ops_not_equal.py new file mode 100644 index 00000000000..be5017cb3fc --- /dev/null +++ b/tests/st/ops/test_ops_not_equal.py @@ -0,0 +1,53 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +import numpy as np +import pytest + +import mindspore as ms +import mindspore.nn as nn +from mindspore import Tensor, ops + + +class Net(nn.Cell): + def construct(self, x, other): + return ops.not_equal(x, other) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.platform_arm_cpu +@pytest.mark.platform_x86_gpu_training +@pytest.mark.platform_arm_ascend_training +@pytest.mark.platform_x86_ascend_training +@pytest.mark.env_onecard +@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE]) +def test_ops_not_equal(mode): + """ + Feature: ops.not_equal + Description: Verify the result of ops.not_equal + Expectation: success + """ + ms.set_context(mode=mode) + x_np = np.array([1, 2, 3]).astype(np.float32) + y_np = np.array([1, 2, 4]).astype(np.float32) + x = Tensor(x_np, ms.float32) + y = Tensor(y_np, ms.float32) + net = Net() + output_ms_case_1 = net(x, 2.0) + expect_output_case_1 = np.not_equal(x_np, 2.0) + output_ms_case_2 = net(x, y) + expect_output_case_2 = np.not_equal(x_np, y_np) + np.testing.assert_array_equal(output_ms_case_1.asnumpy(), expect_output_case_1) + np.testing.assert_array_equal(output_ms_case_2.asnumpy(), expect_output_case_2) diff --git a/tests/st/tensor/test_not_equal.py b/tests/st/tensor/test_not_equal.py new file mode 100644 index 00000000000..2aec471e962 --- /dev/null +++ b/tests/st/tensor/test_not_equal.py @@ -0,0 +1,54 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +import numpy as np +import pytest + +import mindspore as ms +import mindspore.nn as nn +from mindspore import Tensor + + +class NotEqualNet(nn.Cell): + def construct(self, x, other): + return x.not_equal(other) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.platform_arm_cpu +@pytest.mark.platform_x86_gpu_training +@pytest.mark.platform_arm_ascend_training +@pytest.mark.platform_x86_ascend_training +@pytest.mark.env_onecard +@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE]) +def test_not_equal(mode): + """ + Feature: test Tensor.not_equal + Description: Verify the result of Tensor.not_equal + Expectation: expect correct forward result + """ + ms.set_context(mode=mode) + x_np = np.array([1, 2, 3]).astype(np.float32) + y_np = np.array([1, 2, 4]).astype(np.float32) + x = Tensor(x_np, ms.float32) + y = Tensor(y_np, ms.float32) + net = NotEqualNet() + output_ms_case_1 = net(x, 2.0) + expect_output_case_1 = np.not_equal(x_np, 2.0) + output_ms_case_2 = net(x, y) + expect_output_case_2 = np.not_equal(x_np, y_np) + np.testing.assert_array_equal(output_ms_case_1.asnumpy(), expect_output_case_1) + np.testing.assert_array_equal(output_ms_case_2.asnumpy(), expect_output_case_2)