首页 > 解决方案 > 如何为列表数据和数组标签的tensoflow模型列表制作输入管道

问题描述

我有一个列表格式的训练数据及其数组格式的标签。从这些中组合 tf.data.Dataset 的最佳方法是什么。以下是我的示例代码。

for i in range(100):
  ind = np.random.randint(0,10)
  x = np.random.randint(0, 19, (ind, ))
  lst.append(x)

y = np.random.randint(0,100, (100,))


data = tf.data.Dataset.from_generator(lambda : (lst, y), output_types=( tf.int32, tf.int32), output_shapes = ((None, ), ( )))

for i in data.padded_batch(10):
  print(i)

但我不断收到此错误

generator` 产生了一个与预期结构不匹配的元素。预期的结构是 (tf.int32, tf.int32),但产生的元素是 [array([], dtype=int64), array([ 0, 14, 6, 2, 5, 13]), array([ 0, 11, 8, 11]), 数组([ 6, 9, 0, 17, 12, 10]), 数组([12, 3, 8]), 数组([11, 9]), 数组([ 3, 11, 17, 1]), 数组([], dtype=int64), 数组([ 0, 12, 3]), 数组([ 6, 3, 0, 13, 2, 6, 12, 17, 12]), 数组([ 2, 9, 1, 17, 4, 17]), 数组([15, 17, 4, 12, 2, 6, 10, 15, 7]), 数组([16, 5 , 2, 11]), 数组([11, 11, 10, 1, 11, 9, 9]), 数组([10, 5, 9]), 数组([3, 7, 1, 4, 6, 6]), 数组([], dtype=int64), 数组([15, 6, 6, 3, 0, 16, 1, 7, 10]), 数组([ 9, 15, 16, 1, 6, 8, 16, 13, 4]), 数组([13, 0]), 数组([2]), 数组([ 0, 14, 11, 9, 13, 3, 14, 15]), 数组([ ], dtype=int64), 数组([14, 9, 3, 10, 2]), 数组([1]),

标签: pythonnumpytensorflow

解决方案


推荐阅读