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.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 mindspore
  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. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  24. class WeightedLoss(_Loss):
  25. def __init__(self, reduction='mean', weights=1.0):
  26. super(WeightedLoss, self).__init__(reduction)
  27. self.abs = P.Abs()
  28. self.weights = weights
  29. def construct(self, base, target):
  30. x = self.abs(base - target)
  31. return self.get_loss(x, self.weights)
  32. def test_WeightedLoss():
  33. loss = WeightedLoss()
  34. input_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(np.float32))
  35. target_data = Tensor(np.array([[0, 2, 5], [3, 1, 1]]).astype(np.float32))
  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]])))
  47. y_true = Tensor(np.array([[0., 1.], [0., 0.]]), mindspore.float32)
  48. y_pred = Tensor(np.array([[1., 1.], [1., 0.]]), mindspore.float32)
  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. class CustomLoss(_Loss):
  54. def __init__(self, reduction='mean'):
  55. super(CustomLoss, self).__init__(reduction)
  56. self.abs = P.Abs()
  57. def construct(self, base, target):
  58. x = self.abs(base - target)
  59. return self.get_loss(x, weights=2.0)
  60. def test_CustomLoss():
  61. loss = L1Loss()
  62. input_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(np.float32))
  63. target_data = Tensor(np.array([[0, 2, 5], [3, 1, 1]]).astype(np.float32))
  64. output_data = loss(input_data, target_data)
  65. error_range = np.ones(shape=output_data.shape) * 10e-6
  66. customloss = CustomLoss()
  67. test_output = customloss(input_data, target_data)
  68. diff = test_output - output_data * 2.0
  69. assert np.all(abs(diff.asnumpy()) < error_range)