test_shuffle_batch_op.py 2.8 KB
Newer Older
Z
zhoushiyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 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."""

from __future__ import print_function, division
import unittest
import numpy as np
import paddle.fluid as fluid
import paddle.fluid.core as core
import paddle.fluid.layers as layers
from op_test import OpTest
23
import os
Z
zhoushiyu 已提交
24 25 26
import random


27 28 29 30 31 32 33 34 35 36 37 38 39 40
class TestShuffleBatchOpBase(OpTest):
    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 已提交
41 42 43
    def setUp(self):
        self.op_type = 'shuffle_batch'
        self.dtype = np.float64
44 45 46 47 48
        self.shape = self.get_shape()
        x = self.gen_random_array(self.shape)
        seed = np.random.random_integers(
            low=10, high=100, size=(1, )).astype('int64')
        self.inputs = {'X': x, 'Seed': seed}
Z
zhoushiyu 已提交
49
        self.outputs = {
50 51 52
            'Out': np.array([]).astype(x.dtype),
            'ShuffleIdx': np.array([]).astype('int64'),
            'SeedOut': np.array([]).astype(seed.dtype),
Z
zhoushiyu 已提交
53 54 55 56 57 58 59
        }
        self.attrs = {'startup_seed': 1}

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

    def verify_output(self, outs):
60 61 62 63 64
        x = np.copy(self.inputs['X'])
        y = None
        for out in outs:
            if out.shape == x.shape:
                y = np.copy(out)
Z
zhoushiyu 已提交
65
                break
66 67 68 69 70 71 72 73 74 75 76 77

        assert y is not None
        sort_x = self.sort_array(x)
        sort_y = self.sort_array(y)
        self.assertTrue(np.array_equal(sort_x, sort_y))

    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 已提交
78 79 80 81 82

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


83 84 85 86 87
class TestShuffleBatchOp2(TestShuffleBatchOpBase):
    def get_shape(self):
        return (4, 30)


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