add function and tensor not_equal
This commit is contained in:
parent
7d847b44b0
commit
e7d396ddcb
|
@ -294,6 +294,7 @@ Reduction函数
|
|||
mindspore.ops.maximum
|
||||
mindspore.ops.minimum
|
||||
mindspore.ops.ne
|
||||
mindspore.ops.not_equal
|
||||
|
||||
线性代数函数
|
||||
^^^^^^^^^^^^^
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
mindspore.Tensor.not_equal
|
||||
===========================
|
||||
|
||||
.. py:method:: mindspore.Tensor.not_equal(other)
|
||||
|
||||
详情请参考 :func:`mindspore.ops.not_equal`。
|
|
@ -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
|
||||
|
|
|
@ -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。
|
||||
|
|
|
@ -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`。
|
||||
|
|
|
@ -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。
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
mindspore.ops.not_equal
|
||||
========================
|
||||
|
||||
.. py:function:: mindspore.ops.not_equal(x, other)
|
||||
|
||||
ops.not_equal()的别名。
|
||||
详情请参考 :func:`mindspore.ops.not_equal`。
|
|
@ -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
|
||||
|
|
|
@ -294,6 +294,7 @@ Comparison Functions
|
|||
mindspore.ops.maximum
|
||||
mindspore.ops.minimum
|
||||
mindspore.ops.ne
|
||||
mindspore.ops.not_equal
|
||||
|
||||
Linear Algebraic Functions
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
|
@ -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)
|
Loading…
Reference in New Issue