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_in_top_k.py 5.4 kB

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