From 6eb55385c1851f3db6f3dbbbcd3132622a1655d9 Mon Sep 17 00:00:00 2001 From: yide12 Date: Wed, 9 Nov 2022 10:21:40 +0800 Subject: [PATCH] add_negative_ops --- .../api/api_python/mindspore.ops.function.rst | 1 + .../ops/mindspore.ops.func_negative.rst | 8 ++++ .../ops/mindspore.ops.func_permute.rst | 1 + .../api_python_en/mindspore.ops.function.rst | 1 + .../python/mindspore/ops/function/__init__.py | 1 + .../mindspore/ops/function/math_func.py | 13 ++++- tests/st/ops/test_ops_negative.py | 47 +++++++++++++++++++ 7 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 docs/api/api_python/ops/mindspore.ops.func_negative.rst create mode 100644 tests/st/ops/test_ops_negative.py diff --git a/docs/api/api_python/mindspore.ops.function.rst b/docs/api/api_python/mindspore.ops.function.rst index fc0846b1ea0..9f098f136a0 100644 --- a/docs/api/api_python/mindspore.ops.function.rst +++ b/docs/api/api_python/mindspore.ops.function.rst @@ -202,6 +202,7 @@ mindspore.ops.function mindspore.ops.matrix_determinant mindspore.ops.mul mindspore.ops.neg + mindspore.ops.negative mindspore.ops.positive mindspore.ops.pow mindspore.ops.roll diff --git a/docs/api/api_python/ops/mindspore.ops.func_negative.rst b/docs/api/api_python/ops/mindspore.ops.func_negative.rst new file mode 100644 index 00000000000..35839663c41 --- /dev/null +++ b/docs/api/api_python/ops/mindspore.ops.func_negative.rst @@ -0,0 +1,8 @@ +mindspore.ops.negative +======================= + +.. py:function:: mindspore.ops.negative(x) + + ops.neg()的别名。 + + 详情请参考 :func:`mindspore.ops.neg`。 diff --git a/docs/api/api_python/ops/mindspore.ops.func_permute.rst b/docs/api/api_python/ops/mindspore.ops.func_permute.rst index 566877ff8e4..fa65eaa1f40 100644 --- a/docs/api/api_python/ops/mindspore.ops.func_permute.rst +++ b/docs/api/api_python/ops/mindspore.ops.func_permute.rst @@ -13,4 +13,5 @@ mindspore.ops.permute Tensor,具有和输入Tensor相同的维数,按照 `dims` 重新排列。 异常: + - **ValueError** - `dims` 为None。 - **ValueError** - `dims` 的元素总量不等于 `x` 的维数。 diff --git a/docs/api/api_python_en/mindspore.ops.function.rst b/docs/api/api_python_en/mindspore.ops.function.rst index 0cc4e39b291..12eff5121f5 100644 --- a/docs/api/api_python_en/mindspore.ops.function.rst +++ b/docs/api/api_python_en/mindspore.ops.function.rst @@ -203,6 +203,7 @@ Element-by-Element Operations mindspore.ops.matrix_determinant mindspore.ops.mul mindspore.ops.neg + mindspore.ops.negative mindspore.ops.positive mindspore.ops.pow mindspore.ops.roll diff --git a/mindspore/python/mindspore/ops/function/__init__.py b/mindspore/python/mindspore/ops/function/__init__.py index 5392c0ae1e0..cc8d0522a2c 100644 --- a/mindspore/python/mindspore/ops/function/__init__.py +++ b/mindspore/python/mindspore/ops/function/__init__.py @@ -141,6 +141,7 @@ from .math_func import ( addcmul, neg_tensor, neg, + negative, tensor_lt, less, tensor_le, diff --git a/mindspore/python/mindspore/ops/function/math_func.py b/mindspore/python/mindspore/ops/function/math_func.py index cfd8fbd16f6..b47ba4d8780 100644 --- a/mindspore/python/mindspore/ops/function/math_func.py +++ b/mindspore/python/mindspore/ops/function/math_func.py @@ -467,6 +467,14 @@ def neg(x): return neg_tensor(x) +def negative(x): + r""" + Alias for ops.neg(). + For details, please refer to :func:`mindspore.ops.neg`. + """ + return neg_tensor(x) + + def positive(x): r""" Return self Tensor. @@ -528,8 +536,8 @@ def permute(x, dims): Tensor, has the same dimension as input tensor, with `dims` suitably permuted. Raises: - ValueError: If `dims` is none. - ValueError: If the number of `dims` is not equal to Tensor's ndim. + ValueError: If `dims` is None. + ValueError: If the number of elements of `dims` is not equal to `x` ndim. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` @@ -7022,6 +7030,7 @@ __all__ = [ 'arctan2', 'neg_tensor', 'neg', + 'negative', 'tensor_lt', 'less', 'logaddexp2', diff --git a/tests/st/ops/test_ops_negative.py b/tests/st/ops/test_ops_negative.py new file mode 100644 index 00000000000..ebfe00e93f2 --- /dev/null +++ b/tests/st/ops/test_ops_negative.py @@ -0,0 +1,47 @@ +# 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): + return ops.negative(x) + + +@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_negative(mode): + """ + Feature: ops.negative + Description: Verify the result of negative + Expectation: success + """ + ms.set_context(mode=mode) + x = Tensor(np.array([-5.0, 1.5, 3.0, 100.0]), ms.float32) + net = Net() + output = net(x) + expect_output = [5.0, -1.5, -3.0, -100.0] + assert np.allclose(output.asnumpy(), expect_output)