Is an eager-graph compatible same code solution possible?

Viewed 243

I am trying to write code that is eager and graph compatible. However, there is very little information online for how to do this, being a literal footnote on TensorFlow's website. Furthermore, what they have wrote is confusing, saying:

The same code written for eager execution will also build a graph during graph execution. Do this by simply running the same code in a new Python session where eager execution is not enabled.

This implies that a same code solution is possible, where the only change required is the addition or removal of tf.enable_eager_execution().

Currently I use tf.keras to define my model and tf.data for my input pipeline. However, many eager operations don't work in graph, with the opposite also being true.

For example, I keep track of my number of epochs using tf.train.Checkpoint(). In eager mode, after restoring I can access it using epochs.numpy() to assign its value to a local variable. However, this does not work with graphs, which instead would require sess.run(epochs) due to the values not being defined during execution.

Again, to compute my gradients in eager I need to use some form of autograd, in my case tf.GradientTape(). This is not compatible with graphs, as "tf.GradientTape.gradients() does not support graph control flow."

I see that tfe.py_func exists, but once again, this only works when eager is not enabled, thus not helping for this problem.

So how do I make a same code solution, when it seems that many aspects of eager and graph directly conflict with each other?

0 Answers
Related