简体   繁体   中英

What's the difference between the print inside and outside

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?

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.

See https://stackoverflow.com/a/54679387/494134

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM