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

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