test_pad_op.py 2.0 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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.

15 16
from __future__ import print_function

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


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

W
wanghaoshuang 已提交
37 38 39 40
    def test_check_output(self):
        self.check_output()

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

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


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


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

W
wanghaoshuang 已提交
69 70 71

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