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""" test_dropout """ 16import numpy as np 17import pytest 18 19import mindspore.nn as nn 20from mindspore import Tensor 21from mindspore import context 22from mindspore import dtype as mstype 23from mindspore.ops.operations import _grad_ops as P 24 25 26context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 27 28 29class Net(nn.Cell): 30 def __init__(self, keep_prob=0.5): 31 super(Net, self).__init__() 32 self.dropout_grad = P.DropoutGrad(keep_prob) 33 34 def construct(self, output, mask): 35 return self.dropout_grad(output, mask) 36 37 38@pytest.mark.level0 39@pytest.mark.platform_x86_cpu 40@pytest.mark.env_onecard 41def test_dropout_grad_001(): 42 in_tensor = Tensor(np.array([[[3., 1., 2.]], \ 43 [[4., 1., 4.]]]), mstype.float32) 44 in_mask = Tensor(np.array([[[1., 0, 0]], [[1., 1., 0]]]), mstype.float32) 45 dropout_grad = Net() 46 output = dropout_grad(in_tensor, in_mask) 47 print("output:\n", output) 48 49 expect = np.array([[[6., 0., 0.]], [[8., 2., 0.]]]).astype(np.float32) 50 error = np.ones(shape=[2, 3]) * 1.0e-6 51 52 diff = np.abs(output.asnumpy() - expect) 53 assert np.all(np.abs(diff) < error) 54 55 56@pytest.mark.level0 57@pytest.mark.platform_x86_cpu 58@pytest.mark.env_onecard 59def test_dropout_grad_002(): 60 in_tensor = Tensor(np.array([[[3., 1., 2.]], [[4., 1., 4.]]]), mstype.float16) 61 in_mask = Tensor(np.array([[[1., 0, 0]], [[1., 1., 0]]]), mstype.float16) 62 dropout_grad = Net() 63 output = dropout_grad(in_tensor, in_mask) 64 print("output:\n", output) 65 66 expect = np.array([[[6., 0., 0.]], [[8., 2., 0.]]]).astype(np.float16) 67 error = np.ones(shape=[2, 3]) * 1.0e-6 68 69 diff = np.abs(output.asnumpy() - expect) 70 assert np.all(np.abs(diff) < error) 71 72 73@pytest.mark.level0 74@pytest.mark.platform_x86_cpu 75@pytest.mark.env_onecard 76def test_dropout_grad_003(): 77 in_tensor = Tensor(np.array([[[3., 1., 2.], [3., 1., 2.]], \ 78 [[4., 1., 4.], [4., 1., 4.]]]), mstype.float16) 79 in_mask = Tensor(np.array([[[1., 0, 0], [1., 0, 0]], \ 80 [[1., 1., 0], [1., 1., 0]]]), mstype.float16) 81 dropout_grad = Net() 82 output = dropout_grad(in_tensor, in_mask) 83 print("output:\n", output) 84 85 expect = np.array([[[6., 0., 0.], [6., 0., 0.]], \ 86 [[8., 2., 0.], [8., 2., 0.]]]).astype(np.float16) 87 error = np.ones(shape=[2, 2, 3]) * 1.0e-6 88 89 diff = np.abs(output.asnumpy() - expect) 90 assert np.all(np.abs(diff) < error) 91 92 93@pytest.mark.level0 94@pytest.mark.platform_x86_cpu 95@pytest.mark.env_onecard 96def test_dropout_grad_004(): 97 in_tensor = Tensor(np.array([[6.]]), mstype.float32) 98 in_mask = Tensor(np.array([[1.]]), mstype.float32) 99 dropout_grad = Net(1.) 100 output = dropout_grad(in_tensor, in_mask) 101 print("output:\n", output) 102 103 expect = np.array([[6.]]).astype(np.float32) 104 error = np.ones(shape=[1]) * 1.0e-6 105 106 diff = np.abs(output.asnumpy() - expect) 107 assert np.all(np.abs(diff) < error) 108 109 110@pytest.mark.skip(reason='0 in shape is not support') 111@pytest.mark.level0 112@pytest.mark.platform_x86_cpu 113@pytest.mark.env_onecard 114def test_dropout_grad_005(): 115 in_tensor = Tensor(np.array([[]]), mstype.float32) 116 in_mask = Tensor(np.array([[]]), mstype.float32) 117 dropout_grad = Net(1.) 118 output = dropout_grad(in_tensor, in_mask) 119 print("output:\n", output) 120 121 expect = np.array([[]]).astype(np.float32) 122 error = np.ones(shape=[]) * 1.0e-6 123 124 diff = np.abs(output.asnumpy() - expect) 125 assert np.all(np.abs(diff) < error) 126 127 128@pytest.mark.level0 129@pytest.mark.platform_x86_cpu 130@pytest.mark.env_onecard 131def test_dropout_grad_006(): 132 in_tensor = Tensor(np.array([[[3., 1., 2.]], [[4., 1., 4.]]]), mstype.float16) 133 in_mask = Tensor(np.array([[[1., 0, 0]], [[0., 0., 1.]]]), mstype.float16) 134 dropout_grad = Net(0.3333333333) 135 output = dropout_grad(in_tensor, in_mask) 136 print("output:\n", output) 137 138 expect = np.array([[[9., 0., 0.]], [[0., 0., 12.]]]).astype(np.float16) 139 error = np.ones(shape=[2, 3]) * 1.0e-6 140 141 diff = np.abs(output.asnumpy() - expect) 142 assert np.all(np.abs(diff) < error) 143