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

6 years ago
6 years ago
6 years ago
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 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. @pytest.mark.level0
  38. @pytest.mark.platform_x86_gpu_training
  39. @pytest.mark.env_onecard
  40. def test_one_hot():
  41. one_hot = NetOneHot()
  42. indices1 = Tensor(np.array([[0, 1], [4, 5], [2, 6]]).astype(np.int32))
  43. indices2 = Tensor(np.array([1, 2, 3]).astype(np.int32))
  44. indices3 = Tensor(np.array([[0, 1], [1, 0]]).astype(np.int32))
  45. indices4 = Tensor(np.array([[0, 1], [4, 5], [2, 6]]).astype(np.int32))
  46. output = one_hot(indices1, indices2, indices3, indices4)
  47. expect_0 = np.array([
  48. [[2., 3., 3., 3., 3., 3.], [3., 2., 3., 3., 3., 3.]],
  49. [[3., 3., 3., 3., 2., 3.], [3., 3., 3., 3., 3., 2.]],
  50. [[3., 3., 2., 3., 3., 3.], [3., 3., 3., 3., 3., 3.]]
  51. ]).astype(np.float32)
  52. expect_1 = np.array([
  53. [3., 3., 3.],
  54. [2., 3., 3.],
  55. [3., 2., 3.],
  56. [3., 3., 2.],
  57. [3., 3., 3.],
  58. [3., 3., 3.]
  59. ]).astype(np.float32)
  60. expect_2 = np.array([
  61. [[2., 3.], [3., 2.]], [[3., 2.], [2., 3.]], [[3., 3.], [3., 3.]],
  62. [[3., 3.], [3., 3.]]
  63. ]).astype(np.float32)
  64. expect_3 = np.array([
  65. [[2., 3.], [3., 2.], [3., 3.], [3., 3.], [3., 3.], [3., 3.]],
  66. [[3., 3.], [3., 3.], [3., 3.], [3., 3.], [2., 3.], [3., 2.]],
  67. [[3., 3.], [3., 3.], [2., 3.], [3., 3.], [3., 3.], [3., 3.]]
  68. ]).astype(np.float32)
  69. assert (output[0].asnumpy() == expect_0).all()
  70. assert (output[1].asnumpy() == expect_1).all()
  71. assert (output[2].asnumpy() == expect_2).all()
  72. assert (output[3].asnumpy() == expect_3).all()