|
|
|
@ -14,15 +14,14 @@
|
|
|
|
|
# ============================================================================
|
|
|
|
|
""" test loss """
|
|
|
|
|
import numpy as np
|
|
|
|
|
import mindspore
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from mindspore import Tensor
|
|
|
|
|
from mindspore.ops import operations as P
|
|
|
|
|
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):
|
|
|
|
|
def __init__(self, reduction='mean', weights=1.0):
|
|
|
|
|
super(WeightedLoss, self).__init__(reduction)
|
|
|
|
@ -33,10 +32,13 @@ class WeightedLoss(_Loss):
|
|
|
|
|
x = self.abs(base - target)
|
|
|
|
|
return self.get_loss(x, self.weights)
|
|
|
|
|
|
|
|
|
|
def test_WeightedLoss():
|
|
|
|
|
|
|
|
|
|
def weighted_loss(nptype):
|
|
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
|
|
|
|
|
|
|
|
|
|
loss = WeightedLoss()
|
|
|
|
|
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))
|
|
|
|
|
input_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(nptype))
|
|
|
|
|
target_data = Tensor(np.array([[0, 2, 5], [3, 1, 1]]).astype(nptype))
|
|
|
|
|
output_data = loss(input_data, target_data)
|
|
|
|
|
|
|
|
|
|
error_range = np.ones(shape=output_data.shape) * 10e-6
|
|
|
|
@ -50,14 +52,26 @@ def test_WeightedLoss():
|
|
|
|
|
diff = test_output - output_data * 3
|
|
|
|
|
assert np.all(abs(diff.asnumpy()) < error_range)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
loss = WeightedLoss(weights=Tensor(np.array([[0.7, 0.3], [0.7, 0.3]]).astype(nptype)))
|
|
|
|
|
y_true = Tensor(np.array([[0., 1.], [0., 0.]]).astype(nptype))
|
|
|
|
|
y_pred = Tensor(np.array([[1., 1.], [1., 0.]]).astype(nptype))
|
|
|
|
|
test_data = 0.35
|
|
|
|
|
output = loss(y_true, y_pred)
|
|
|
|
|
diff = test_data - output.asnumpy()
|
|
|
|
|
assert np.all(abs(diff) < error_range)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0
|
|
|
|
|
@pytest.mark.platform_x86_gpu_training
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_weighted_loss_float32():
|
|
|
|
|
weighted_loss(np.float32)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0
|
|
|
|
|
@pytest.mark.platform_x86_gpu_training
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_weighted_loss_float64():
|
|
|
|
|
weighted_loss(np.float64)
|
|
|
|
|
|
|
|
|
|
class CustomLoss(_Loss):
|
|
|
|
|
def __init__(self, reduction='mean'):
|
|
|
|
|
super(CustomLoss, self).__init__(reduction)
|
|
|
|
@ -67,10 +81,10 @@ class CustomLoss(_Loss):
|
|
|
|
|
x = self.abs(base - target)
|
|
|
|
|
return self.get_loss(x, weights=2.0)
|
|
|
|
|
|
|
|
|
|
def test_CustomLoss():
|
|
|
|
|
def custom_loss(nptype):
|
|
|
|
|
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))
|
|
|
|
|
input_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(nptype))
|
|
|
|
|
target_data = Tensor(np.array([[0, 2, 5], [3, 1, 1]]).astype(nptype))
|
|
|
|
|
output_data = loss(input_data, target_data)
|
|
|
|
|
|
|
|
|
|
error_range = np.ones(shape=output_data.shape) * 10e-6
|
|
|
|
@ -78,3 +92,21 @@ def test_CustomLoss():
|
|
|
|
|
test_output = customloss(input_data, target_data)
|
|
|
|
|
diff = test_output - output_data * 2.0
|
|
|
|
|
assert np.all(abs(diff.asnumpy()) < error_range)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0
|
|
|
|
|
@pytest.mark.platform_x86_gpu_training
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_custom_loss_float16():
|
|
|
|
|
custom_loss(np.float16)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0
|
|
|
|
|
@pytest.mark.platform_x86_gpu_training
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_custom_loss_float32():
|
|
|
|
|
custom_loss(np.float32)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0
|
|
|
|
|
@pytest.mark.platform_x86_gpu_training
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_custom_loss_float64():
|
|
|
|
|
custom_loss(np.float64)
|
|
|
|
|