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.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 pytest
  16. import numpy as np
  17. from mindspore import Tensor
  18. from mindspore.ops import operations as P
  19. from mindspore.common import dtype as mstype
  20. import mindspore.nn as nn
  21. import mindspore.context as context
  22. class NetArgmax(nn.Cell):
  23. def __init__( self):
  24. super(NetArgmax, self).__init__()
  25. axis1 = 0
  26. axis2 = -1
  27. self.argmax1 = P.Argmax(axis1, output_type=mstype.int32)
  28. self.argmax2 = P.Argmax(axis2, output_type=mstype.int32)
  29. self.argmax3 = P.Argmax(output_type=mstype.int32)
  30. def construct(self, x):
  31. return (self.argmax1(x), self.argmax2(x), self.argmax3(x))
  32. @pytest.mark.level0
  33. @pytest.mark.platform_x86_gpu_training
  34. @pytest.mark.env_onecard
  35. def test_argmax():
  36. x = Tensor(np.array([[1., 20., 5.],
  37. [67., 8., 9.],
  38. [130., 24., 15.],
  39. [0.3, -0.4, -15.]]).astype(np.float32))
  40. expect1 = np.array([2,2,2]).astype(np.int32)
  41. expect2 = np.array([1,0,0,0]).astype(np.int32)
  42. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  43. Argmax = NetArgmax()
  44. output = Argmax(x)
  45. assert (output[0].asnumpy() == expect1).all()
  46. assert (output[1].asnumpy() == expect2).all()
  47. assert (output[2].asnumpy() == expect2).all()
  48. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  49. Argmax1 = NetArgmax()
  50. output1 = Argmax(x)
  51. assert (output1[0].asnumpy() == expect1).all()
  52. assert (output1[1].asnumpy() == expect2).all()
  53. assert (output1[2].asnumpy() == expect2).all()