首页 > 解决方案 > 如何让 tf.data.Dataset.from_tensor_slices 接受我的 dtype?

问题描述

# Convert to Tensor
imagepaths = tf.convert_to_tensor(imagepaths, dtype=tf.string)
labels = tf.convert_to_tensor(labels, dtype=tf.int32)
# Build a TF Queue, shuffle data
image, label = tf.data.Dataset.from_tensor_slices([imagepaths, labels])

因此,以下代码是我在 Tensorflow 2 中使用的代码,我不断更改我转换为的类型,但是无论我使用哪种,它都会不断地给我错误。有任何想法吗?下面我列出了我得到的一些错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: cannot compute Pack as input #1(zero-based) was expected to be a string tensor but is a int32 tensor [Op:Pack] name: component_0


return ops.EagerTensor(value, handle, device, dtype)
TypeError: Cannot convert provided value to EagerTensor

标签: pythontensorflowdtype

解决方案


您可以通过切片两个张量的元组将两个张量组合成一个 Dataset 对象。像这样:

# Convert to Tensor
imagepaths = tf.convert_to_tensor(imagepaths, dtype=tf.string)
labels = tf.convert_to_tensor(labels, dtype=tf.int32)
# Build a TF Queue, shuffle data
dataset = tf.data.Dataset.from_tensor_slices((imagepaths, labels))

请注意,张量在它们的第一维中应该具有相同的大小。


推荐阅读