• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2022 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15
16import numpy as np
17import pytest
18
19import mindspore.common.dtype as mstype
20import mindspore.nn as nn
21from mindspore import Tensor
22from mindspore import context
23
24
25class MultiLabelSoftMarginLossNet(nn.Cell):
26    def __init__(self, weight=None, reduction='mean'):
27        super(MultiLabelSoftMarginLossNet, self).__init__()
28        self.multilabel_soft_margin_loss = nn.MultiLabelSoftMarginLoss(weight=weight, reduction=reduction)
29
30    def construct(self, x, target):
31        return self.multilabel_soft_margin_loss(x, target)
32
33
34@pytest.mark.level2
35@pytest.mark.platform_x86_cpu
36@pytest.mark.platform_arm_cpu
37@pytest.mark.platform_x86_gpu_training
38@pytest.mark.platform_arm_ascend_training
39@pytest.mark.platform_x86_ascend_training
40@pytest.mark.env_onecard
41@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
42@pytest.mark.parametrize('weight', [None, Tensor([1.0, 1.5, 0.8], mstype.float32)])
43@pytest.mark.parametrize('reduction', ['mean', 'none', 'sum'])
44def test_multilabel_soft_margin_loss(mode, weight, reduction):
45    """
46    Feature: MultiLabelSoftMarginLoss with weight=[None, Tensor([1.0, 1.5, 0.8], mstype.float32)],
47    reduction=['mean', 'none', 'sum']
48    Description: Verify the result of MultiLabelSoftMarginLoss
49    Expectation: success
50    """
51    context.set_context(mode=mode)
52    net = MultiLabelSoftMarginLossNet(weight=weight, reduction=reduction)
53    arr1 = np.array([[0.3, 0.6, 0.6], [0.9, 0.4, 0.2]], np.float32)
54    arr2 = np.array([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0]], np.float32)
55    x = Tensor(arr1, mstype.float32)
56    label = Tensor(arr2, mstype.float32)
57    output = net(x, label)
58    if weight is None:
59        if reduction == 'mean':
60            expected = np.array(0.846940, np.float32)
61        elif reduction == 'sum':
62            expected = np.array(1.693880, np.float32)
63        else:
64            expected = np.array([0.776444, 0.917436], np.float32)
65    else:
66        if reduction == 'mean':
67            expected = np.array(0.974961, np.float32)
68        elif reduction == 'sum':
69            expected = np.array(1.949922, np.float32)
70        else:
71            expected = np.array([0.920193, 1.029729], np.float32)
72    assert np.allclose(output.asnumpy(), expected)
73