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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 mindspore.common.api import _executor
  20. from ..ut_filter import non_graph_engine
  21. def test_L1Loss():
  22. loss = nn.L1Loss()
  23. input_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(np.float32))
  24. target_data = Tensor(np.array([[0, 2, 5], [3, 1, 1]]).astype(np.float32))
  25. loss(input_data, target_data)
  26. def test_MSELoss():
  27. loss = nn.MSELoss()
  28. input_data = Tensor(np.array([[1, 2, 3], [2, 3, 2]]).astype(np.float32))
  29. target_data = Tensor(np.array([[0, 0, 5], [1, 2, 3]]).astype(np.float32))
  30. loss(input_data, target_data)
  31. @non_graph_engine
  32. def test_SoftmaxCrossEntropyWithLogits():
  33. """ test_SoftmaxCrossEntropyWithLogits """
  34. loss = nn.SoftmaxCrossEntropyWithLogits()
  35. logits = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  36. labels = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  37. loss.construct(logits, labels)
  38. def test_SoftmaxCrossEntropyWithLogits_reduce():
  39. """ test_SoftmaxCrossEntropyWithLogits """
  40. loss = nn.SoftmaxCrossEntropyWithLogits(reduction="mean")
  41. logits = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  42. labels = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  43. loss(logits, labels)
  44. def test_SoftmaxCrossEntropyExpand():
  45. from mindspore import context
  46. context.set_context(mode=context.GRAPH_MODE)
  47. loss = nn.SoftmaxCrossEntropyExpand()
  48. logits = Tensor(np.random.randint(0, 9, [100, 10]).astype(np.float32))
  49. labels = Tensor(np.random.randint(0, 9, [10,]).astype(np.float32))
  50. _executor.compile(loss, logits, labels)
  51. def test_cosine_embedding_loss():
  52. """ test CosineEmbeddingLoss """
  53. loss = nn.CosineEmbeddingLoss()
  54. x1 = Tensor(np.array([[0.3, 0.8], [0.4, 0.3]]).astype(np.float32))
  55. x2 = Tensor(np.array([[0.4, 1.2], [-0.4, -0.9]]).astype(np.float32))
  56. label = Tensor(np.array([1, -1]).astype(np.int32))
  57. loss(x1, x2, label)