I am struggling with a detail in Tensorflow regarding the map
method of Dataset
as described here. The example
dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
dataset = dataset.map(lambda x: x + 2)
list(dataset.as_numpy_iterator())
works fine, but changing the element type by applying map
as
dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
dataset = dataset.map(lambda x: x / 10.0)
list(dataset.as_numpy_iterator())
yields the error message
TypeError: `x` and `y` must have the same dtype, got tf.int32 != tf.float32.
because the return type of the applied map function is not the same as its input type. Why is that the case? Is it impossible to change the type? If so, how can I achieve the desired result of changing the element type in the dataset to tf.float32
?
Note that the actual dataset is more complex, but this is a minimum example illustrating the issue.
I finally found it out myself. The issue is not related to map
at all, but for the division an explicit cast is needed.
dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
dataset = dataset.map(lambda x: tf.cast(x, tf.float32) / 10.0)
list(dataset.as_numpy_iterator())