Sometimes while training the model in TensorFlow you get Attributeerror: module tensorflow has no attribute contrib error. Or not only in the training process but because of other reasons you get the same error. In this short article, we will learn how you can solve Attributeerror: module tensorflow has no attribute contrib error when using TensorFlow. We will go through various solutions and hopefully one of them will help you to solve this issue. Moreover, we will also discuss how we can interpret and understand errors in Python taking Module ‘tensorflow’ has no attribute ‘contrib’ as an example.
Attributeerror: module tensorflow has no attribute contrib
Before going into the reasons why you are getting this error let us first try to solve the issue using various methods because I know many of you are not interested in knowing the reason but you are searching for the solution. So, in case someone wants to find out why they are facing this issue, you can read the next section where we will discuss the reasons for Attributeerror: module tensorflow has no attribute contrib error:

Method-1: Attributeerror: module tensorflow has no attribute contrib
On reasons is that tf.contrib has moved out of TensorFlow starting to TensorFlow 2.0 aplha. So, you need to upgrade your TF 1.x code to TF 2.x using the following script:
tf_upgrade_v2
If you are still facing difficulty with how you can upgrade your existing TF 1.x, you can read the official documentation:
Method-2:Attributeerror: module tensorflow has no attribute contrib
Another very easy method by which you can easily upgrade TF 1.x to TF2.x without doing any external installation is using the following code in your TF 1.x. This will automatically upgrade it to TensorFlow 2.x.
$tf_upgrade_v2 \
--intree my_project/ \
--outtree my_project_v2/ \
--reportfile report.txt
The above code replaces all the instructions that are written in TF 1.x into TF 2.x.
Now, if the above method is unable to convert the complete code then don’t worry. Follow the following instructions:
- Open the report.txt file that is generated by the above code. In this file, you will find commands that are deprecated and their alternative commands that can be used in TensorFlow 2.x.
- Now, replace the commands that are throwing errors with a new one. For example, replace :
tf.contrib
With the following commands:
tf.compat.v1.estimator
This is just an example to show you how you can replace the code written in TF 1.x into TF 2.x.
Method-3: Use the pip command
Another very common method to get rid of this error is to install a new and updated version of TensorFlow on your system. You can use the pip command to install TensorFlow version 2.2.0 on your system:
# if you are using pip version
pip install tensorflow==2.2.0
# if you are using pip3 version
pip3 install tensorflow==2.2.0
Hope this method will solve the issue:
Method-4: Reinstall the TensorFlow
If non of the above-mentioned methods helped you to solve the issue, then you can uninstall the TensorFlow and install it again on your system to get the upgraded TensorFlow:
You can uninstall TensorFlow using the following commands:
# uninstalling the tensorflow
pip uninstall tensorflow-gpu
Now, install TensorFlow again on your system.
# installing the tensorflow
pip install tensorflow-gpu
This will hopefully solve the problem. Cheer!!!!
Reasons for Attributeerror: module tensorflow has no attribute contrib error
One of the main reasons for getting these errors is that the TensorFlow module does not have contrib attribute because the contrib module had been removed in TF 2.x. The reason for removing this module in TF 2.x is in favor of having more focused and supportive libraries.
If you are using the contrib module and getting errors, then you need to update the code accordingly.
If you are using the older version of TensorFlow, you can use the contrib module but it is highly recommended to upgrade the to TF2.x to get advantage of new and updated features.
How to install TensorFlow on Ubuntu?
There can be many ways to install TensorFlow on Ubuntu operating system. One of the best methods is to use the sudo command as shown below:
sudo python3 -m pip install tensorflow
Once the installation is complete, you can use the following command to see if the TensorFlow has been installed on your system or not.
sudo python3 -m pip show tensorflow
It will display the details of the TensorFlow module installed on your system.
Summary
In this short article, we discussed how we can solve Attributeerror: module TensorFlow has no attribute contrib error while using the contrib module in TensorFlow. We solved the issue using four different methods and we also discussed the reason for getting this error.
Further Reading:
You may also like:
- [Solved] Module matplotlib.cbook has no attribute iterable
- [Solved] modulenotfounderror: no module named ‘matplotlib’
- TensorFlow tutorials
Pingback: [Solved] importerror: cannot import name joblib from sklearn.externals - TechFor-Today
Pingback: TypeError: can only concatenate str (not "int") to str - TechFor-Today
Pingback: [Solved] AttributeError: 'nonetype' object has no attribute 'shape' - Techfor-Today
Pingback: IndexError: tuple index out of range [Solved] - Techfor-Today
Pingback: ValueError: Not Enough Values to Unpack [5 Cases] - Techfor-Today