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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. def test_multilabel_accuracy():
  44. x = Tensor(np.array([[0, 1, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1]]))
  45. y = Tensor(np.array([[0, 1, 1, 1], [0, 1, 1, 1], [0, 0, 0, 1]]))
  46. metric = Accuracy('multilabel')
  47. metric.clear()
  48. metric.update(x, y)
  49. accuracy = metric.eval()
  50. assert accuracy == 1 / 3
  51. def test_shape_accuracy():
  52. x = Tensor(np.array([[0, 1, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1]]))
  53. y = Tensor(np.array([[0, 1, 1, 1], [0, 1, 1, 1]]))
  54. metric = Accuracy('multilabel')
  55. metric.clear()
  56. with pytest.raises(ValueError):
  57. metric.update(x, y)
  58. def test_shape_accuracy2():
  59. x = Tensor(np.array([[0, 1, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1]]))
  60. y = Tensor(np.array([0, 1, 1, 1]))
  61. metric = Accuracy('multilabel')
  62. metric.clear()
  63. with pytest.raises(ValueError):
  64. metric.update(x, y)
  65. def test_shape_accuracy3():
  66. x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  67. y = Tensor(np.array([[1, 0, 1], [1, 1, 1]]))
  68. metric = Accuracy('classification')
  69. metric.clear()
  70. with pytest.raises(ValueError):
  71. metric.update(x, y)
  72. def test_shape_accuracy4():
  73. x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  74. y = Tensor(np.array(1))
  75. metric = Accuracy('classification')
  76. metric.clear()
  77. with pytest.raises(ValueError):
  78. metric.update(x, y)
  79. def test_type_accuracy():
  80. with pytest.raises(TypeError):
  81. Accuracy('test')