我正在为Tensorflow中关于
map
的方法
Dataset
如所述
here
.示例
dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
dataset = dataset.map(lambda x: x + 2)
list(dataset.as_numpy_iterator())
工作正常,但通过应用来更改元素类型
地图
像
dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
dataset = dataset.map(lambda x: x / 10.0)
list(dataset.as_numpy_iterator())
产生错误消息
TypeError: `x` and `y` must have the same dtype, got tf.int32 != tf.float32.
因为应用的映射函数的返回类型与其输入类型不同。为什么会这样?不可能更改类型吗?如果是,我如何实现将数据集中的元素类型更改为
tf.float32
?
请注意,实际的数据集更为复杂,但这是说明该问题的最小示例。