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 16import numpy as np 17import pytest 18 19import mindspore.context as context 20import mindspore.nn as nn 21from mindspore import Tensor 22from mindspore.ops import operations as P 23 24context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 25 26 27class Net(nn.Cell): 28 def __init__(self): 29 super(Net, self).__init__() 30 self.dropout = P.Dropout() 31 32 def construct(self, x): 33 return self.dropout(x) 34 35 36@pytest.mark.level1 37@pytest.mark.platform_x86_cpu 38@pytest.mark.env_onecard 39def test_net(): 40 x = np.random.randn(3, 3, 4).astype(np.float32) 41 dropout = Net() 42 output, mask = dropout(Tensor(x)) 43 print(x) 44 print(output) 45 print(mask) 46 47 48class Net1(nn.Cell): 49 def __init__(self): 50 super(Net1, self).__init__() 51 self.dropout = P.Dropout(keep_prob=0.1) 52 53 def construct(self, x): 54 return self.dropout(x) 55 56 57@pytest.mark.level1 58@pytest.mark.platform_x86_cpu 59@pytest.mark.env_onecard 60def test_net1(): 61 x = np.arange(0, 16).reshape(2, 2, 4).astype(np.float32) 62 dropout = Net1() 63 output, mask = dropout(Tensor(x)) 64 print(x) 65 print(output) 66 print(mask) 67 68 69class Net2(nn.Cell): 70 def __init__(self): 71 super(Net2, self).__init__() 72 self.dropout = P.Dropout(keep_prob=1.0) 73 74 def construct(self, x): 75 return self.dropout(x) 76 77 78@pytest.mark.level1 79@pytest.mark.platform_x86_cpu 80@pytest.mark.env_onecard 81def test_net2(): 82 x = np.arange(0, 12).reshape(3, 4).astype(np.float16) 83 dropout = Net2() 84 output, mask = dropout(Tensor(x)) 85 print(x) 86 print(output) 87 print(mask) 88 89 90if __name__ == '__main__': 91 test_net() 92 test_net1() 93 test_net2() 94