attributeerror: module tensorflow has no attribute contrib ( Solved )

attributeerror module tensorflow has no attribute contrib

Attributeerror: module tensorflow has no attribute contrib error mainly occurs because of version incompatibility of tf.contrib module. Since this module is not available in TF 2.0 version. It was earlier part of Tensorflow 1.x series. In this article, we will see the best fixes for this error in details. So Lets start-

attributeerror: module tensorflow has no attribute contrib ( Solution ) –

There are multiple ways to fix this issue. Lets understand them seperately .

tensorflow 2x
tensorflow 2x

Solution 1: Converting Tensorflow 1. x based code to Tensorflow 2. x version –

It seems a big task but trusts me, It is just one-line command. We can pass the complete code base for tensorflow 1.x. The tf_upgrade_v2 script will replace the incompatibility for all such functions and syntax. You may try the below command.

$tf_upgrade_v2 --intree code_1.x/ --outtree code_2.x/ --reportfile log.txt

Here code_1.x is Tensorflow 1.x code base. The code_2.x is a converted code base. The log.txt will contains the details of any mid-exception etc.

 

Solution 2: Downgrading the Tensorflow version –

If we are allowed to do so then we can opt for this easiest option. We can change the version of the TensorFlow module in out server/ system. Actually, the root cause for this error was running the syntax-based out of Tensorflow 1. x in the environment of Tensorflow 2. x series. If we change the environment then we can simply fix this error.

For changing the version of tensorflow, You can either try pip, conda or any other packager manager. Here we are proving the command for pip package manager-

pip install tensorflow==1.2.1

Here we can change the version for tensorflow on the basis of compatibility.

 

Solution 3: Manually replacing incompatible code pieces –

This is one of the hardest way to fix this error. But still, it is a solution. As we know tf.contrib is an incompatible field. Hence we will use its compatible alternative which is

tf.compat.v1.estimator

. It will fix the problem.

 

Thanks 

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner