Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: module tensorflow has no attribute contrib #7767

Closed
Girish1127 opened this issue Nov 6, 2019 · 25 comments
Closed

AttributeError: module tensorflow has no attribute contrib #7767

Girish1127 opened this issue Nov 6, 2019 · 25 comments

Comments

@Girish1127
Copy link

version: python 3.6.9
OS: windows10
IDE: anaconda, spyder (executing locally)
also installed tensorflow by creating new environment in anaconda (without using pip)
tensorflow version 2.0.0

code shown below:
import tensorflow as tf
''''
'''''
init_kernel = tf.contrib.layers.xavier_initializer()
getting error for the above line.
AttributeError: module 'tensorflow' has no attribute 'contrib'

@gaseosaluz
Copy link

Since you say that you used TF 2.0, from the TF docs:

https://www.tensorflow.org/guide/upgrade#compatibility_modules

I gather (but I could be wrong) that tf.contrib is not available on TF 2.0 (At least not all of the functionality).

These discussion maybe helpful?
tensorflow/tensorflow#26854
tensorflow/community#18

@saberkun saberkun closed this as completed Nov 6, 2019
@sinakhani
Copy link

Hello there,
When I use
initializer = tf.contrib.layers.xavier_initializer(seed = 0))
to initialize my ML layer I get the following error
AttributeError: module 'tensorflow' has no attribute 'contrib'
Any suggestion to remove this error is really appreciated.

@perturbedmuffin
Copy link

perturbedmuffin commented Jan 16, 2020

tensorflow.contrib was removed in TF2. Tensorflow Core released a utility to convert your TF 1.x scripts to TF2.0 which can be found here: https://www.tensorflow.org/guide/upgrade

EDIT: It was brought to my attention that the utility mentioned here cannot convert the tf.contrib library, so this method does not work. I will leave the comment up to save someone's time in the future.

@mrhanti
Copy link

mrhanti commented Feb 24, 2020

@EduardoABarrera parts of tensorflow/models latest code base are still using the TF 1.x API.

For instance:

...
from object_detection.utils import static_shape

// Throws: AttributeError: module 'tensorflow' has no attribute 'contrib'
slim = tf.contrib.slim

BOX_ENCODINGS = box_predictor.BOX_ENCODINGS
...

Find the complete file here

This is causing problems when doing transfer learning on object detection models. Do you have any idea or workaround for this one?

@perturbedmuffin
Copy link

@EduardoABarrera parts of tensorflow/models latest code base are still using the TF 1.x API.

For instance:

...
from object_detection.utils import static_shape

// Throws: AttributeError: module 'tensorflow' has no attribute 'contrib'
slim = tf.contrib.slim

BOX_ENCODINGS = box_predictor.BOX_ENCODINGS
...

Find the complete file here

This is causing problems when doing transfer learning on object detection models. Do you have any idea or workaround for this one?

@mrhanti I also bumped into this problem, and it turns out that the library tf.contrib is the only library that isn't converted via the utility mentioned above. If you're trying to convert a model like AlexNet that was written in TF1.X to a version of AlexNet written in TF2.0, I don't think there is a simple solution for that. I detailed this same problem (almost?) in this Stack overflow post.

The third point in my Stack overflow post is what I had to end up doing (writing out the model architecture by hand after converting it a few times), and it involved a lot of work. Sorry to bear bad news. :(

@init-22
Copy link

init-22 commented Apr 24, 2020

So there is no easy way out for this? @EduardoABarrera

@pbanikk
Copy link

pbanikk commented Aug 1, 2020

https://www.tensorflow.org/api_docs/python/tf/compat/v1/nn/rnn_cell/BasicLSTMCell
The only workaround if you want to use Basic LSTM Cell.

@pourya-farzi
Copy link

The unwinding path is just this line :
!pip install tensorflow==1.14
better than getting involved in convoluted procedure

@mikechen66
Copy link

mikechen66 commented Oct 20, 2020

For the usage in LSTM, it has been changed to the following.

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior() 
# -outputs, states = tf.contrib.rnn.static_rnn(lstm_cells, _X, dtype=tf.float32)
outputs, states = tf.compat.v1.nn.static_rnn(lstm_cells, _X, dtype=tf.float32)

And then it works. However, contrib is used widely in TensorFlow 1.x, Google does no provides an integrated solution to the contrib issue. For instance, it is another solution to the specific contrib case.

# -initializer = tf.contrib.layers.xavier_initializer(seed = 1)
initializer = tf.truncated_normal_initializer(stddev=0.1)

It is the pain of TensorFlow 2.x by the Google Team. Therefore, we need to solve the contrib problems case by case.

@saookami
Copy link

My Tensorflow version is 2.1.0,I checked file where in tensorflow\contrib.
There's just a little bit of code here.
Like i found slim film but there's have nothion in it,

@pnmartinez
Copy link

pnmartinez commented Jan 15, 2021

The unwinding path is just this line :
!pip install tensorflow==1.14
better than getting involved in convoluted procedure

I would add: create a specific environment for this and use it for TF1 stuff. You can do that in conda like:

conda create --name tf1 python=3.7  # python 3.8 won't work!

Then you can happily:

conda install tensorflow==1.14

@ShaikhaTheGreen
Copy link

I have a similar problem with this line:

        self.optimizer = tf.compat.v1.contrib.opt.ScipyOptimizerInterface(self.loss, 
                                                                method = 'L-BFGS-B', 
                                                                options = {'maxiter': 50000,
                                                                           'maxfun': 50000,
                                                                           'maxcor': 50,
                                                                           'maxls': 50,
                                                                           'ftol' : 1.0 * np.finfo(float).eps})
        
        init = tf.global_variables_initializer()
        self.sess.run(init)

It throws:
AttributeError: module 'tensorflow._api.v2.compat.v1' has no attribute 'contrib'

Any suggestion is appreciated. Thanks!

@gholn
Copy link

gholn commented Feb 17, 2021

Please check if your PYTHONPATH is set. If it is not set, you can get this error of 'has no attribute 'contrib''.
In Windows, set PYTHONPATH in System Environment variables.
In Linus, set it by $export PYTHONPATH=$PYTHONPATH:pwd:pwd/slim
Or you can add it to '.bashrc' file

@sandeep22-v
Copy link

export PYTHONPATH=$PYTHONPATH:pwd:pwd/slim
This will help surely..thank me later

@Fado2021
Copy link

@engsbk Hello,
Please how do you solved your issue??

AttributeError: module 'tensorflow._api.v2.compat.v1' has no attribute 'contrib'

@ShaikhaTheGreen
Copy link

ShaikhaTheGreen commented Mar 16, 2021

@engsbk Hello,
Please how do you solved your issue??

AttributeError: module 'tensorflow._api.v2.compat.v1' has no attribute 'contrib'

You can solve this in one of two ways:

  • you can entirely remove Tensorflow v1.x and install Tensorflow v2.x
  • Or Install Tensorflow v2.x and make sure it in the PATH directory.

Unless the code you are trying to run is totally uncompatible with Tensorflow v.2.x, in which case just switch back to Tensorflow v1.x.

@Fado2021
Copy link

Fado2021 commented Mar 16, 2021 via email

@Fado2021
Copy link

@engsbk I tried your suggestion, but still have problems. Any help??

TensorFlow 2.0.0 + Keras 2.3.1 on Python 3.6.

Problem1: config = tf.ConfigProto()
AttributeError: module 'tensorflow' has no attribute 'ConfigProto'

Problem2: self.optimizer = tf.contrib.opt.ScipyOptimizerInterface(self.loss,
method='L-BFGS-B',
options={'maxiter': 20000,
'maxfun': 50000,
'maxcor': 50,
'maxls': 50,
'ftol': 1 * np.finfo(float).eps})
AttributeError: module 'tensorflow_core.compat.v1' has no attribute 'contrib'

p1

@fdsig
Copy link

fdsig commented Jun 25, 2021

On Linux only:

`sudo apt update

update-alternatives --install /usr/bin/python python /usr/opt/python3.7  

virtualenv -p python3.7 contrib/**insert your path/and name for virtual environment)

source **insert/path/to/environment**/bin/activate 

pip install tensorflow==1.** `

As mentioned above this wont work on python 3.7 or later.

useful links are:

https://www.tensorflow.org/guide/migrate

Python alternative install on Linux:


https://medium.com/analytics-vidhya/how-to-install-and-switch-between-different-python-versions-in-ubuntu-16-04-dc1726796b9b

https://hackersandslackers.com/multiple-versions-python-ubuntu/

python source download urls:

https://www.python.org/ftp/python/3.7.0/

managing virtual environments Linux:

https://www.liquidweb.com/kb/creating-virtual-environment-ubuntu-16-04/

https://docs.python.org/3/library/venv.html

haveing tried most of the above- including migrating code and import tensorflow.compat.v1 as tf tf.disable_v2_behavior() which both are not viable workaround for code that uses the config module.

I was sucesfull able to run code with the above method without issue.

Hope this helps :-)

@Leci37
Copy link

Leci37 commented Jan 9, 2023

Replace TF V1 line
slim = tf.contrib.slim
for TF V2
import tf_slim as slim

@haroon445
Copy link

version: python 3.6.9 OS: windows10 IDE: anaconda, spyder (executing locally) also installed tensorflow by creating new environment in anaconda (without using pip) tensorflow version 2.0.0

code shown below: import tensorflow as tf '''' ''''' init_kernel = tf.contrib.layers.xavier_initializer() getting error for the above line. AttributeError: module 'tensorflow' has no attribute 'contrib'

The "attributeerror: module tensorflow has no attribute contrib" error occurs in Python when we try to import or use tf.contrib in TensorFlow 2.0 or later versions. This is because tf.contrib has been removed from TensorFlow 2.0.

To fix this error we need to downgrade the tensorflow to the version that supports the tf.contrib or upgrade the code from 1.x version to 2.x version. You can check this guide to resolve this error, I have found it very helpful.

@amkjitu
Copy link

amkjitu commented Aug 27, 2023

Replace TF V1 line slim = tf.contrib.slim for TF V2 import tf_slim as slim

THANKSSSSS IT WORKSSSS

@shravani1115
Copy link

Replace TF V1 line slim = tf.contrib.slim for TF V2 import tf_slim as slim

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

I am facing this error please help

@Roxie2003
Copy link

Replace TF V1 line slim = tf.contrib.slim for TF V2 import tf_slim as slim

Thank you so muchhhhh :)

@RubensFPereira
Copy link

RubensFPereira commented Apr 18, 2024

Hello Everyone!
You guys can help me with this problem?

File "F:\Anaconda\envs\pothole\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\data_decoders\tf_example_decoder.py", line 332, in
class _BackupHandler(tf_example_decoder.ItemHandler):
AttributeError: module 'object_detection.data_decoders.tf_example_decoder' has no attribute 'ItemHandler'

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests