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_op.py 2.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  22. class Net(nn.Cell):
  23. def __init__(self):
  24. super(Net, self).__init__()
  25. self.dropout = P.Dropout()
  26. def construct(self, x):
  27. return self.dropout(x)
  28. @pytest.mark.level0
  29. @pytest.mark.platform_x86_cpu
  30. @pytest.mark.env_onecard
  31. def test_net():
  32. x = np.random.randn(3, 3, 4).astype(np.float32)
  33. dropout = Net()
  34. output, mask = dropout(Tensor(x))
  35. print(x)
  36. print(output)
  37. print(mask)
  38. class Net1(nn.Cell):
  39. def __init__(self):
  40. super(Net1, self).__init__()
  41. self.dropout = P.Dropout(keep_prob=0.1)
  42. def construct(self, x):
  43. return self.dropout(x)
  44. @pytest.mark.level0
  45. @pytest.mark.platform_x86_cpu
  46. @pytest.mark.env_onecard
  47. def test_net1():
  48. x = np.arange(0, 16).reshape(2, 2, 4).astype(np.float32)
  49. dropout = Net1()
  50. output, mask = dropout(Tensor(x))
  51. print(x)
  52. print(output)
  53. print(mask)
  54. class Net2(nn.Cell):
  55. def __init__(self):
  56. super(Net2, self).__init__()
  57. self.dropout = P.Dropout(keep_prob=1.0)
  58. def construct(self, x):
  59. return self.dropout(x)
  60. @pytest.mark.level0
  61. @pytest.mark.platform_x86_cpu
  62. @pytest.mark.env_onecard
  63. def test_net2():
  64. x = np.arange(0, 12).reshape(3, 4).astype(np.float16)
  65. dropout = Net2()
  66. output, mask = dropout(Tensor(x))
  67. print(x)
  68. print(output)
  69. print(mask)
  70. if __name__ == '__main__':
  71. test_net()
  72. test_net1()
  73. test_net2()