test_maxout_op.py 3.9 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
wanghaox 已提交
17 18
import unittest
import numpy as np
19
import paddle.fluid as fluid
20
from paddle.fluid import Program, program_guard
21
import paddle.fluid.core as core
22
from op_test import OpTest
W
wanghaox 已提交
23 24


25
def maxout_forward_naive(input, groups, channel_axis):
W
wanghaox 已提交
26
    s0, s1, s2, s3 = input.shape
27 28 29
    if channel_axis == 3:
        return np.ndarray([s0, s1, s2, s3 // groups, groups], \
            buffer = input, dtype=input.dtype).max(axis=(4))
M
minqiyang 已提交
30
    return np.ndarray([s0, s1 // groups, groups, s2, s3], \
W
wanghaox 已提交
31 32 33 34 35 36 37
        buffer = input, dtype=input.dtype).max(axis=(2))


class TestMaxOutOp(OpTest):
    def setUp(self):
        self.op_type = "maxout"
        self.init_test_case()
38 39
        input = np.random.random(self.shape)
        output = self.MaxOut_forward_naive(input, self.groups, self.axis)
W
wanghaox 已提交
40 41

        self.inputs = {'X': input}
42
        self.attrs = {'groups': self.groups, 'axis': self.axis}
W
wanghaox 已提交
43

44
        self.outputs = {'Out': output}
W
wanghaox 已提交
45 46 47 48 49 50 51 52 53 54

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')

    def init_test_case(self):
        self.MaxOut_forward_naive = maxout_forward_naive
        self.shape = [100, 6, 2, 2]
55
        self.groups = 2
56 57 58 59 60 61 62 63 64 65 66
        self.axis = 1


class TestMaxOutOpAxis(TestMaxOutOp):
    def init_test_case(self):
        self.MaxOut_forward_naive = maxout_forward_naive
        self.shape = [100, 2, 2, 6]  # NHWC format
        self.groups = 2
        self.axis = 3


67
class TestMaxOutOpAxisAPI(unittest.TestCase):
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    def test_axis(self):
        data1 = fluid.data(name='data1', shape=[3, 6, 2, 2], dtype='float32')
        data2 = fluid.data(name='data2', shape=[3, 2, 2, 6], dtype='float32')
        out1 = fluid.layers.maxout(data1, groups=2, axis=1)
        out2 = fluid.layers.maxout(data2, groups=2, axis=-1)
        data1_np = np.random.random((3, 6, 2, 2)).astype("float32")
        data2_np = np.transpose(data1_np, [0, 2, 3, 1])

        if core.is_compiled_with_cuda():
            place = core.CUDAPlace(0)
        else:
            place = core.CPUPlace()
        exe = fluid.Executor(place)
        exe.run(fluid.default_startup_program())
        results = exe.run(fluid.default_main_program(),
                          feed={"data1": data1_np,
                                "data2": data2_np},
                          fetch_list=[out1, out2],
                          return_numpy=True)

        self.assertTrue(
            np.allclose(results[0], np.transpose(results[1], (0, 3, 1, 2))))

    def test_exception(self):
        input = fluid.data(name="input", shape=[2, 4, 6, 6], dtype="float32")

        def _attr_axis():
            out = fluid.layers.maxout(input, groups=2, axis=2)

        self.assertRaises(ValueError, _attr_axis)
W
wanghaox 已提交
98 99


100 101 102 103 104 105 106 107 108 109 110 111 112
class TestMaxOutOpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program()):
            # The input type must be Variable.
            self.assertRaises(TypeError, fluid.layers.maxout, 1, 2)
            # The input dtype must be float16, float32, float64.
            x_int32 = fluid.data(name='x_int32', shape=[12, 10], dtype='int32')
            self.assertRaises(TypeError, fluid.layers.maxout, x_int32, 2)
            # support the input dtype is float32
            x_fp32 = fluid.data(name='x_fp32', shape=[12, 10], dtype='float32')
            fluid.layers.maxout(x_fp32, 2)


W
wanghaox 已提交
113 114
if __name__ == '__main__':
    unittest.main()