In TensorFlow 2.0, tf.GraphDef
is no longer in use. TensorFlow 2.0 encapsulates graph computations as Python functions instead of using Session
making TensorFlow more Pythonic.
If you want to continue using GraphDef in TensorFlow 2.0, use tf.compat.v1.Graphdef()
instead.
You can follow the migration guide to migrate your TensorFlow code from TensorFlow 1.x to TensorFlow 2.
This tutorial will go through the error in detail and how to solve it with code examples.
Table of contents
AttributeError: module ‘tensorflow’ has no attribute ‘GraphDef’
AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘module ‘tensorflow’ has no attribute ‘GraphDef’” tells us that the TensorFlow module does not have the attribute GraphDef()
. GraphDef
belongs to TensorFlow 1.x API and provides a serialized version of a computation graph as a protobuf.
You should not need to use GraphDef directly in TensorFlow 2. To load GraphDefs in TensorFlow 2, we can use SavedModel.
Generally, if the AttributeError refers to a module not having an attribute, either the functionality is under a different name or deprecated. Consult the documentation of the module to find where functionalities and sub-modules are.
Do not name python scripts after module names. For example, naming a script tensorflow.py
. If you try:
import tensorflow as tf
you will import the script file tensorflow.py
under your current working directory, rather than the actual TensorFlow module. The Python interpreter searches for a module first in the current working directory, then the PYTHONPATH, then the installation-dependent default path. You can name a script after its functionality instead.
Example: Loading pb File
Let’s look at an example where we load a protobuf file using GFile
and GraphDef
. First we will define a computation graph and save it using write_graph
import tensorflow as tf # Disable eager execution because placeholder is not compatible with it tf.compat.v1.disable_eager_execution() I = tf.compat.v1.placeholder(tf.float32, shape=[None, 3], name='I') # input W = tf.Variable(tf.zeros(shape=[3, 2]), dtype=tf.float32, name='W') # weights b = tf.Variable(tf.zeros(shape=[2]), dtype=tf.float32, name='b') # biases O = tf.nn.relu(tf.matmul(I, W) + b, name='O') # activation / output saver = tf.compat.v1.train.Saver() init_op = tf.compat.v1.global_variables_initializer() with tf.compat.v1.Session() as sess: sess.run(init_op) # save the graph tf.compat.v1.train.write_graph(sess.graph_def, '.', 'hellotensor.pb', as_text=False)
When we run this code, we will write a serialized graph to a protobuf file called hellotensor.pb
.
Next, we will load the file using a context manager with GFile and read the bytes into a GraphDef
object. We must ensure we are in the same directory where we saved the protobuf file hellotensor.pb
. Let’s look at the code:
with tf.gfile.GFile('hellotensor.pb', 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) with tf.Graph().as_default() as graph: tf.import_graph_def(graph_def, name='prefix') print(graph)
Let’s run the code to see what happens
with tf.gfile.GFile(frozen_graph, 'rb') as f: AttributeError: module 'tensorflow' has no attribute 'gfile'
The first error occurs because in TensorFlow 2.0 gfile is under tf.io
not tf
.
Solution Part 1: Solving AttributeError: module ‘tensorflow’ has no attribute ‘gfile’
To solve this error, we need to replace tf.gfile.GFile
with tf.io.gfile.GFile
. Let’s look at the revised code:
with tf.io.gfile.GFile('hellotensor.pb', 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) with tf.Graph().as_default() as graph: tf.import_graph_def(graph_def, name='prefix') print(graph)
Let’s run the code to see what happens:
AttributeError: module 'tensorflow' has no attribute 'GraphDef'
We resolved the gfile AttributeError but now we have a GraphDef AttributeError. This error occurs because GraphDef is no longer in use in TensorFlow 2.x.
Solution Part 2: Solving AttributeError: module ‘tensorflow’ has no attribute ‘GraphDef’
We can use the tf.compat.v1
module to solve this error. The module contains the complete TF1.x API with its original semantics. Generally, you should avoid using the legacy compat.v1 APIs for any new code you write in TensorFlow 2.0, but this approach is suitable for previously written code. We will change tf.GraphDef()
to tf.compat.v1.GraphDef()
. Let’s look at the revised code:
with tf.io.gfile.GFile('hellotensor.pb', 'rb') as f: graph_def = tf.compat.v1.GraphDef() graph_def.ParseFromString(f.read()) with tf.Graph().as_default() as graph: tf.import_graph_def(graph_def, name='prefix') print(graph)
Let’s run the code to see what happens:
<tensorflow.python.framework.ops.Graph object at 0x7f82b7dea490>
We successfully loaded the seralized graph into a Graph object.
TensorFlow 1.x vs TensorFlow 2
TensorFlow 2 follows a fundamentally different programming paradigm from TensorFlow 1.x. There are different runtime behaviors around execution, variables, control flow, tensor shapes, and tensor equality comparisons. TensorFlow 2 is preferable to use as it removes redundant APIs and makes APIs more consistent.
To migrate to TensorFlow 2, follow the TF1.x to TF2 migration guide.
Summary
Congratulations on reading to the end of this tutorial! The AttributeError: module ‘tensorflow’ has no attribute ‘GraphDef’ occurs when you try to create a GraphDef object to store a serialized version of a computation graph in TensorFlow 2.x. to execute Operations or evaluate Tensors in TensorFlow 2.0. Session is a TF1.x class.
To solve this error, you can either migrate to TensorFlow 2.0 and use the updated APIs or use tf.compat.v1.GraphDef()
.
For further reading on TensorFlow, go to the articles:
- How To Solve Python AttributeError: module ‘tensorflow’ has no attribute ‘ConfigProto’
- How to Solve Python AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’
- How to Solve Python AttributeError: module ‘tensorflow.python.framework.ops’ has no attribute ‘_TensorLike’
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.