test_pad_op.py 2.0 KB
Newer Older
D
dzhwinter 已提交
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

W
wanghaoshuang 已提交
15 16
import unittest
import numpy as np
W
wanghaoshuang 已提交
17
from op_test import OpTest
W
wanghaoshuang 已提交
18 19


W
wanghaoshuang 已提交
20
class TestPadOp(OpTest):
W
wanghaoshuang 已提交
21
    def setUp(self):
W
wanghaoshuang 已提交
22
        self.initTestCase()
W
wanghaoshuang 已提交
23
        self.op_type = "pad"
W
wanghaoshuang 已提交
24
        self.inputs = {'X': np.random.random(self.shape).astype("float32"), }
W
wanghaoshuang 已提交
25
        self.attrs = {}
W
wanghaoshuang 已提交
26 27
        self.attrs['paddings'] = np.array(self.paddings).flatten()
        self.attrs['pad_value'] = self.pad_value
W
wanghaoshuang 已提交
28 29
        self.outputs = {
            'Out': np.pad(self.inputs['X'],
W
wanghaoshuang 已提交
30
                          self.paddings,
W
wanghaoshuang 已提交
31
                          mode='constant',
W
wanghaoshuang 已提交
32
                          constant_values=self.pad_value)
W
wanghaoshuang 已提交
33 34
        }

W
wanghaoshuang 已提交
35 36 37 38
    def test_check_output(self):
        self.check_output()

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

W
wanghaoshuang 已提交
41 42 43
    def initTestCase(self):
        self.shape = (16, 16)
        self.paddings = [(0, 1), (2, 3)]
Y
Yang Yang(Tony) 已提交
44
        self.pad_value = 0.0
W
wanghaoshuang 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57


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) 已提交
58
        self.pad_value = 1.0
W
wanghaoshuang 已提交
59 60 61 62 63 64 65 66


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

W
wanghaoshuang 已提交
67 68 69

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