I'm trying to create a tensorflow dataset using from_tensor_slices
but the string I'm passing is getting messed up within the map function:
def test(x):
print(x)
return x
filenames=['a','b','c']
dataset = tf.data.Dataset.from_tensor_slices(filenames)
dd= dataset.map(test)
The function prints:
Tensor("args_0:0", shape=(), dtype=string)
When I try:
print(next(dd.as_numpy_iterator()))
It gives "a"
as expected. But I want to access the string inside the function so that it can open filenames. What's going on?
I found a similar answer here:
"args_0:0" when printing Tensor
But it wasn't enlightening and was not a minimal example. I also want to know the solution for from_tensor_slices
I checked tf.executing_eagerly()
and it gives True. My tensorflow version is 2.15.1. I'd prefer not to upgrade.
I found a similar answer here: Tensorflow Dataset open several files and keep them separated
Apparently only tensorflow functions can be applied to tensorflow arguments within the map function. So my test function would work if I replaced print(x)
with tf.print(x
).