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 2.7 kB

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