python - 如何让 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
解决方案
您可以通过切片两个张量的元组将两个张量组合成一个 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))
请注意,张量在它们的第一维中应该具有相同的大小。
推荐阅读
- javascript - 谷歌地图移动应用(共享标记公开)
- html - 如何将标签中的两个图像彼此对齐?
- .htaccess - 为特定域重定向 301 htaccess
- css - 光滑的滑块在其他元素下移动
- python - 为什么我在 python 列表中出现索引错误?
- c - 任何可用于 C 中无符号字符指针行为的文档?
- react-native - TypeError:未定义不是对象 - AsyncStorage
- c++ - QColor 到人类可读的字符串
- python - 根据星期几查找每小时平均数据,以模拟缺失日期的数据
- pandas - Pandas Dataframe 使用分类数据创建 Seaborn 水平条形图