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_argmax_op.py 3.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. import random
  16. from functools import reduce
  17. import numpy as np
  18. import pytest
  19. import mindspore.context as context
  20. import mindspore.nn as nn
  21. from mindspore import Tensor
  22. from mindspore.common import dtype as mstype
  23. import mindspore.ops as ops
  24. class NetArgmax(nn.Cell):
  25. def __init__(self, axis=0):
  26. super(NetArgmax, self).__init__()
  27. self.argmax = ops.Argmax(axis, output_type=mstype.int32)
  28. def construct(self, x):
  29. return self.argmax(x)
  30. @pytest.mark.level0
  31. @pytest.mark.platform_x86_gpu_training
  32. @pytest.mark.env_onecard
  33. def test_argmax_1d():
  34. for mode in [context.PYNATIVE_MODE, context.GRAPH_MODE]:
  35. context.set_context(mode=mode, device_target="GPU")
  36. x = Tensor(np.array([1., 20., 5.]).astype(np.float32))
  37. Argmax = NetArgmax(axis=0)
  38. output = Argmax(x)
  39. expect = np.array([1]).astype(np.float32)
  40. assert (output.asnumpy() == expect).all()
  41. @pytest.mark.level0
  42. @pytest.mark.platform_x86_gpu_training
  43. @pytest.mark.env_onecard
  44. def test_argmax_2d():
  45. for mode in [context.PYNATIVE_MODE, context.GRAPH_MODE]:
  46. context.set_context(mode=mode, device_target="GPU")
  47. x = Tensor(np.array([[1., 20., 5.],
  48. [67., 8., 9.],
  49. [130., 24., 15.],
  50. [0.3, -0.4, -15.]]).astype(np.float32))
  51. Argmax_axis_0 = NetArgmax(axis=0)
  52. output = Argmax_axis_0(x)
  53. expect = np.array([2, 2, 2]).astype(np.int32)
  54. assert (output.asnumpy() == expect).all()
  55. Argmax_axis_1 = NetArgmax(axis=1)
  56. output = Argmax_axis_1(x)
  57. expect = np.array([1, 0, 0, 0]).astype(np.int32)
  58. assert (output.asnumpy() == expect).all()
  59. @pytest.mark.level0
  60. @pytest.mark.platform_x86_gpu_training
  61. @pytest.mark.env_onecard
  62. def test_argmax_high_dims():
  63. for mode in [context.PYNATIVE_MODE, context.GRAPH_MODE]:
  64. context.set_context(mode=mode, device_target="GPU")
  65. for dim in range(3, 10):
  66. shape = np.random.randint(1, 10, size=dim)
  67. x = np.random.randn(reduce(lambda x, y: x * y, shape)).astype(np.float32)
  68. x = x.reshape(shape)
  69. rnd_axis = random.randint(-dim + 1, dim - 1)
  70. Argmax = NetArgmax(axis=rnd_axis)
  71. ms_output = Argmax(Tensor(x))
  72. np_output = np.argmax(x, axis=rnd_axis)
  73. assert (ms_output.asnumpy() == np_output).all()