Relationship between Eager Execution and tf.function

Viewed 1864

My code is distributed across several files and functions and so it is not practical to refer to it over here. I think, I can solve it on my own if I understand eager execution and tf.function very well. Could someone help me here with the following queries ?

  1. Does @tf.function decorator work only when eager execution is disabled ?
  2. What is the relationship between @tf.function decorator and eager mode of execution ?
  3. How does TensorFlow switch between eager mode and non-eager mode internally ? When I disable eager execution, and I have some error in my code, I still get reference to files which suggest to me that eager execution might be happening internally.
1 Answers

Let me try to explain it. Hope it will be useful.

  • @tf.function decorator work only when eager execution is disabled ? no, actually tf.function is something to accelerate execution when eager mode is enabled
  • What is the relationship between @tf.function decorator and eager mode of execution ? @tf.function will cause tensorflow autograph working and accelerate execution for those operation inside it.
  • How does TensorFlow switch between eager mode and non-eager mode internally ? tf.function and AutoGraph work by generating code and tracing it into TensorFlow graphs. So when you call a @tf.function decorated function the first time, tensorflow will first convert it into a graph, then execute it, after that, when you can the function again, it will just execute the graph.

You can also check the tensorflow documentation. https://www.tensorflow.org/guide/function

Related