问题描述
限时送ChatGPT账号..我有一长串整数列表(代表句子,每个大小不同),我想使用 tf.data 库提供这些列表.每个列表(列表的列表)都有不同的长度,我得到一个错误,我可以在这里重现:
I have a long list of lists of integers (representing sentences, each one of different sizes) that I want to feed using the tf.data library. Each list (of the lists of list) has different length, and I get an error, which I can reproduce here:
t = [[4,2], [3,4,5]]
dataset = tf.data.Dataset.from_tensor_slices(t)
我得到的错误是:
ValueError: Argument must be a dense tensor: [[4, 2], [3, 4, 5]] - got shape [2], but wanted [2, 2].
有没有办法做到这一点?
Is there a way to do this?
编辑 1:明确地说,我不想填充列表的输入列表(它是一个包含超过一百万个元素的句子列表,长度不同)我想使用 tf.data 库来提供,以适当的方式,具有不同长度的列表的列表.
EDIT 1: Just to be clear, I don't want to pad the input list of lists (it's a list of sentences containing over a million elements, with varying lengths) I want to use the tf.data library to feed, in a proper way, a list of lists with varying length.
推荐答案
您可以使用 tf.data.Dataset.from_generator()
将任何可迭代的 Python 对象(如列表列表)转换为 Dataset
:
t = [[4, 2], [3, 4, 5]]
dataset = tf.data.Dataset.from_generator(lambda: t, tf.int32, output_shapes=[None])
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()
with tf.Session() as sess:
print(sess.run(next_element)) # ==> '[4, 2]'
print(sess.run(next_element)) # ==> '[3, 4, 5]'
这篇关于如何在 tf.data.Dataset 中输入不同大小的列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论