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