test_data_feeder.py 3.4 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.

K
Kexin Zhao 已提交
15
import unittest
16

P
pangyoki 已提交
17
import paddle
18
import paddle.fluid as fluid
P
pangyoki 已提交
19 20

paddle.enable_static()
Y
Yu Yang 已提交
21 22


K
Kexin Zhao 已提交
23 24
class TestDataFeeder(unittest.TestCase):
    def test_lod_level_0_converter(self):
G
GGBond8488 已提交
25 26
        img = paddle.static.data(name='image', shape=[-1, 1, 28, 28])
        label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64')
K
Kexin Zhao 已提交
27 28 29 30 31
        feeder = fluid.DataFeeder([img, label], fluid.CPUPlace())
        result = feeder.feed([([0] * 784, [9]), ([1] * 784, [1])])

        self.assertEqual(result['image'].shape(), [2, 1, 28, 28])
        self.assertEqual(result['label'].shape(), [2, 1])
32 33
        self.assertEqual(result['image'].recursive_sequence_lengths(), [])
        self.assertEqual(result['label'].recursive_sequence_lengths(), [])
K
Kexin Zhao 已提交
34

S
sneaxiy 已提交
35 36 37 38 39 40
        try:
            result = feeder.feed([([0] * 783, [9]), ([1] * 783, [1])])
            self.assertTrue(False)
        except ValueError:
            self.assertTrue(True)

K
Kexin Zhao 已提交
41 42 43
    def test_lod_level_1_converter(self):
        # lod_level = 1
        # each sentence has a different number of words
G
GGBond8488 已提交
44 45
        sentences = paddle.static.data(
            name='sentences', shape=[-1, 1], dtype='int64', lod_level=1
46
        )
G
GGBond8488 已提交
47
        label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64')
K
Kexin Zhao 已提交
48 49 50 51 52
        feeder = fluid.DataFeeder([sentences, label], fluid.CPUPlace())

        # lod = [[0, 3, 5, 9]]
        # data = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
        # label = [1] * len(data)
53 54 55
        result = feeder.feed(
            [([1, 2, 3], [1]), ([4, 5], [1]), ([6, 7, 8, 9], [1])]
        )
K
Kexin Zhao 已提交
56 57 58

        self.assertEqual(result['sentences'].shape(), [9, 1])
        self.assertEqual(result['label'].shape(), [3, 1])
59 60 61
        self.assertEqual(
            result['sentences'].recursive_sequence_lengths(), [[3, 2, 4]]
        )
62
        self.assertEqual(result['label'].recursive_sequence_lengths(), [])
K
Kexin Zhao 已提交
63 64 65 66

    def test_lod_level_2_converter(self):
        # lod_level = 2
        # paragraphs -> sentences -> words
G
GGBond8488 已提交
67 68
        paragraphs = paddle.static.data(
            name='paragraphs', shape=[-1, 1], dtype='int64', lod_level=2
69
        )
G
GGBond8488 已提交
70
        label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64')
K
Kexin Zhao 已提交
71 72 73 74 75
        feeder = fluid.DataFeeder([paragraphs, label], fluid.CPUPlace())

        # lod = [[0, 2, 3], [0, 3, 5, 9]]
        # data = [[[1, 2, 3], [4, 5]], [[6, 7, 8, 9]]]
        # label = [1] * len(data)
76 77 78
        result = feeder.feed(
            [([[1, 2, 3], [4, 5]], [1]), ([[6, 7, 8, 9]], [1])]
        )
K
Kexin Zhao 已提交
79 80 81

        self.assertEqual(result['paragraphs'].shape(), [9, 1])
        self.assertEqual(result['label'].shape(), [2, 1])
82 83 84 85
        self.assertEqual(
            result['paragraphs'].recursive_sequence_lengths(),
            [[2, 1], [3, 2, 4]],
        )
86
        self.assertEqual(result['label'].recursive_sequence_lengths(), [])
Y
Yu Yang 已提交
87 88 89


if __name__ == '__main__':
K
Kexin Zhao 已提交
90
    unittest.main()