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# ============================================================================ 15import numpy as np 16import pytest 17 18import mindspore.context as context 19import mindspore.nn as nn 20from mindspore import Tensor 21from mindspore.ops import operations as P 22 23class InTopKNet(nn.Cell): 24 def __init__(self, k): 25 super(InTopKNet, self).__init__() 26 self.in_top_k = P.InTopK(k) 27 28 def construct(self, predictions, targets): 29 return self.in_top_k(predictions, targets) 30 31 32def in_top_k(nptype): 33 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 34 35 predictions = Tensor(np.array([[4, 1, 2, 0, 0, 0, 0, 0, 0], 36 [7, 9, 9, 0, 0, 0, 0, 0, 0], 37 [3, 3, 3, 0, 0, 0, 0, 0, 0]]).astype(nptype)) 38 k = 165 39 in_top_k_net = InTopKNet(k) 40 targets = Tensor(np.array([0, 1, 0]).astype(np.int32)) 41 output = in_top_k_net(predictions, targets) 42 expected_output = np.array([True, True, True]) 43 np.testing.assert_array_equal(output.asnumpy(), expected_output) 44 45 k = -2 46 in_top_k_net = InTopKNet(k) 47 targets = Tensor(np.array([0, 1, 0]).astype(np.int32)) 48 output = in_top_k_net(predictions, targets) 49 expected_output = np.array([False, False, False]) 50 np.testing.assert_array_equal(output.asnumpy(), expected_output) 51 52 k = 1 53 in_top_k_net = InTopKNet(k) 54 targets = Tensor(np.array([0, 1, 0]).astype(np.int32)) 55 output = in_top_k_net(predictions, targets) 56 expected_output = np.array([True, True, True]) 57 np.testing.assert_array_equal(output.asnumpy(), expected_output) 58 59 targets = Tensor(np.array([1, 0, 2]).astype(np.int32)) 60 output = in_top_k_net(predictions, targets) 61 expected_output = np.array([False, False, True]) 62 np.testing.assert_array_equal(output.asnumpy(), expected_output) 63 64 targets = Tensor(np.array([2, 2, 1]).astype(np.int32)) 65 output = in_top_k_net(predictions, targets) 66 expected_output = np.array([False, True, True]) 67 np.testing.assert_array_equal(output.asnumpy(), expected_output) 68 69 k = 2 70 in_top_k_net = InTopKNet(k) 71 targets = Tensor(np.array([0, 1, 2]).astype(np.int32)) 72 output = in_top_k_net(predictions, targets) 73 expected_output = np.array([True, True, True]) 74 np.testing.assert_array_equal(output.asnumpy(), expected_output) 75 76 targets = Tensor(np.array([2, 2, 0]).astype(np.int32)) 77 output = in_top_k_net(predictions, targets) 78 expected_output = np.array([True, True, True]) 79 np.testing.assert_array_equal(output.asnumpy(), expected_output) 80 81 targets = Tensor(np.array([1, 0, 1]).astype(np.int32)) 82 output = in_top_k_net(predictions, targets) 83 expected_output = np.array([False, False, True]) 84 np.testing.assert_array_equal(output.asnumpy(), expected_output) 85 86 k = 3 87 in_top_k_net = InTopKNet(k) 88 targets = Tensor(np.array([2, 2, 2]).astype(np.int32)) 89 output = in_top_k_net(predictions, targets) 90 expected_output = np.array([True, True, True]) 91 np.testing.assert_array_equal(output.asnumpy(), expected_output) 92 93 targets = Tensor(np.array([1, 1, 0]).astype(np.int32)) 94 output = in_top_k_net(predictions, targets) 95 expected_output = np.array([True, True, True]) 96 np.testing.assert_array_equal(output.asnumpy(), expected_output) 97 98 targets = Tensor(np.array([0, 0, 1]).astype(np.int32)) 99 output = in_top_k_net(predictions, targets) 100 expected_output = np.array([True, True, True]) 101 np.testing.assert_array_equal(output.asnumpy(), expected_output) 102 103@pytest.mark.level0 104@pytest.mark.platform_x86_gpu_training 105@pytest.mark.env_onecard 106def test_in_top_k_float16(): 107 in_top_k(np.float16) 108 109@pytest.mark.level0 110@pytest.mark.platform_x86_gpu_training 111@pytest.mark.env_onecard 112def test_in_top_k_float32(): 113 in_top_k(np.float32) 114 115@pytest.mark.level0 116@pytest.mark.platform_x86_gpu_training 117@pytest.mark.env_onecard 118def test_in_top_k_invalid_input(): 119 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 120 121 # predictions must be 2d 122 with pytest.raises(ValueError): 123 in_top_k_net = InTopKNet(1) 124 predictions = Tensor(np.zeros(4).astype(np.float32)) 125 targets = Tensor(np.zeros(4).astype(np.int32)) 126 _ = in_top_k_net(predictions, targets) 127 128 # targets must be 1d 129 with pytest.raises(ValueError): 130 in_top_k_net = InTopKNet(1) 131 predictions = Tensor(np.zeros(4).astype(np.float32)) 132 targets = Tensor(np.zeros(4).reshape(2, 2).astype(np.int32)) 133 _ = in_top_k_net(predictions, targets) 134 135 # predictions.shape[1] must be equal to targets.shape[0] 136 with pytest.raises(ValueError): 137 in_top_k_net = InTopKNet(1) 138 predictions = Tensor(np.zeros(4).reshape(2, 2).astype(np.float32)) 139 targets = Tensor(np.zeros(4).astype(np.int32)) 140 _ = in_top_k_net(predictions, targets) 141