python - 如何让 tf.data.Dataset.from_tensor_slices 接受我的 dtype?

标签 python tensorflow 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

最佳答案

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

# 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))

请注意,张量的第一维大小应相同。

关于python - 如何让 tf.data.Dataset.from_tensor_slices 接受我的 dtype?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60995257/

相关文章:

python - 如何在 Jupyter Notebook 中正确设置 SparkContext 的配置?

python - 如何将 numpy 和 pandas 日期时间对象转换为数字?

python下划线函数名

python - 相同的 Keras 脚本可以在 WSL (Ubuntu) 中运行,但不能在 Conda 中运行?

python - Pandas 将 csv 读取为字符串类型

python - 如何使 dockerized Flask 应用程序将输出写入文件?

python - 如何在 Tensorflow 2.0 中保存在 GPU 上训练的模型以加载到 CPU 上

python - Tensorflow/Keras : Input 0 of layer lstm is incompatible with the layer: expected ndim=3, 发现 ndim=2

python - 优化器最小化错误: 'float' object has no attribute 'dtype'

python - 类型错误 : data type not understood while parsing CSV with Pandas