I am learning the tensorflow which version is 2.8.0 on my MacBook M1. For debugging the code in the map function of dataset, I want to print tensor value in my function.
def function(i):
print("in: ", i)
if i < 2:
i = i - 1
return i
dataset = tf.data.Dataset.range(1, 6)
dataset = dataset.map(lambda x: tf.py_function(function, inp=[x], Tout=tf.int64))
for x in dataset:
print("out:", x)
I got the output as blew:
in: tf.Tensor(1, shape=(), dtype=int64)
out: tf.Tensor(0, shape=(), dtype=int64)
in: tf.Tensor(2, shape=(), dtype=int64)
out: tf.Tensor(2, shape=(), dtype=int64)
in: tf.Tensor(3, shape=(), dtype=int64)
out: tf.Tensor(3, shape=(), dtype=int64)
in: tf.Tensor(4, shape=(), dtype=int64)
out: tf.Tensor(4, shape=(), dtype=int64)
in: tf.Tensor(5, shape=(), dtype=int64)
out: tf.Tensor(5, shape=(), dtype=int64)
After I delete the print outside, I did not get any output. What's the difference between the print inside and the print outside. I don't understand why it can only take effect when the prints appear at the same time. Beside that, what the difference between print and tf.print?
CodePudding user response:
I believe it is because tensorflow datasets have lazy loading, which means they aren't evaluated until you actually try to iterate over the result.
When you removed the for loop, you were no longer iterating over the result, so it was never evaluated.