Skip to content

Instantly share code, notes, and snippets.

@adityaiitb
Last active August 27, 2020 17:30
Show Gist options
  • Save adityaiitb/551d5a3758331224890aab6ff3aab09d to your computer and use it in GitHub Desktop.
Save adityaiitb/551d5a3758331224890aab6ff3aab09d to your computer and use it in GitHub Desktop.
How to obtain a Graph in TensorFlow2

tf.function compiles a function into a callable TensorFlow graph. Documentation

@tf.function
def f(x):
  return x + 1

To obtain a graph / graphdef

Use the get_concrete_function method of the callable created by tf.function. It returns a tf.Graph object.

g = f.get_concrete_function(1).graph
isinstance(g, tf.Graph)

gdef = g.as_graph_def(add_shapes=True)
tf.io.write_graph(gdef, ".", "foo.pbtxt")

Tensorflow Graph editor (not maintained)

Graph Editor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment