test_maxout_op.py 3.3 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 20
import paddle.fluid as fluid
import paddle.fluid.core as core
21
from op_test import OpTest
W
wanghaox 已提交
22 23


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


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

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

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

    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]
54
        self.groups = 2
55 56 57 58 59 60 61 62 63 64 65
        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


66
class TestMaxOutOpAxisAPI(unittest.TestCase):
67 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
    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 已提交
97 98 99 100


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