如何在 tf.data.Dataset 中输入不同大小的列表列表

编程入门 行业动态 更新时间:2024-10-15 02:32:08
本文介绍了如何在 tf.data.Dataset 中输入不同大小的列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-05-01 05:28:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   大小   如何在   data   tf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!