• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_fbeta"""
16import numpy as np
17import pytest
18
19from mindspore import Tensor
20from mindspore.nn.metrics import get_metric_fn, Fbeta
21
22
23def test_classification_fbeta():
24    """test_classification_fbeta"""
25    x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
26    y = Tensor(np.array([1, 0, 1]))
27    y2 = Tensor(np.array([[0, 1], [1, 0], [0, 1]]))
28    metric = get_metric_fn('F1')
29    metric.clear()
30    metric.update(x, y)
31    fbeta = metric.eval()
32    fbeta_mean = metric.eval(True)
33    fbeta2 = metric(x, y2)
34
35    assert np.allclose(fbeta, np.array([2 / 3, 2 / 3]))
36    assert np.allclose(fbeta2, np.array([2 / 3, 2 / 3]))
37    assert np.allclose(fbeta_mean, 2 / 3)
38
39
40def test_fbeta_update1():
41    x = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.2], [0.9, 0.6, 0.5]]))
42    y = Tensor(np.array([1, 0]))
43    metric = Fbeta(2)
44    metric.clear()
45
46    with pytest.raises(ValueError):
47        metric.update(x, y)
48
49
50def test_fbeta_update2():
51    x1 = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.2], [0.9, 0.6, 0.5]]))
52    y1 = Tensor(np.array([1, 0, 2]))
53    x2 = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
54    y2 = Tensor(np.array([1, 0, 2]))
55    metric = Fbeta(2)
56    metric.clear()
57    metric.update(x1, y1)
58
59    with pytest.raises(ValueError):
60        metric.update(x2, y2)
61
62
63def test_fbeta_init():
64    with pytest.raises(ValueError):
65        Fbeta(0)
66
67
68def test_fbeta_runtime():
69    metric = Fbeta(2)
70    metric.clear()
71
72    with pytest.raises(RuntimeError):
73        metric.eval()
74