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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 mindspore.nn as nn
  18. from mindspore import Tensor
  19. from ..ut_filter import non_graph_engine
  20. def test_L1Loss():
  21. loss = nn.L1Loss()
  22. input_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(np.float32))
  23. target_data = Tensor(np.array([[0, 2, 5], [3, 1, 1]]).astype(np.float32))
  24. loss(input_data, target_data)
  25. def test_MSELoss():
  26. loss = nn.MSELoss()
  27. input_data = Tensor(np.array([[1, 2, 3], [2, 3, 2]]).astype(np.float32))
  28. target_data = Tensor(np.array([[0, 0, 5], [1, 2, 3]]).astype(np.float32))
  29. loss(input_data, target_data)
  30. @non_graph_engine
  31. def test_SoftmaxCrossEntropyWithLogits():
  32. """ test_SoftmaxCrossEntropyWithLogits """
  33. loss = nn.SoftmaxCrossEntropyWithLogits()
  34. logits = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  35. labels = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  36. loss.construct(logits, labels)
  37. def test_SoftmaxCrossEntropyWithLogits_reduce():
  38. """ test_SoftmaxCrossEntropyWithLogits """
  39. loss = nn.SoftmaxCrossEntropyWithLogits(reduction="mean")
  40. logits = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  41. labels = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  42. loss(logits, labels)
  43. def test_BCELoss():
  44. """ test_BCELoss """
  45. loss = nn.BCELoss()
  46. inputs_data = Tensor(np.array([[0.1, 0.2, 0.3], [0.5, 0.7, 0.9]]).astype(np.float32))
  47. target_data = Tensor(np.array([[0, 1, 0], [0, 0, 1]]).astype(np.float32))
  48. loss(inputs_data, target_data)
  49. def test_BCELoss_reduce():
  50. """ test_BCELoss """
  51. loss = nn.BCELoss(reduction='mean')
  52. inputs_data = Tensor(np.array([[0.1, 0.2, 0.3], [0.5, 0.7, 0.9]]).astype(np.float32))
  53. target_data = Tensor(np.array([[0, 1, 0], [0, 0, 1]]).astype(np.float32))
  54. loss(inputs_data, target_data)
  55. def test_BCELoss_weight():
  56. """ test_BCELoss """
  57. weight = Tensor(np.array([[1.0, 2.0, 3.0], [2.2, 2.6, 3.9]]).astype(np.float32))
  58. loss = nn.BCELoss(weight=weight)
  59. inputs_data = Tensor(np.array([[0.1, 0.2, 0.3], [0.5, 0.7, 0.9]]).astype(np.float32))
  60. target_data = Tensor(np.array([[0, 1, 0], [0, 0, 1]]).astype(np.float32))
  61. loss(inputs_data, target_data)
  62. def test_cosine_embedding_loss():
  63. """ test CosineEmbeddingLoss """
  64. loss = nn.CosineEmbeddingLoss()
  65. x1 = Tensor(np.array([[0.3, 0.8], [0.4, 0.3]]).astype(np.float32))
  66. x2 = Tensor(np.array([[0.4, 1.2], [-0.4, -0.9]]).astype(np.float32))
  67. label = Tensor(np.array([1, -1]).astype(np.int32))
  68. loss(x1, x2, label)