The TensorFlow AutoGraph module makes it easy to turn eager code into a graph, allowing automatic optimization. To do so, the easiest way is to add the tf.function decorator on top of your function:
@tf.function
def compute(a, b, c):
d = a * b + c
e = a * b * c
return d, e
A Python decorator is a concept that allows functions to be wrapped, adding functionalities or altering them. Decorators start with an @ (the "at" symbol).
When we call the compute function for the first time, TensorFlow will transparently create the following graph:
Figure 2.4: The graph automatically generated by TensorFlow when calling the compute function for the first time
TensorFlow AutoGraph can convert most Python statements, such as for loops, while loops, if statements, and iterations. Thanks to graph optimizations, graph execution can sometimes be faster than eager code. More generally, AutoGraph should be used in the following...