Missed TensorFlow Dev Summit? Check out the video playlist. Watch recordings

tf.autograph.to_graph

TensorFlow 1 version View source on GitHub

Converts a Python entity into a TensorFlow graph.

tf.autograph.to_graph(
    entity, recursive=True, experimental_optional_features=None
)

Also see: tf.autograph.to_code, tf.function.

Unlike tf.function, to_graph is a low-level transpiler that converts Python code to TensorFlow graph code. It does not implement any caching, variable management or create any actual ops, and is best used where greater control over the generated TensorFlow graph is desired. Another difference from tf.function is that to_graph will not wrap the graph into a TensorFlow function or a Python callable. Internally, tf.function uses to_graph.

Example usage:

def f(x): 
  if x > 0: 
    y = x * x 
  else: 
    y = -x 
  return y 
 
converted_f = to_graph(f) 
x = tf.constant(2) 
converted_f(x)  # converted_foo is like a TensorFlow Op. 
<tf.Tensor: shape=(), dtype=int32, numpy=4> 

Supported Python entities include:

  • functions
  • classes
  • object methods

Functions are converted into new functions with converted code.

Classes are converted by generating a new class whose methods use converted code.

Methods are converted into unbound function that have an additional first argument called self.

For a tutorial, see the tf.function and AutoGraph guide. For more detailed information, see the AutoGraph reference documentation.

Args:

  • entity: Python callable or class to convert.
  • recursive: Whether to recursively convert any functions that the converted function may call.
  • experimental_optional_features: None, a tuple of, or a single tf.autograph.experimental.Feature value.

Returns:

Same as entity, the converted Python function or class.

Raises:

  • ValueError: If the entity could not be converted.