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_argmaxwithvalue_op.py 2.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 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 NetArgmaxWithValue(nn.Cell):
  22. def __init__(self):
  23. super(NetArgmaxWithValue, self).__init__()
  24. axis1 = 0
  25. axis2 = -1
  26. self.argmax1 = P.ArgMaxWithValue(axis1)
  27. self.argmax2 = P.ArgMaxWithValue(axis2)
  28. self.argmax3 = P.ArgMaxWithValue()
  29. def construct(self, x):
  30. return (self.argmax1(x), self.argmax2(x), self.argmax3(x))
  31. @pytest.mark.level0
  32. @pytest.mark.platform_x86_gpu_training
  33. @pytest.mark.env_onecard
  34. def test_argmaxwithvalue():
  35. x = Tensor(np.array([[1., 20., 5.],
  36. [67., 8., 9.],
  37. [130., 24., 15.],
  38. [0.3, -0.4, -15.]]).astype(np.float32))
  39. expect1 = np.array([2, 2, 2]).astype(np.float32)
  40. expect2 = np.array([1, 0, 0, 0]).astype(np.float32)
  41. expect11 = np.array([130, 24, 15]).astype(np.float32)
  42. expect22 = np.array([20, 67, 130, 0.3]).astype(np.float32)
  43. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  44. argmax = NetArgmaxWithValue()
  45. output = argmax(x)
  46. assert (output[0][0].asnumpy() == expect1).all()
  47. assert (output[0][1].asnumpy() == expect11).all()
  48. assert (output[1][0].asnumpy() == expect2).all()
  49. assert (output[1][1].asnumpy() == expect22).all()
  50. assert (output[2][0].asnumpy() == expect1).all()
  51. assert (output[2][1].asnumpy() == expect11).all()
  52. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  53. argmax = NetArgmaxWithValue()
  54. output = argmax(x)
  55. assert (output[0][0].asnumpy() == expect1).all()
  56. assert (output[0][1].asnumpy() == expect11).all()
  57. assert (output[1][0].asnumpy() == expect2).all()
  58. assert (output[1][1].asnumpy() == expect22).all()
  59. assert (output[2][0].asnumpy() == expect1).all()
  60. assert (output[2][1].asnumpy() == expect11).all()