test_pad_op.py 1.4 KB
Newer Older
W
wanghaoshuang 已提交
1 2
import unittest
import numpy as np
W
wanghaoshuang 已提交
3
from op_test import OpTest
W
wanghaoshuang 已提交
4 5


W
wanghaoshuang 已提交
6
class TestPadOp(OpTest):
W
wanghaoshuang 已提交
7
    def setUp(self):
W
wanghaoshuang 已提交
8
        self.initTestCase()
W
wanghaoshuang 已提交
9
        self.op_type = "pad"
W
wanghaoshuang 已提交
10
        self.inputs = {'X': np.random.random(self.shape).astype("float32"), }
W
wanghaoshuang 已提交
11
        self.attrs = {}
W
wanghaoshuang 已提交
12 13
        self.attrs['paddings'] = np.array(self.paddings).flatten()
        self.attrs['pad_value'] = self.pad_value
W
wanghaoshuang 已提交
14 15
        self.outputs = {
            'Out': np.pad(self.inputs['X'],
W
wanghaoshuang 已提交
16
                          self.paddings,
W
wanghaoshuang 已提交
17
                          mode='constant',
W
wanghaoshuang 已提交
18
                          constant_values=self.pad_value)
W
wanghaoshuang 已提交
19 20
        }

W
wanghaoshuang 已提交
21 22 23 24
    def test_check_output(self):
        self.check_output()

    def test_check_grad_normal(self):
W
wanghaoshuang 已提交
25
        self.check_grad(['X'], 'Out', max_relative_error=0.006)
W
wanghaoshuang 已提交
26

W
wanghaoshuang 已提交
27 28 29
    def initTestCase(self):
        self.shape = (16, 16)
        self.paddings = [(0, 1), (2, 3)]
Y
Yang Yang(Tony) 已提交
30
        self.pad_value = 0.0
W
wanghaoshuang 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43


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)]
Y
Yang Yang(Tony) 已提交
44
        self.pad_value = 1.0
W
wanghaoshuang 已提交
45 46 47 48 49 50 51 52


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

W
wanghaoshuang 已提交
53 54 55

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