提交 d73f2bd6 编写于 作者: K Kexin Zhao 提交者: Kexin Zhao

fix data_feeder lod bug

上级 5c58eee5
...@@ -54,9 +54,9 @@ class DataToLoDTensorConverter(object): ...@@ -54,9 +54,9 @@ class DataToLoDTensorConverter(object):
self.data.append(data) self.data.append(data)
else: else:
cur_lod_len = len(data) cur_lod_len = len(data)
lod[-1].append(lod[-1][-1] + cur_lod_len) lod[0].append(lod[0][-1] + cur_lod_len)
for each_data in data: for each_data in data:
self._feed_impl_(each_data, lod[:-1], lod_level - 1) self._feed_impl_(each_data, lod[1:], lod_level - 1)
def done(self): def done(self):
arr = numpy.array(self.data, dtype=self.dtype).reshape(self.shape) arr = numpy.array(self.data, dtype=self.dtype).reshape(self.shape)
......
...@@ -13,15 +13,62 @@ ...@@ -13,15 +13,62 @@
# limitations under the License. # limitations under the License.
import paddle.fluid as fluid import paddle.fluid as fluid
import unittest
def test_converter(): class TestDataFeeder(unittest.TestCase):
img = fluid.layers.data(name='image', shape=[1, 28, 28]) def test_lod_level_0_converter(self):
label = fluid.layers.data(name='label', shape=[1], dtype='int64') img = fluid.layers.data(name='image', shape=[1, 28, 28])
feeder = fluid.DataFeeder([img, label], fluid.CPUPlace()) label = fluid.layers.data(name='label', shape=[1], dtype='int64')
result = feeder.feed([[[0] * 784, [9]], [[1] * 784, [1]]]) feeder = fluid.DataFeeder([img, label], fluid.CPUPlace())
print(result) result = feeder.feed([([0] * 784, [9]), ([1] * 784, [1])])
print(result)
self.assertEqual(result['image'].shape(), [2, 1, 28, 28])
self.assertEqual(result['label'].shape(), [2, 1])
self.assertEqual(result['image'].lod(), [])
self.assertEqual(result['label'].lod(), [])
def test_lod_level_1_converter(self):
# lod_level = 1
# each sentence has a different number of words
sentences = fluid.layers.data(
name='sentences', shape=[1], dtype='int64', lod_level=1)
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
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)
result = feeder.feed(
[([1, 2, 3], [1]), ([4, 5], [1]), ([6, 7, 8, 9], [1])])
print(result)
self.assertEqual(result['sentences'].shape(), [9, 1])
self.assertEqual(result['label'].shape(), [3, 1])
self.assertEqual(result['sentences'].lod(), [[0, 3, 5, 9]])
self.assertEqual(result['label'].lod(), [])
def test_lod_level_2_converter(self):
# lod_level = 2
# paragraphs -> sentences -> words
paragraphs = fluid.layers.data(
name='paragraphs', shape=[1], dtype='int64', lod_level=2)
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
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)
result = feeder.feed(
[([[1, 2, 3], [4, 5]], [1]), ([[6, 7, 8, 9]], [1])])
print(result)
self.assertEqual(result['paragraphs'].shape(), [9, 1])
self.assertEqual(result['label'].shape(), [2, 1])
self.assertEqual(result['paragraphs'].lod(), [[0, 2, 3], [0, 3, 5, 9]])
self.assertEqual(result['label'].lod(), [])
if __name__ == '__main__': if __name__ == '__main__':
test_converter() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册