|
|
@@ -1,4 +1,4 @@ |
|
|
# Copyright 2020 Huawei Technologies Co., Ltd |
|
|
|
|
|
|
|
|
# Copyright 2021 Huawei Technologies Co., Ltd |
|
|
# |
|
|
# |
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
# you may not use this file except in compliance with the License. |
|
|
# you may not use this file except in compliance with the License. |
|
|
@@ -14,19 +14,24 @@ |
|
|
# ============================================================================ |
|
|
# ============================================================================ |
|
|
""" test loss """ |
|
|
""" test loss """ |
|
|
import numpy as np |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
|
|
|
import mindspore |
|
|
from mindspore import Tensor |
|
|
from mindspore import Tensor |
|
|
from mindspore.ops import operations as P |
|
|
from mindspore.ops import operations as P |
|
|
from mindspore.nn.loss.loss import _Loss |
|
|
from mindspore.nn.loss.loss import _Loss |
|
|
|
|
|
from mindspore.nn.loss.loss import L1Loss |
|
|
|
|
|
import mindspore.context as context |
|
|
|
|
|
|
|
|
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target='GPU') |
|
|
|
|
|
|
|
|
class WeightedLoss(_Loss): |
|
|
class WeightedLoss(_Loss): |
|
|
def __init__(self, reduction='mean', weights=1.0): |
|
|
def __init__(self, reduction='mean', weights=1.0): |
|
|
super(WeightedLoss, self).__init__(reduction, weights) |
|
|
|
|
|
|
|
|
super(WeightedLoss, self).__init__(reduction) |
|
|
self.abs = P.Abs() |
|
|
self.abs = P.Abs() |
|
|
|
|
|
self.weights = weights |
|
|
|
|
|
|
|
|
def construct(self, base, target): |
|
|
def construct(self, base, target): |
|
|
x = self.abs(base - target) |
|
|
x = self.abs(base - target) |
|
|
return self.get_loss(x) |
|
|
|
|
|
|
|
|
return self.get_loss(x, self.weights) |
|
|
|
|
|
|
|
|
def test_WeightedLoss(): |
|
|
def test_WeightedLoss(): |
|
|
loss = WeightedLoss() |
|
|
loss = WeightedLoss() |
|
|
@@ -35,17 +40,41 @@ def test_WeightedLoss(): |
|
|
output_data = loss(input_data, target_data) |
|
|
output_data = loss(input_data, target_data) |
|
|
|
|
|
|
|
|
error_range = np.ones(shape=output_data.shape) * 10e-6 |
|
|
error_range = np.ones(shape=output_data.shape) * 10e-6 |
|
|
loss.weights = 1.0 |
|
|
|
|
|
|
|
|
loss = WeightedLoss(weights=2.0) |
|
|
test_output = loss(input_data, target_data) |
|
|
test_output = loss(input_data, target_data) |
|
|
diff = test_output - output_data * loss.weights |
|
|
|
|
|
|
|
|
diff = test_output - output_data * 2.0 |
|
|
assert np.all(abs(diff.asnumpy()) < error_range) |
|
|
assert np.all(abs(diff.asnumpy()) < error_range) |
|
|
|
|
|
|
|
|
loss.weights = 2.0 |
|
|
|
|
|
|
|
|
loss = WeightedLoss(weights=3) |
|
|
test_output = loss(input_data, target_data) |
|
|
test_output = loss(input_data, target_data) |
|
|
diff = test_output - output_data * loss.weights |
|
|
|
|
|
|
|
|
diff = test_output - output_data * 3 |
|
|
assert np.all(abs(diff.asnumpy()) < error_range) |
|
|
assert np.all(abs(diff.asnumpy()) < error_range) |
|
|
|
|
|
|
|
|
loss.weights = 3 |
|
|
|
|
|
test_output = loss(input_data, target_data) |
|
|
|
|
|
diff = test_output - output_data * loss.weights |
|
|
|
|
|
|
|
|
loss = WeightedLoss(weights=Tensor(np.array([[0.7, 0.3], [0.7, 0.3]]))) |
|
|
|
|
|
y_true = Tensor(np.array([[0., 1.], [0., 0.]]), mindspore.float32) |
|
|
|
|
|
y_pred = Tensor(np.array([[1., 1.], [1., 0.]]), mindspore.float32) |
|
|
|
|
|
test_data = 0.35 |
|
|
|
|
|
output = loss(y_true, y_pred) |
|
|
|
|
|
diff = test_data - output.asnumpy() |
|
|
|
|
|
assert np.all(abs(diff) < error_range) |
|
|
|
|
|
|
|
|
|
|
|
class CustomLoss(_Loss): |
|
|
|
|
|
def __init__(self, reduction='mean'): |
|
|
|
|
|
super(CustomLoss, self).__init__(reduction) |
|
|
|
|
|
self.abs = P.Abs() |
|
|
|
|
|
|
|
|
|
|
|
def construct(self, base, target): |
|
|
|
|
|
x = self.abs(base - target) |
|
|
|
|
|
return self.get_loss(x, weights=2.0) |
|
|
|
|
|
|
|
|
|
|
|
def test_CustomLoss(): |
|
|
|
|
|
loss = L1Loss() |
|
|
|
|
|
input_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(np.float32)) |
|
|
|
|
|
target_data = Tensor(np.array([[0, 2, 5], [3, 1, 1]]).astype(np.float32)) |
|
|
|
|
|
output_data = loss(input_data, target_data) |
|
|
|
|
|
|
|
|
|
|
|
error_range = np.ones(shape=output_data.shape) * 10e-6 |
|
|
|
|
|
customloss = CustomLoss() |
|
|
|
|
|
test_output = customloss(input_data, target_data) |
|
|
|
|
|
diff = test_output - output_data * 2.0 |
|
|
assert np.all(abs(diff.asnumpy()) < error_range) |
|
|
assert np.all(abs(diff.asnumpy()) < error_range) |