首页 > 解决方案 > “无法将张量添加到批次:元素数量不匹配”,同时遍历数据集

问题描述

我在此处的 Colab 笔记本中包含了代码(直到迭代部分)。我把我的数据喂给了我train_dataset这样的人。

batch_size = 30

train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = (
    train_dataset.map(encode_single_sample, num_parallel_calls=tf.data.experimental.AUTOTUNE)
                 .batch(batch_size) #this one gives the error
                 .prefetch(buffer_size=tf.data.experimental.AUTOTUNE))

我得到一个InvalidArgumentError: Cannot add tensor to the batch: number of elements does not match。当我遍历数据集时,形状是:[张量]:[78],[批次]:[34] :

for batch in train_dataset.take(1): #error while iterating

上面给出了.batch(batch_size)错误。我注意到每次我再次运行代码时错误消息中的 [tensor] 和 [batch] 数字都会发生变化。当我更改batch_size为 1 时代码运行,但我也希望能够更改它。

如果有人可以调试这部分,将不胜感激

编辑 1:我在这里关注 Keras 的 OCR 教程,他们的代码运行良好。

标签: pythontensorflowkeras

解决方案


推荐阅读