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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.nn.metrics import Accuracy
  20. from mindspore import Tensor
  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_multilabel_accuracy():
  34. x = Tensor(np.array([[0, 1, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1]]))
  35. y = Tensor(np.array([[0, 1, 1, 1], [0, 1, 1, 1], [0, 0, 0, 1]]))
  36. metric = Accuracy('multilabel')
  37. metric.clear()
  38. metric.update(x, y)
  39. accuracy = metric.eval()
  40. assert accuracy == 1/3
  41. def test_shape_accuracy():
  42. x = Tensor(np.array([[0, 1, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1]]))
  43. y = Tensor(np.array([[0, 1, 1, 1], [0, 1, 1, 1]]))
  44. metric = Accuracy('multilabel')
  45. metric.clear()
  46. with pytest.raises(ValueError):
  47. metric.update(x, y)
  48. def test_shape_accuracy2():
  49. x = Tensor(np.array([[0, 1, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1]]))
  50. y = Tensor(np.array([0, 1, 1, 1]))
  51. metric = Accuracy('multilabel')
  52. metric.clear()
  53. with pytest.raises(ValueError):
  54. metric.update(x, y)
  55. def test_shape_accuracy3():
  56. x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  57. y = Tensor(np.array([[1, 0, 1], [1, 1, 1]]))
  58. metric = Accuracy('classification')
  59. metric.clear()
  60. with pytest.raises(ValueError):
  61. metric.update(x, y)
  62. def test_shape_accuracy4():
  63. x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  64. y = Tensor(np.array(1))
  65. metric = Accuracy('classification')
  66. metric.clear()
  67. with pytest.raises(ValueError):
  68. metric.update(x, y)
  69. def test_type_accuracy():
  70. with pytest.raises(TypeError):
  71. Accuracy('test')