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 8.1 kB

5 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # Copyright 2020 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.common import dtype as mstype
  19. from mindspore import nn
  20. from mindspore import Tensor
  21. from ..ut_filter import non_graph_engine
  22. def test_L1Loss():
  23. loss = nn.L1Loss()
  24. input_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(np.float32))
  25. target_data = Tensor(np.array([[0, 2, 5], [3, 1, 1]]).astype(np.float32))
  26. loss(input_data, target_data)
  27. def test_MSELoss():
  28. loss = nn.MSELoss()
  29. input_data = Tensor(np.array([[1, 2, 3], [2, 3, 2]]).astype(np.float32))
  30. target_data = Tensor(np.array([[0, 0, 5], [1, 2, 3]]).astype(np.float32))
  31. loss(input_data, target_data)
  32. @non_graph_engine
  33. def test_SoftmaxCrossEntropyWithLogits():
  34. """ test_SoftmaxCrossEntropyWithLogits """
  35. loss = nn.SoftmaxCrossEntropyWithLogits()
  36. logits = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  37. labels = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  38. loss.construct(logits, labels)
  39. def test_SoftmaxCrossEntropyWithLogits_reduce():
  40. """ test_SoftmaxCrossEntropyWithLogits """
  41. loss = nn.SoftmaxCrossEntropyWithLogits(reduction="mean")
  42. logits = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  43. labels = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  44. loss(logits, labels)
  45. def test_BCELoss():
  46. """ test_BCELoss """
  47. loss = nn.BCELoss()
  48. inputs_data = Tensor(np.array([[0.1, 0.2, 0.3], [0.5, 0.7, 0.9]]).astype(np.float32))
  49. target_data = Tensor(np.array([[0, 1, 0], [0, 0, 1]]).astype(np.float32))
  50. loss(inputs_data, target_data)
  51. def test_BCELoss_reduce():
  52. """ test_BCELoss """
  53. loss = nn.BCELoss(reduction='mean')
  54. inputs_data = Tensor(np.array([[0.1, 0.2, 0.3], [0.5, 0.7, 0.9]]).astype(np.float32))
  55. target_data = Tensor(np.array([[0, 1, 0], [0, 0, 1]]).astype(np.float32))
  56. loss(inputs_data, target_data)
  57. def test_BCELoss_weight():
  58. """ test_BCELoss """
  59. weight = Tensor(np.array([[1.0, 2.0, 3.0], [2.2, 2.6, 3.9]]).astype(np.float32))
  60. loss = nn.BCELoss(weight=weight)
  61. inputs_data = Tensor(np.array([[0.1, 0.2, 0.3], [0.5, 0.7, 0.9]]).astype(np.float32))
  62. target_data = Tensor(np.array([[0, 1, 0], [0, 0, 1]]).astype(np.float32))
  63. loss(inputs_data, target_data)
  64. def test_cosine_embedding_loss():
  65. """ test CosineEmbeddingLoss """
  66. loss = nn.CosineEmbeddingLoss()
  67. x1 = Tensor(np.array([[0.3, 0.8], [0.4, 0.3]]).astype(np.float32))
  68. x2 = Tensor(np.array([[0.4, 1.2], [-0.4, -0.9]]).astype(np.float32))
  69. label = Tensor(np.array([1, -1]).astype(np.int32))
  70. loss(x1, x2, label)
  71. def test_focal_loss():
  72. """ test_FocalLoss """
  73. x1 = Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], mstype.float32)
  74. x2 = Tensor([[1], [1], [0]], mstype.int32)
  75. focalloss = nn.FocalLoss()
  76. focalloss(x1, x2)
  77. def test_focal_loss_gamma():
  78. """ test_FocalLoss """
  79. x1 = Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], mstype.float32)
  80. x2 = Tensor([[1], [1], [0]], mstype.int32)
  81. with pytest.raises(TypeError):
  82. focalloss = nn.FocalLoss(weight=None, gamma="mmm", reduction='mean')
  83. focalloss(x1, x2)
  84. def test_focal_loss_weight():
  85. """ test_FocalLoss """
  86. x1 = Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], mstype.float32)
  87. x2 = Tensor([[1], [1]], mstype.int32)
  88. with pytest.raises(TypeError):
  89. focalloss = nn.FocalLoss(weight='a', gamma=2.0, reduction='mean')
  90. focalloss(x1, x2)
  91. def test_focal_loss_reduction():
  92. """ test_FocalLoss """
  93. x1 = Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], mstype.float32)
  94. x2 = Tensor([[1], [1], [0]], mstype.int32)
  95. with pytest.raises(ValueError):
  96. focalloss = nn.FocalLoss(weight=None, gamma=2.0, reduction='m')
  97. focalloss(x1, x2)
  98. def test_focal_loss_input():
  99. """ test_FocalLoss """
  100. x1 = Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], mstype.float32)
  101. x2 = Tensor([[1]], mstype.int32)
  102. focalloss = nn.FocalLoss(weight=None, gamma=2.0, reduction='mean')
  103. with pytest.raises(ValueError):
  104. focalloss(x1, x2)
  105. def test_dice_loss():
  106. """ test_dice_loss """
  107. loss = nn.DiceLoss()
  108. y_pred = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
  109. y = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mstype.float32)
  110. # Pass the test if no error is reported
  111. loss(y_pred, y)
  112. def test_dice_loss_check_shape():
  113. """ test_dice_loss """
  114. loss = nn.DiceLoss()
  115. y_pred = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
  116. y = Tensor(np.array([[1, 0], [0, 1]]), mstype.float32)
  117. with pytest.raises(ValueError):
  118. loss(y_pred, y)
  119. def test_multi_class_dice_loss():
  120. """ test_multi_class_dice_loss """
  121. loss = nn.MultiClassDiceLoss(weights=None, ignore_indiex=None, activation="softmax")
  122. y_pred = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
  123. y = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mstype.float32)
  124. loss(y_pred, y)
  125. def test_multi_class_dice_loss_check_shape():
  126. """ test_multi_class_dice_loss """
  127. loss = nn.MultiClassDiceLoss(weights=None, ignore_indiex=None, activation="softmax")
  128. y_pred = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
  129. y = Tensor(np.array([[1, 0], [0, 1]]), mstype.float32)
  130. with pytest.raises(ValueError):
  131. loss(y_pred, y)
  132. def test_multi_class_dice_loss_init_weight():
  133. """ test_multi_class_dice_loss """
  134. with pytest.raises(TypeError):
  135. loss = nn.MultiClassDiceLoss(weights='1', ignore_indiex=None, activation="softmax")
  136. y_pred = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
  137. y = Tensor(np.array([[1, 0], [0, 1]]), mstype.float32)
  138. loss(y_pred, y)
  139. def test_multi_class_dice_loss_init_ignore_indiex():
  140. """ test_multi_class_dice_loss """
  141. with pytest.raises(TypeError):
  142. loss = nn.MultiClassDiceLoss(weights=None, ignore_indiex="2", activation="softmax")
  143. y_pred = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
  144. y = Tensor(np.array([[1, 0], [0, 1]]), mstype.float32)
  145. loss(y_pred, y)
  146. def test_multi_class_dice_loss_init_activation():
  147. """ test_multi_class_dice_loss """
  148. with pytest.raises(TypeError):
  149. loss = nn.MultiClassDiceLoss(weights=None, ignore_indiex=None, activation=2)
  150. y_pred = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
  151. y = Tensor(np.array([[1, 0], [0, 1]]), mstype.float32)
  152. loss(y_pred, y)
  153. def test_multi_class_dice_loss_init_activation2():
  154. """ test_multi_class_dice_loss """
  155. with pytest.raises(ValueError):
  156. loss = nn.MultiClassDiceLoss(weights=None, ignore_indiex=None, activation='www')
  157. y_pred = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
  158. y = Tensor(np.array([[1, 0], [0, 1]]), mstype.float32)
  159. loss(y_pred, y)
  160. def test_rmse_loss():
  161. loss = nn.RMSELoss()
  162. input_data = Tensor(np.array([[1, 2, 3], [2, 3, 2]]).astype(np.float32))
  163. target_data = Tensor(np.array([[0, 0, 5], [1, 2, 3]]).astype(np.float32))
  164. loss(input_data, target_data)
  165. def test_mae_loss():
  166. loss = nn.MAELoss()
  167. input_data = Tensor(np.array([[1, 2, 3], [2, 3, 2]]).astype(np.float32))
  168. target_data = Tensor(np.array([[0, 0, 5], [1, 2, 3]]).astype(np.float32))
  169. loss(input_data, target_data)