1# Copyright 2022 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 18from mindspore import Tensor 19import mindspore 20from mindspore import nn 21import mindspore.ops as ops 22import mindspore.context as context 23 24 25class Net(nn.Cell): 26 """Net used to test nn.Dropout1d""" 27 def __init__(self, p): 28 super(Net, self).__init__() 29 self.dropout1d = nn.Dropout1d(p) 30 31 def construct(self, x): 32 return self.dropout1d(x) 33 34 35class FNet(nn.Cell): 36 """Net used to test ops.dropout1d""" 37 def __init__(self, p): 38 super(FNet, self).__init__() 39 self.p = p 40 41 def construct(self, x): 42 out = ops.dropout1d(x, self.p) 43 return out 44 45 46@pytest.mark.level2 47@pytest.mark.platform_x86_cpu 48@pytest.mark.platform_arm_cpu 49@pytest.mark.platform_x86_gpu_training 50@pytest.mark.env_onecard 51@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE]) 52def test_dropout1d(mode): 53 """ 54 Feature: dropout1d 55 Description: Verify the result of Dropout1d 56 Expectation: success 57 """ 58 context.set_context(mode=mode) 59 x = np.random.randn(4, 3) 60 dropout = Net(p=1.0) 61 x = Tensor(x, mindspore.float32) 62 dropout.set_train() 63 output = dropout(x) 64 expect = np.zeros((4, 3)) 65 np.allclose(output.asnumpy(), expect) 66 67 68@pytest.mark.level2 69@pytest.mark.platform_x86_cpu 70@pytest.mark.platform_arm_cpu 71@pytest.mark.platform_x86_gpu_training 72@pytest.mark.env_onecard 73@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE]) 74def test_f_dropout1d(mode): 75 """ 76 Feature: function api dropout1d 77 Description: Verify the result of dropout1d 78 Expectation: success 79 """ 80 context.set_context(mode=mode) 81 x = np.random.randn(4, 3) 82 x = Tensor(x, mindspore.float32) 83 net = FNet(p=1.0) 84 output = net(x) 85 expect = np.zeros((4, 3)) 86 np.allclose(output.asnumpy(), expect) 87