add marginrankingloss ops

This commit is contained in:
fengyihang 2022-11-07 10:42:50 +08:00
parent 424c0b69d9
commit 87a22de9a5
12 changed files with 201 additions and 62 deletions

View File

@ -36,6 +36,7 @@ mindspore.ops.function
mindspore.ops.lp_pool1d
mindspore.ops.lp_pool2d
mindspore.ops.lrn
mindspore.ops.margin_ranking_loss
mindspore.ops.max_pool3d
mindspore.ops.max_unpool1d
mindspore.ops.max_unpool2d

View File

@ -3,4 +3,6 @@ mindspore.Tensor.arcsinh
.. py:method:: mindspore.Tensor.arcsinh()
参考 `Tensor.asinh() <https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore/Tensor/mindspore.Tensor.asinh.html>`_
Tensor.asinh()的别名。
详情请参考 :func:`mindspore.ops.asinh`

View File

@ -3,4 +3,6 @@ mindspore.Tensor.arctanh
.. py:method:: mindspore.Tensor.arctanh()
参考 `Tensor.atanh() <https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore/Tensor/mindspore.Tensor.atanh.html>`_
Tensor.atanh()的别名。
详情请参考 :func:`mindspore.ops.atanh`

View File

@ -0,0 +1,6 @@
mindspore.ops.margin_ranking_loss
==================================
.. py:function:: mindspore.ops.margin_ranking_loss(input1, input2, target, margin=0.0, reduction='mean')
详情请参考 :class:`mindspore.nn.MarginRankingLoss`

View File

@ -36,6 +36,7 @@ Neural Network
mindspore.ops.lp_pool1d
mindspore.ops.lp_pool2d
mindspore.ops.lrn
mindspore.ops.margin_ranking_loss
mindspore.ops.max_pool3d
mindspore.ops.max_unpool1d
mindspore.ops.max_unpool2d

View File

@ -3651,8 +3651,8 @@ class Tensor(Tensor_):
def arcsinh(self):
r"""
See `Tensor.asinh()
<https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore/Tensor/mindspore.Tensor.asinh.html>`_.
Alias for Tensor.asinh().
For details, please refer to :func:`mindspore.ops.asinh`.
"""
self._init_check()
return tensor_operator_registry.get('asinh')(self)
@ -3673,8 +3673,8 @@ class Tensor(Tensor_):
def arctanh(self):
r"""
See `Tensor.atanh()
<https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore/Tensor/mindspore.Tensor.atanh.html>`_.
Alias for Tensor.atanh().
For details, please refer to :func:`mindspore.ops.atanh`.
"""
self._init_check()
return tensor_operator_registry.get('atanh')(self)

View File

@ -525,19 +525,12 @@ class MarginRankingLoss(LossBase):
def __init__(self, margin=0.0, reduction='mean'):
"""Initialize MarginRankingLoss."""
super(MarginRankingLoss, self).__init__(reduction)
self.margin = validator.check_value_type("margin", margin, [float], self.cls_name)
self.reduction = reduction
self.margin = margin
self.maximum = P.Maximum()
def construct(self, input1, input2, target):
_check_is_tensor('input1', input1, self.cls_name)
_check_is_tensor('input2', input2, self.cls_name)
_check_is_tensor('target', target, self.cls_name)
F.same_type_shape(input1, input2)
F.same_type_shape(target, input1)
x = self.maximum(0, -target * (input1 - input2) + self.margin)
return self.get_loss(x)
x = ops.margin_ranking_loss(input1, input2, target, self.margin, self.reduction)
return x
class SmoothL1Loss(LossBase):

View File

@ -328,6 +328,7 @@ from .nn_func import (
log_softmax,
lrn,
mish,
margin_ranking_loss,
max_unpool1d,
max_unpool2d,
max_unpool3d,

View File

@ -2645,6 +2645,66 @@ def mish(x):
return mish_(x)
@constexpr
def _check_value_type(arg_name, arg_value, valid_types, prim_name=None):
"""Checks whether a value is instance of some types."""
return validator.check_value_type(arg_name, arg_value, valid_types, prim_name)
@constexpr(check=False)
def _check_is_tensor(param_name, input_data, cls_name):
"""Internal function, used to check whether the input data is Tensor."""
if input_data is not None and not isinstance(ops.typeof(input_data), mstype.tensor_type):
raise TypeError(f"For '{cls_name}', the '{param_name}' must be '{mstype.tensor_type}', "
f"but got '{ops.typeof(input_data)}'")
def _get_axis(x):
"""Get a range of axis for input."""
shape = ops.shape(x)
length = ops.tuple_len(shape)
perm = ops.make_range(0, length)
return perm
def _get_loss(x, reduction, cls_name, weights=1.0):
"""Calculate the loss with reduction and weights."""
if reduction not in ('mean', 'sum', 'none'):
raise ValueError(f"For '{cls_name}', the 'reduction' must be in ['mean', 'sum', 'none'], "
f"but got {reduction}.")
reduce_mean = P.ReduceMean()
reduce_sum = P.ReduceSum()
mul = P.Mul()
cast = P.Cast()
input_dtype = x.dtype
x = cast(x, mstype.float32)
weights = cast(weights, mstype.float32)
x = mul(weights, x)
if reduction == 'mean':
x = reduce_mean(x, _get_axis(x))
if reduction == 'sum':
x = reduce_sum(x, _get_axis(x))
x = cast(x, input_dtype)
return x
def margin_ranking_loss(input1, input2, target, margin=0.0, reduction='mean'):
"""
For details, please refer to :class:`mindspore.nn.MarginRankingLoss`.
"""
margin = _check_value_type("margin", margin, [float], "margin_ranking_loss")
_check_is_tensor('input1', input1, "margin_ranking_loss")
_check_is_tensor('input2', input2, "margin_ranking_loss")
_check_is_tensor('target', target, "margin_ranking_loss")
maximum = P.Maximum()
ops.same_type_shape(input1, input2)
ops.same_type_shape(target, input1)
x = maximum(0, -target * (input1 - input2) + margin)
return _get_loss(x, reduction, "margin_ranking_loss")
def max_pool3d(x, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False):
r"""
Performs a 3D max pooling on the input Tensor.
@ -4324,6 +4384,7 @@ __all__ = [
'relu6',
'conv3d',
'glu',
'margin_ranking_loss',
'multi_margin_loss',
'multi_label_margin_loss',
'elu',

View File

@ -24,7 +24,7 @@ from mindspore import Tensor
class MarginRankingLoss(nn.Cell):
def __init__(self, reduction="none"):
super(MarginRankingLoss, self).__init__()
self.margin_ranking_loss = nn.MarginRankingLoss(reduction=reduction)
self.margin_ranking_loss = nn.MarginRankingLoss(margin=0.0, reduction=reduction)
def construct(self, x, y, label):
return self.margin_ranking_loss(x, y, label)
@ -43,62 +43,26 @@ target = Tensor(np.array([-1, -1, 1]), ms.float32)
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE])
def test_margin_ranking_loss_none(mode):
@pytest.mark.parametrize('reduction', ["none", "mean", "sum"])
def test_margin_ranking_loss(mode, reduction):
"""
Feature: test MarginRankingLoss op with reduction none.
Description: Verify the result of MarginRankingLoss.
Expectation: expect correct forward result.
"""
ms.set_context(mode=mode)
loss = MarginRankingLoss('none')
loss = MarginRankingLoss(reduction=reduction)
output = loss(input1, input2, target)
expect_output = np.array([0.98759997, 0., 2.7003999])
if reduction == 'none':
expect_output = np.array([0.98759997, 0., 2.7003999])
elif reduction == 'sum':
expect_output = np.array(3.6879997)
else:
expect_output = np.array(1.2293333)
assert np.allclose(output.asnumpy(), expect_output)
@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_margin_ranking_loss_sum(mode):
"""
Feature: test MarginRankingLoss op with reduction sum.
Description: Verify the result of MarginRankingLoss.
Expectation: expect correct forward result.
"""
ms.set_context(mode=mode)
loss = MarginRankingLoss('sum')
output = loss(input1, input2, target)
expect_output = np.array(3.6879997)
assert np.allclose(output.asnumpy(), expect_output)
@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_margin_ranking_loss_mean(mode):
"""
Feature: test MarginRankingLoss op with reduction mean.
Description: Verify the result of MarginRankingLoss.
Expectation: expect correct forward result.
"""
ms.set_context(mode=mode)
loss = MarginRankingLoss('mean')
output = loss(input1, input2, target)
expect_output = np.array(1.2293333)
assert np.allclose(output.asnumpy(), expect_output)
@pytest.mark.level0
@pytest.mark.platform_x86_cpu
@pytest.mark.platform_arm_cpu
@ -113,6 +77,7 @@ def test_tensor_dim(mode):
Description: Verify the result of dim.
Expectation: expect correct forward result.
"""
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()

View File

@ -0,0 +1,62 @@
# 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
import mindspore.ops as ops
from mindspore import Tensor
class MarginRankingLoss(nn.Cell):
def __init__(self, reduction):
super(MarginRankingLoss, self).__init__()
self.reduction = reduction
def construct(self, x, y, label, margin):
return ops.margin_ranking_loss(x, y, label, margin=margin, reduction=self.reduction)
@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])
@pytest.mark.parametrize('reduction', ["none", "mean", "sum"])
def test_margin_ranking_loss(mode, reduction):
"""
Feature: test MarginRankingLoss op.
Description: Verify the result of MarginRankingLoss.
Expectation: expect correct forward result.
"""
ms.set_context(mode=mode)
loss = MarginRankingLoss(reduction)
input1 = Tensor(np.array([0.3864, -2.4093, -1.4076]), ms.float32)
input2 = Tensor(np.array([-0.6012, -1.6681, 1.2928]), ms.float32)
target = Tensor(np.array([-1, -1, 1]), ms.float32)
output = loss(input1, input2, target, 0.0)
if reduction == 'none':
expect_output = np.array([0.98759997, 0., 2.7003999])
elif reduction == 'sum':
expect_output = np.array(3.6879997)
else:
expect_output = np.array(1.2293333)
assert np.allclose(output.asnumpy(), expect_output)

View File

@ -0,0 +1,45 @@
# 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
import mindspore.ops as ops
from mindspore import Tensor
class MarginRankingLoss(nn.Cell):
def __init__(self, reduction):
super(MarginRankingLoss, self).__init__()
self.reduction = reduction
def construct(self, x, y, label, margin):
return ops.margin_ranking_loss(x, y, label, margin, reduction=self.reduction)
@pytest.mark.parametrize('reduction', ["none", "mean", "sum"])
def test_margin_ranking_loss(reduction):
"""
Feature: test MarginRankingLoss op with reduction none.
Description: Verify the result of MarginRankingLoss.
Expectation: expect correct forward result.
"""
loss = MarginRankingLoss(reduction)
input1 = Tensor(np.array([0.3864, -2.4093, -1.4076]), ms.float32)
input2 = Tensor(np.array([-0.6012, -1.6681, 1.2928]), ms.float32)
target = Tensor(np.array([-1, -1, 1]), ms.float32)
loss(input1, input2, target, 0.0)