test_pad_op.py 1.1 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 25 26
        }


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

        self.check_grad(op, inputs, set(["X"]), "Out", max_relative_error=0.5)


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