test_pad_op.py 1.2 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 12 13
from gradient_checker import GradientChecker, create_op
from op_test_util import OpTestMeta


class TestPadOp(unittest.TestCase):
    __metaclass__ = OpTestMeta

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


W
wanghaoshuang 已提交
25 26 27
class TestPadGradOp(GradientChecker):
    def setUp(self):
        self.op = Operator(
W
wanghaoshuang 已提交
28 29 30 31 32
            type="pad",
            X="X",
            Out="Out",
            paddings=[(0, 1), (2, 3)],
            pad_value=0)
W
wanghaoshuang 已提交
33 34 35 36 37
        self.inputs = {'X': np.random.random((16, 16)).astype("float32"), }

    def test_normal(self):
        self.check_grad(
            self.op, self.inputs, set(["X"]), "Out", max_relative_error=0.5)
W
wanghaoshuang 已提交
38

W
wanghaoshuang 已提交
39 40
    def test_cpu_gpu_compare(self):
        self.compare_grad(self.op, self.inputs)
W
wanghaoshuang 已提交
41 42 43 44


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