1# Copyright 2021 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""" test loss """ 16import numpy as np 17import pytest 18 19from mindspore import Tensor 20from mindspore.ops import operations as P 21from mindspore.nn.loss.loss import LossBase 22from mindspore.nn.loss.loss import L1Loss 23import mindspore.context as context 24 25class WeightedLoss(LossBase): 26 def __init__(self, reduction='mean', weights=1.0): 27 super(WeightedLoss, self).__init__(reduction) 28 self.abs = P.Abs() 29 self.weights = weights 30 31 def construct(self, base, target): 32 x = self.abs(base - target) 33 return self.get_loss(x, self.weights) 34 35 36def weighted_loss(nptype): 37 context.set_context(mode=context.GRAPH_MODE, device_target='GPU') 38 39 loss = WeightedLoss() 40 input_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(nptype)) 41 target_data = Tensor(np.array([[0, 2, 5], [3, 1, 1]]).astype(nptype)) 42 output_data = loss(input_data, target_data) 43 44 error_range = np.ones(shape=output_data.shape) * 10e-6 45 loss = WeightedLoss(weights=2.0) 46 test_output = loss(input_data, target_data) 47 diff = test_output - output_data * 2.0 48 assert np.all(abs(diff.asnumpy()) < error_range) 49 50 loss = WeightedLoss(weights=3) 51 test_output = loss(input_data, target_data) 52 diff = test_output - output_data * 3 53 assert np.all(abs(diff.asnumpy()) < error_range) 54 55 loss = WeightedLoss(weights=Tensor(np.array([[0.7, 0.3], [0.7, 0.3]]).astype(nptype))) 56 y_true = Tensor(np.array([[0., 1.], [0., 0.]]).astype(nptype)) 57 y_pred = Tensor(np.array([[1., 1.], [1., 0.]]).astype(nptype)) 58 test_data = 0.35 59 output = loss(y_true, y_pred) 60 diff = test_data - output.asnumpy() 61 assert np.all(abs(diff) < error_range) 62 63@pytest.mark.level0 64@pytest.mark.platform_x86_gpu_training 65@pytest.mark.env_onecard 66def test_weighted_loss_float32(): 67 weighted_loss(np.float32) 68 69@pytest.mark.level0 70@pytest.mark.platform_x86_gpu_training 71@pytest.mark.env_onecard 72def test_weighted_loss_float64(): 73 weighted_loss(np.float64) 74 75class CustomLoss(LossBase): 76 def __init__(self, reduction='mean'): 77 super(CustomLoss, self).__init__(reduction) 78 self.abs = P.Abs() 79 80 def construct(self, base, target): 81 x = self.abs(base - target) 82 return self.get_loss(x, weights=2.0) 83 84def custom_loss(nptype): 85 context.set_context(mode=context.GRAPH_MODE, device_target='GPU') 86 87 loss = L1Loss() 88 input_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(nptype)) 89 target_data = Tensor(np.array([[0, 2, 5], [3, 1, 1]]).astype(nptype)) 90 output_data = loss(input_data, target_data) 91 92 error_range = np.ones(shape=output_data.shape) * 10e-6 93 customloss = CustomLoss() 94 test_output = customloss(input_data, target_data) 95 diff = test_output - output_data * 2.0 96 assert np.all(abs(diff.asnumpy()) < error_range) 97 98@pytest.mark.level1 99@pytest.mark.platform_x86_gpu_training 100@pytest.mark.env_onecard 101def test_custom_loss_float16(): 102 custom_loss(np.float16) 103 104@pytest.mark.level0 105@pytest.mark.platform_x86_gpu_training 106@pytest.mark.env_onecard 107def test_custom_loss_float32(): 108 custom_loss(np.float32) 109 110@pytest.mark.level0 111@pytest.mark.platform_x86_gpu_training 112@pytest.mark.env_onecard 113def test_custom_loss_float64(): 114 custom_loss(np.float64) 115