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_dropout.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright 2020-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.nn as nn
  18. from mindspore import Tensor
  19. import mindspore.context as context
  20. from mindspore.ops import operations as P
  21. from mindspore.ops.operations import _inner_ops as inner
  22. class Net(nn.Cell):
  23. def __init__(self, keep_prob):
  24. super(Net, self).__init__()
  25. self.drop = P.Dropout(keep_prob)
  26. def construct(self, x_):
  27. return self.drop(x_)
  28. @pytest.mark.level0
  29. @pytest.mark.platform_x86_gpu_training
  30. @pytest.mark.env_onecard
  31. def test_dropout():
  32. x_shape = [32, 16, 2, 5]
  33. x = np.ones(x_shape).astype(np.float32)
  34. keep_prob = 0.4
  35. dropout = Net(keep_prob)
  36. tx = Tensor(x)
  37. output, mask = dropout(tx)
  38. # check output
  39. output_np = output.asnumpy()
  40. elem_count = x.size
  41. nonzero_count = np.count_nonzero(output_np)
  42. assert (elem_count * (keep_prob - 0.1)) < nonzero_count < (elem_count * (keep_prob + 0.1))
  43. output_sum = np.sum(output_np)
  44. x_sum = np.sum(x)
  45. assert abs(output_sum - x_sum)/x_sum < 0.1
  46. # check mask
  47. mask_np = mask.asnumpy()
  48. mask_sum = np.sum(mask_np)
  49. assert np.count_nonzero(mask_np) == nonzero_count
  50. assert abs(mask_sum - nonzero_count)/nonzero_count < 0.1
  51. class DropoutDynamic(nn.Cell):
  52. def __init__(self, keep_prob):
  53. super(DropoutDynamic, self).__init__()
  54. self.test_dynamic = inner.GpuConvertToDynamicShape()
  55. self.drop = P.Dropout(keep_prob)
  56. def construct(self, x):
  57. x = self.test_dynamic(x)
  58. return self.drop(x)
  59. @pytest.mark.level0
  60. @pytest.mark.platform_x86_gpu_training
  61. @pytest.mark.env_onecard
  62. def test_dropout_dynamic():
  63. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  64. x_1 = np.ones([32, 16, 2, 5]).astype(np.float32)
  65. x_2 = np.ones([32, 16, 2, 5, 6]).astype(np.float32)
  66. keep_prob = 0.4
  67. net = DropoutDynamic(keep_prob)
  68. output_1, mask_1 = net(Tensor(x_1))
  69. elem_count_1 = x_1.size
  70. nonzero_count_1 = np.count_nonzero(output_1.asnumpy())
  71. assert (elem_count_1 * (keep_prob - 0.1)) < nonzero_count_1 < (elem_count_1 * (keep_prob + 0.1))
  72. output_sum_1 = np.sum(output_1.asnumpy())
  73. x_sum_1 = np.sum(x_1)
  74. assert abs(output_sum_1 - x_sum_1)/x_sum_1 < 0.1
  75. mask_sum_1 = np.sum(mask_1.asnumpy())
  76. assert np.count_nonzero(mask_1.asnumpy()) == nonzero_count_1
  77. assert abs(mask_sum_1 - nonzero_count_1)/nonzero_count_1 < 0.1
  78. output_2, mask_2 = net(Tensor(x_2))
  79. elem_count_2 = x_2.size
  80. nonzero_count_2 = np.count_nonzero(output_2.asnumpy())
  81. assert (elem_count_2 * (keep_prob - 0.1)) < nonzero_count_2 < (elem_count_2 * (keep_prob + 0.1))
  82. output_sum_2 = np.sum(output_2.asnumpy())
  83. x_sum_2 = np.sum(x_2)
  84. assert abs(output_sum_2 - x_sum_2)/x_sum_2 < 0.1
  85. mask_sum_2 = np.sum(mask_2.asnumpy())
  86. assert np.count_nonzero(mask_2.asnumpy()) == nonzero_count_2
  87. assert abs(mask_sum_2 - nonzero_count_2)/nonzero_count_2 < 0.1