test_pad_op.py 2.5 KB
Newer Older
W
wanghaoshuang 已提交
1 2
import unittest
import numpy as np
W
wanghaoshuang 已提交
3
from paddle.v2.framework.op import Operator
W
wanghaoshuang 已提交
4 5 6 7 8 9 10 11
from gradient_checker import GradientChecker, create_op
from op_test_util import OpTestMeta


class TestPadOp(unittest.TestCase):
    __metaclass__ = OpTestMeta

    def setUp(self):
W
wanghaoshuang 已提交
12
        self.initTestCase()
W
wanghaoshuang 已提交
13
        self.type = "pad"
W
wanghaoshuang 已提交
14
        self.inputs = {'X': np.random.random(self.shape).astype("float32"), }
W
wanghaoshuang 已提交
15
        self.attrs = {}
W
wanghaoshuang 已提交
16 17
        self.attrs['paddings'] = np.array(self.paddings).flatten()
        self.attrs['pad_value'] = self.pad_value
W
wanghaoshuang 已提交
18 19
        self.outputs = {
            'Out': np.pad(self.inputs['X'],
W
wanghaoshuang 已提交
20
                          self.paddings,
W
wanghaoshuang 已提交
21
                          mode='constant',
W
wanghaoshuang 已提交
22
                          constant_values=self.pad_value)
W
wanghaoshuang 已提交
23 24
        }

W
wanghaoshuang 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    def initTestCase(self):
        self.shape = (16, 16)
        self.paddings = [(0, 1), (2, 3)]
        self.pad_value = 0


class TestCase1(TestPadOp):
    def initTestCase(self):
        self.shape = (2, 3, 4, 4)
        self.paddings = [(0, 1), (2, 3), (2, 1), (1, 1)]
        self.pad_value = 0.5


class TestCase2(TestPadOp):
    def initTestCase(self):
        self.shape = (2, 2, 2)
        self.paddings = [(0, 0), (0, 0), (1, 2)]
        self.pad_value = 1


class TestCase3(TestPadOp):
    def initTestCase(self):
        self.shape = (8)
        self.paddings = [(0, 1)]
        self.pad_value = 0.9

W
wanghaoshuang 已提交
51

W
wanghaoshuang 已提交
52 53
class TestPadGradOp(GradientChecker):
    def setUp(self):
W
wanghaoshuang 已提交
54
        self.initTestCase()
W
wanghaoshuang 已提交
55
        self.op = Operator(
W
wanghaoshuang 已提交
56 57 58
            type="pad",
            X="X",
            Out="Out",
W
wanghaoshuang 已提交
59 60 61 62 63 64 65 66
            paddings=np.array(self.paddings).flatten(),
            pad_value=self.pad_value)
        self.inputs = {'X': np.random.random(self.shape).astype("float32"), }

    def initTestCase(self):
        self.shape = (16, 16)
        self.paddings = [(0, 1), (2, 3)]
        self.pad_value = 0
W
wanghaoshuang 已提交
67 68

    def test_normal(self):
W
wanghaoshuang 已提交
69
        self.check_grad(self.op, self.inputs, set(["X"]), "Out")
W
wanghaoshuang 已提交
70

W
wanghaoshuang 已提交
71 72
    def test_cpu_gpu_compare(self):
        self.compare_grad(self.op, self.inputs)
W
wanghaoshuang 已提交
73 74


W
wanghaoshuang 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
class TestiGradCase1(TestPadOp):
    def initTestCase(self):
        self.shape = (2, 3, 4, 4)
        self.paddings = [(0, 1), (2, 3), (2, 1), (1, 1)]
        self.pad_value = 0.5


class TestGradCase2(TestPadOp):
    def initTestCase(self):
        self.shape = (2, 2, 2)
        self.paddings = [(0, 0), (0, 0), (1, 2)]
        self.pad_value = 1


class TestGradCase3(TestPadOp):
    def initTestCase(self):
        self.shape = (8)
        self.paddings = [(0, 1)]
        self.pad_value = 0.9


W
wanghaoshuang 已提交
96 97
if __name__ == '__main__':
    unittest.main()