test_shuffle_batch_op.py 2.6 KB
Newer Older
Z
zhoushiyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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."""

16
import os
Z
zhoushiyu 已提交
17
import unittest
18

Z
zhoushiyu 已提交
19
import numpy as np
W
wanghuancoder 已提交
20
from eager_op_test import OpTest
21

22
from paddle import fluid
Z
zhoushiyu 已提交
23 24


25 26 27 28 29 30 31 32 33 34 35 36
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()]
37
        return super()._get_places()
38

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

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

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

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

    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 已提交
77 78

    def test_check_grad(self):
79
        self.check_grad(['X'], 'Out', check_dygraph=False)
Z
zhoushiyu 已提交
80 81


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


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