difference between Tensorflow's Graph and GraphDef
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
TensorFlow's computation model is built around graphs. When you build a model, you are constructing a directed acyclic graph of operations. Two closely related but distinct concepts in this model are tf.Graph and GraphDef. Understanding the difference between them is essential for tasks like saving and loading models, optimizing inference graphs, and debugging TensorFlow programs.
This article explains what each one represents, how they relate to each other, and when you would interact with each in practice.
What tf.Graph Is
A tf.Graph is TensorFlow's in-memory Python object that represents a computation graph. It is a live, mutable structure that holds operations (nodes), tensors (edges), and additional metadata like collections, variable initializers, and control dependencies.
Key properties of tf.Graph:
- It is a Python-side runtime object with methods for adding operations, looking up tensors by name, managing variable scopes, and applying optimizations.
- It holds mutable state. You can keep adding operations to a graph after creating it.
- It includes metadata that does not exist in the serialized format, such as Python-level gradient functions, device placement hints, and collections (like
tf.GraphKeys.TRAINABLE_VARIABLES). - There is a default graph (
tf.compat.v1.get_default_graph()) that operations are added to unless you explicitly specify otherwise.
What GraphDef Is
GraphDef is a Protocol Buffer (protobuf) message that represents the serialized, language-independent description of a computation graph. It is defined in TensorFlow's protobuf schema and contains only the information needed to reconstruct the graph's structure: nodes, their operations, input connections, attributes, and device assignments.
Output:
Key properties of GraphDef:
- It is a serialized data structure (protobuf), not a Python object with behavior.
- It is immutable once created. To modify it, you create a new
GraphDef. - It is language-independent. The same
GraphDefcan be loaded in Python, C++, Java, or Go. - It does not include Python-specific metadata like gradient functions, variable collections, or feed/fetch dictionaries.
How They Relate
The relationship flows in both directions:
Graph to GraphDef: Call graph.as_graph_def() to serialize a live graph into a GraphDef protobuf.
GraphDef to Graph: Call tf.import_graph_def(graph_def) to reconstruct a live graph from a serialized GraphDef.
Note that the import adds a name prefix ("imported") to avoid name collisions with operations already in the target graph.
When You Work with Each
You use tf.Graph when:
- Building models interactively in Python.
- Adding operations, creating variables, and defining training loops.
- Debugging by inspecting operations and their properties.
- Using TF1-style sessions or TF2's
tf.function(which captures aConcreteFunctionbacked by a graph).
You use GraphDef when:
- Saving a model's computation graph to disk (as part of SavedModel or a frozen graph).
- Loading a pre-trained model for inference in a different language or on a different platform.
- Applying graph transformations and optimizations (like constant folding, pruning, or quantization) using TensorFlow's graph transform tool.
- Inspecting a model's structure without executing it.
Freezing a Graph: A Common Use Case
A "frozen graph" is a GraphDef where all variables have been converted to constants. This is the standard format for deploying TensorFlow models to production.
The frozen GraphDef is self-contained and can be loaded in a C++ runtime without needing a checkpoint file.
Common Pitfalls
- Assuming GraphDef preserves everything. Collections, gradient functions, and Python callbacks are not included in the
GraphDef. If you serialize and deserialize a graph, you lose these. This is whySavedModel(which includes bothGraphDefand variable checkpoints) is preferred over rawGraphDeffor model saving. - Editing GraphDef directly. While
GraphDefis a protobuf and technically editable, manually modifying nodes is error-prone and can produce invalid graphs. Use TensorFlow's graph transform tools instead. - Confusing GraphDef with SavedModel. A
SavedModelincludes aGraphDef, variable values, signatures, and assets. AGraphDefalone does not include trained weights (unless the graph is frozen). - Name collisions during import. When importing a
GraphDefinto an existing graph, always use thenameparameter oftf.import_graph_defto add a prefix. Without it, duplicate operation names cause errors.
Summary
tf.Graph is the live, mutable Python object you interact with when building and training models. GraphDef is its serialized protobuf representation, designed for storage, transport, and cross-language compatibility. You convert between them with as_graph_def() and import_graph_def(). In practice, you work with tf.Graph during development and with GraphDef when saving, loading, or optimizing models for deployment.
Related reading
- Difference between tf.clip_by_value and tf.clip_by_global_norm for RNN's and how to decide max value to clip on?
- Difference between tfjs_layers_model and tfjs_graph_model
- Difference between tf.layers.conv2d and tf.contrib.slim.conv2d
- Difference between tf.saved_model.savemodel, path_to_dir and tf.keras.model.save
- Difference between Tensorflow's tf.keras.layers.Dense and PyTorch's torch.nn.Linear?
- Difference between tf.assign and assignment operator
- Difference between tf.nn_conv2d and tf.nn.depthwise_conv2d
- Difference between Variable and get_variable in TensorFlow

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.