test_dropout_op.py 1.7 KB
Newer Older
1 2 3 4 5 6
import unittest
import numpy as np
from gradient_checker import GradientChecker, create_op
from op_test_util import OpTestMeta


7
class TestDropoutOpWithProbZero(unittest.TestCase):
8 9 10 11 12 13 14 15 16
    __metaclass__ = OpTestMeta

    def setUp(self):
        self.type = "dropout"
        self.inputs = {'X': np.random.random((32, 64)).astype("float32")}
        self.attrs = {'dropout_prob': 0.0}
        self.outputs = {'Out': self.inputs['X'], 'Mask': np.ones((32, 64))}


17
class TestDropoutOpWithProbOne(unittest.TestCase):
18 19 20 21 22 23 24 25 26
    __metaclass__ = OpTestMeta

    def setUp(self):
        self.type = "dropout"
        self.inputs = {'X': np.random.random((32, 64)).astype("float32")}
        self.attrs = {'dropout_prob': 1.0}
        self.outputs = {'Out': np.zeros((32, 64)), 'Mask': np.zeros((32, 64))}


27 28 29 30 31 32 33 34 35 36
class TestDropoutOpWithRank3(unittest.TestCase):
    __metaclass__ = OpTestMeta

    def setUp(self):
        self.type = "dropout"
        self.inputs = {'X': np.random.random((32, 64, 8)).astype("float32")}
        self.attrs = {'dropout_prob': 0.0}
        self.outputs = {'Out': self.inputs['X'], 'Mask': np.ones((32, 64, 8))}


X
Xinghai Sun 已提交
37
class TestDropoutGradOp(GradientChecker):
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    def setUp(self):
        self.op = create_op("dropout")
        self.inputs = {'X': np.random.random((10, 5)).astype("float32")}

    def test_cpu_gpu_compare(self):
        self.compare_grad(self.op, self.inputs)

    def test_normal(self):
        self.check_grad(self.op, self.inputs, set(["X"]), "Out")


class TestDropoutGradOpWithRank3(TestDropoutGradOp):
    def setUp(self):
        self.op = create_op("dropout")
        self.inputs = {'X': np.random.random((10, 5, 4)).astype("float32")}
53 54 55 56


if __name__ == '__main__':
    unittest.main()