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_accuracy.py 3.5 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 accuracy"""
  16. import math
  17. import numpy as np
  18. import pytest
  19. from mindspore import Tensor
  20. from mindspore.nn.metrics import Accuracy
  21. def test_classification_accuracy():
  22. """test_classification_accuracy"""
  23. x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  24. y = Tensor(np.array([1, 0, 1]))
  25. y2 = Tensor(np.array([[0, 1], [1, 0], [0, 1]]))
  26. metric = Accuracy('classification')
  27. metric.clear()
  28. metric.update(x, y)
  29. accuracy = metric.eval()
  30. accuracy2 = metric(x, y2)
  31. assert math.isclose(accuracy, 2 / 3)
  32. assert math.isclose(accuracy2, 2 / 3)
  33. def test_classification_accuracy_indexes_awareness():
  34. """A indexes aware version of test_classification_accuracy"""
  35. x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  36. y = Tensor(np.array([1, 0, 1]))
  37. y2 = Tensor(np.array([0, 0, 1]))
  38. metric = Accuracy('classification').set_indexes([0, 2])
  39. metric.clear()
  40. metric.update(x, y, y2)
  41. accuracy = metric.eval()
  42. assert math.isclose(accuracy, 1 / 3)
  43. @pytest.mark.parametrize('indexes', [0, [0., 2.], [0., 1], ['1', '0']])
  44. def test_set_indexes(indexes):
  45. pat_str = "For 'set_indexes', the argument 'indexes' should be a list and all its elements should " \
  46. "be int, please check whether it is correct."
  47. with pytest.raises(ValueError, match=pat_str):
  48. _ = Accuracy('classification').set_indexes(indexes)
  49. def test_multilabel_accuracy():
  50. x = Tensor(np.array([[0, 1, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1]]))
  51. y = Tensor(np.array([[0, 1, 1, 1], [0, 1, 1, 1], [0, 0, 0, 1]]))
  52. metric = Accuracy('multilabel')
  53. metric.clear()
  54. metric.update(x, y)
  55. accuracy = metric.eval()
  56. assert accuracy == 1 / 3
  57. def test_shape_accuracy():
  58. x = Tensor(np.array([[0, 1, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1]]))
  59. y = Tensor(np.array([[0, 1, 1, 1], [0, 1, 1, 1]]))
  60. metric = Accuracy('multilabel')
  61. metric.clear()
  62. with pytest.raises(ValueError):
  63. metric.update(x, y)
  64. def test_shape_accuracy2():
  65. x = Tensor(np.array([[0, 1, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1]]))
  66. y = Tensor(np.array([0, 1, 1, 1]))
  67. metric = Accuracy('multilabel')
  68. metric.clear()
  69. with pytest.raises(ValueError):
  70. metric.update(x, y)
  71. def test_shape_accuracy3():
  72. x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  73. y = Tensor(np.array([[1, 0, 1], [1, 1, 1]]))
  74. metric = Accuracy('classification')
  75. metric.clear()
  76. with pytest.raises(ValueError):
  77. metric.update(x, y)
  78. def test_shape_accuracy4():
  79. x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  80. y = Tensor(np.array(1))
  81. metric = Accuracy('classification')
  82. metric.clear()
  83. with pytest.raises(ValueError):
  84. metric.update(x, y)
  85. def test_type_accuracy():
  86. with pytest.raises(TypeError):
  87. Accuracy('test')