• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 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_perplexity"""
16
17import math
18import numpy as np
19import pytest
20from mindspore import Tensor
21from mindspore.nn.metrics import get_metric_fn, Perplexity
22
23
24def test_perplexity():
25    """test_perplexity"""
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    metric = get_metric_fn('perplexity')
29    metric.clear()
30    metric.update(x, y)
31    perplexity = metric.eval()
32
33    assert math.isclose(perplexity, 2.231443166940565, abs_tol=0.001)
34
35
36def test_perplexity_update1():
37    x = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.2], [0.9, 0.6, 0.5]]))
38    metric = Perplexity()
39    metric.clear()
40
41    with pytest.raises(ValueError):
42        metric.update(x)
43
44
45def test_perplexity_update2():
46    x = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.2], [0.9, 0.6, 0.5]]))
47    y = Tensor(np.array([1, 0]))
48    metric = Perplexity()
49    metric.clear()
50
51    with pytest.raises(RuntimeError):
52        metric.update(x, y)
53
54
55def test_perplexity_init():
56    with pytest.raises(TypeError):
57        Perplexity(ignore_label='abc')
58
59
60def test_perplexity_runtime():
61    metric = Perplexity()
62    metric.clear()
63
64    with pytest.raises(RuntimeError):
65        metric.eval()
66