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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. import mindspore.common.dtype as mstype
  19. import mindspore.nn as 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_dice_loss():
  72. """ test_dice_loss """
  73. loss = nn.DiceLoss()
  74. y_pred = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
  75. y = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mstype.float32)
  76. # Pass the test if no error is reported
  77. loss(y_pred, y)
  78. def test_dice_loss_check_shape():
  79. """ test_dice_loss """
  80. loss = nn.DiceLoss()
  81. y_pred = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
  82. y = Tensor(np.array([[1, 0], [0, 1]]), mstype.float32)
  83. with pytest.raises(ValueError):
  84. loss(y_pred, y)