How To Solve Python AttributeError: module ‘tensorflow’ has no attribute ‘ConfigProto’

by | Machine Learning, Programming, Python, Tips

In TensorFlow 2.0, tf.ConfigProto is no longer in use. The functionalities of ConfigProto are now under tf.config.experimental.

If you want to continue using ConfigProto in TensorFlow 2.0 use tf.compat.v1.ConfigProto() instead.

You can follow the migration guide if you want 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.


AttributeError: module ‘tensorflow’ has no attribute ‘ConfigProto’

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 ‘ConfigProto’” tells us that the tensorflow module does not have the attribute ConfigProto(). The ConfigProto() class belongs to TensorFlow 1.x data type, and we use it to set Session configuration parameters.

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

Let’s look at an example where we try to create an object of the ConfigProto() class to set Session configuration parameters with TensorFlow 2.0:

import tensorflow as tf

print(tf.__version__)

config = tf.ConfigProto(intra_op_parallelism_threads=8,

    inter_op_parallelism_threads=8,

    allow_soft_placement=True)
2.3.1
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-623f18b58789> in <module>
      3 print(tf.__version__)
      4 
----> 5 config = tf.ConfigProto(intra_op_parallelism_threads=8,
      6 
      7     inter_op_parallelism_threads=8,

AttributeError: module 'tensorflow' has no attribute 'ConfigProto'

Let’s look at how to solve this error:

Solution: Use tf.compat.v1

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. Let’s look at the revised code:

import tensorflow as tf

print(tf.__version__)

config = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=8,

    inter_op_parallelism_threads=8,

    allow_soft_placement=True)

print(config)
2.3.1
intra_op_parallelism_threads: 8
inter_op_parallelism_threads: 8
allow_soft_placement: true

We are now able to set the Session parameters using the TF1.x API.

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. The runtime behaviour of using functions instead of sessions integrates well with Python runtime and Eager execution.

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 ‘ConfigProto’ occurs when you try to use ConfigProto in TensorFlow 2.0. ConfigProto 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.ConfigProto().

For further reading on TensorFlow, go to the articles:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

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.

Buy Me a Coffee