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_metric_factory.py 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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_metric_factory"""
  16. import math
  17. import numpy as np
  18. from mindspore import Tensor
  19. from mindspore.nn.metrics import get_metric_fn
  20. def test_classification_accuracy():
  21. x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  22. y = Tensor(np.array([1, 0, 1]))
  23. metric = get_metric_fn('accuracy', eval_type='classification')
  24. metric.clear()
  25. metric.update(x, y)
  26. accuracy = metric.eval()
  27. assert math.isclose(accuracy, 2 / 3)
  28. def test_classification_accuracy_by_alias():
  29. x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  30. y = Tensor(np.array([1, 0, 1]))
  31. metric = get_metric_fn('acc', eval_type='classification')
  32. metric.clear()
  33. metric.update(x, y)
  34. accuracy = metric.eval()
  35. assert math.isclose(accuracy, 2 / 3)
  36. def test_classification_precision():
  37. x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  38. y = Tensor(np.array([1, 0, 1]))
  39. metric = get_metric_fn('precision', eval_type='classification')
  40. metric.clear()
  41. metric.update(x, y)
  42. precision = metric.eval()
  43. assert np.equal(precision, np.array([0.5, 1])).all()