test_shuffle_batch_op.py 2.7 KB
Newer Older
Z
zhoushiyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
"""This is unit test of Test shuffle_batch Op."""

import unittest
import numpy as np
import paddle.fluid as fluid
from op_test import OpTest
20
import os
Z
zhoushiyu 已提交
21 22


23
class TestShuffleBatchOpBase(OpTest):
24

25 26 27 28 29 30 31 32 33 34 35 36 37
    def gen_random_array(self, shape, low=0, high=1):
        rnd = (high - low) * np.random.random(shape) + low
        return rnd.astype(self.dtype)

    def get_shape(self):
        return (10, 10, 5)

    def _get_places(self):
        # NOTE: shuffle_batch is not supported on Windows
        if os.name == 'nt':
            return [fluid.CPUPlace()]
        return super(TestShuffleBatchOpBase, self)._get_places()

Z
zhoushiyu 已提交
38 39 40
    def setUp(self):
        self.op_type = 'shuffle_batch'
        self.dtype = np.float64
41 42
        self.shape = self.get_shape()
        x = self.gen_random_array(self.shape)
43 44
        seed = np.random.random_integers(low=10, high=100,
                                         size=(1, )).astype('int64')
45
        self.inputs = {'X': x, 'Seed': seed}
Z
zhoushiyu 已提交
46
        self.outputs = {
47 48 49
            'Out': np.array([]).astype(x.dtype),
            'ShuffleIdx': np.array([]).astype('int64'),
            'SeedOut': np.array([]).astype(seed.dtype),
Z
zhoushiyu 已提交
50 51 52 53 54 55 56
        }
        self.attrs = {'startup_seed': 1}

    def test_check_output(self):
        self.check_output_customized(self.verify_output)

    def verify_output(self, outs):
57 58 59 60 61
        x = np.copy(self.inputs['X'])
        y = None
        for out in outs:
            if out.shape == x.shape:
                y = np.copy(out)
Z
zhoushiyu 已提交
62
                break
63 64 65 66

        assert y is not None
        sort_x = self.sort_array(x)
        sort_y = self.sort_array(y)
67
        np.testing.assert_array_equal(sort_x, sort_y)
68 69 70 71 72 73 74

    def sort_array(self, array):
        shape = array.shape
        new_shape = [-1, shape[-1]]
        arr_list = np.reshape(array, new_shape).tolist()
        arr_list.sort(key=lambda x: x[0])
        return np.reshape(np.array(arr_list), shape)
Z
zhoushiyu 已提交
75 76 77 78 79

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


80
class TestShuffleBatchOp2(TestShuffleBatchOpBase):
81

82 83 84 85
    def get_shape(self):
        return (4, 30)


Z
zhoushiyu 已提交
86 87
if __name__ == '__main__':
    unittest.main()