You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_loss.py 3.8 kB

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