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_one_hot_op.py 3.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. from mindspore import Tensor
  17. from mindspore.ops import operations as P
  18. import mindspore.nn as nn
  19. from mindspore.common.api import ms_function
  20. import numpy as np
  21. import mindspore.context as context
  22. context.set_context(device_target='GPU')
  23. class NetOneHot(nn.Cell):
  24. def __init__(self):
  25. super(NetOneHot, self).__init__()
  26. self.on_value = 2.0
  27. self.off_value = 3.0
  28. self.depth_1 = 6
  29. self.one_hot_1 = nn.OneHot(-1, self.depth_1, self.on_value, self.off_value)
  30. self.depth_2 = 4
  31. self.one_hot_2 = nn.OneHot(0, self.depth_1, self.on_value, self.off_value)
  32. self.one_hot_3 = nn.OneHot(0, self.depth_2, self.on_value, self.off_value)
  33. self.one_hot_4 = nn.OneHot(1, self.depth_1, self.on_value, self.off_value)
  34. @ms_function
  35. def construct(self, indices1, indices2, indices3, indices4):
  36. return (self.one_hot_1(indices1), self.one_hot_2(indices2),
  37. self.one_hot_3(indices3), self.one_hot_4(indices4))
  38. @pytest.mark.level0
  39. @pytest.mark.platform_x86_gpu_training
  40. @pytest.mark.env_onecard
  41. def test_one_hot():
  42. one_hot = NetOneHot()
  43. indices1 = Tensor(np.array([[0, 1], [4, 5], [2, 6]]).astype(np.int32))
  44. indices2 = Tensor(np.array([1, 2, 3]).astype(np.int32))
  45. indices3 = Tensor(np.array([[0, 1], [1, 0]]).astype(np.int32))
  46. indices4 = Tensor(np.array([[0, 1], [4, 5], [2, 6]]).astype(np.int32))
  47. output = one_hot(indices1, indices2, indices3, indices4)
  48. expect_0 = np.array([
  49. [[2., 3., 3., 3., 3., 3.], [3., 2., 3., 3., 3., 3.]],
  50. [[3., 3., 3., 3., 2., 3.], [3., 3., 3., 3., 3., 2.]],
  51. [[3., 3., 2., 3., 3., 3.], [3., 3., 3., 3., 3., 3.]]
  52. ]).astype(np.float32)
  53. expect_1 = np.array([
  54. [3., 3., 3.],
  55. [2., 3., 3.],
  56. [3., 2., 3.],
  57. [3., 3., 2.],
  58. [3., 3., 3.],
  59. [3., 3., 3.]
  60. ]).astype(np.float32)
  61. expect_2 = np.array([
  62. [[2., 3.], [3., 2.]], [[3., 2.], [2., 3.]], [[3., 3.], [3., 3.]],
  63. [[3., 3.], [3., 3.]]
  64. ]).astype(np.float32)
  65. expect_3 = np.array([
  66. [[2., 3.], [3., 2.], [3., 3.], [3., 3.], [3., 3.], [3., 3.]],
  67. [[3., 3.], [3., 3.], [3., 3.], [3., 3.], [2., 3.], [3., 2.]],
  68. [[3., 3.], [3., 3.], [2., 3.], [3., 3.], [3., 3.], [3., 3.]]
  69. ]).astype(np.float32)
  70. assert (output[0].asnumpy() == expect_0).all()
  71. assert (output[1].asnumpy() == expect_1).all()
  72. assert (output[2].asnumpy() == expect_2).all()
  73. assert (output[3].asnumpy() == expect_3).all()