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' #505

Open
natefrisch01 opened this issue Jun 23, 2020 · 8 comments
Open

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

natefrisch01 opened this issue Jun 23, 2020 · 8 comments

Comments

@natefrisch01
Copy link

natefrisch01 commented Jun 23, 2020

tensorflow.contrib.eager is no longer in TensorFlow, but it's used in the jupyter notebooks and in google colab.

import tensorflow as tf
tfe = tf.contrib.eager

# Eager Execution
#@markdown Check the box below if you want to use [Eager Execution](https://www.tensorflow.org/guide/eager)
#@markdown Eager execution provides An intuitive interface, Easier debugging, and a control flow comparable to Numpy. You can read more about it on the [Google AI Blog](https://ai.googleblog.com/2017/10/eager-execution-imperative-define-by.html)
use_tf_eager = False #@param {type:"boolean"}

# Use try/except so we can easily re-execute the whole notebook.
if use_tf_eager:
    try:
        tf.enable_eager_execution()
    except:
        pass
@jaypatel15406
Copy link

Maybe you Haven't installed Nightly Artifacts of TensorFlow. Nighty Artifacts is one core component in order to use TensorFlow eager Mode
To install the nightly pip packages, do:

# For CPU only
pip install tf-nightly  

# For GPU support
pip install -U tf-nightly-gpu

If it doesn't work, then add more detailed version by running the following in a Python shell:

import tensorflow as tf
print(tf.__version__)
print(tf.__git_version__)

For More Details, you can refer:- TensorFlow Eager Execution

@piojanu
Copy link

piojanu commented Jul 23, 2020

@jaypatel15406 it's true for colab notebooks too, which say that you don't have to install anything. Simply from TF2.0 there is no contrib module anymore and these notebooks are outdated as far as I'm concerned. See this stack.

Can anybody help with it? How to convert them to TF2.0?

EDIT:

For now, I simply deleted the lines

import tensorflow as tf
tfe = tf.contrib.eager

# Eager Execution
#@markdown Check the box below if you want to use [Eager Execution](https://www.tensorflow.org/guide/eager)
#@markdown Eager execution provides An intuitive interface, Easier debugging, and a control flow comparable to Numpy. You can read more about it on the [Google AI Blog](https://ai.googleblog.com/2017/10/eager-execution-imperative-define-by.html)
use_tf_eager = False #@param {type:"boolean"}

# Use try/except so we can easily re-execute the whole notebook.
if use_tf_eager:
    try:
        tf.enable_eager_execution()
    except:
        pass

and repliced them with:

import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()

This will disable eager execution and remove dependence on tf.contrib from the code (I hope so, to this moment it did).

@piojanu
Copy link

piojanu commented Jul 23, 2020

I think @mikeshwe started porting this code to TF2. I'll try to do my best to port the rest of notebooks as I go based on the first one (I committed to learning Bayesian Methods with this practical course). I'll push PRs after each notebook is ported.

Should we open another issue for communication?

@cyniphile
Copy link

Another solution is to downgrade: #502

@vincecr0ft
Copy link

I've got a port that seems to work in TF2, at least for the second chapter, and only in eager mode. However, even running on 2 Tesla T4s the code takes hours to run (so far - my instances keep timing out).

I think there's something else gone wrong here.

@vincecr0ft
Copy link

ok so rather than add the option for graph mode I've added the @tf.function decorator to all the mcmc.sample_chain calls and the whole thing now runs in about 2 minutes.

https://github.com/tensorflow/probability/blob/master/tensorflow_probability/examples/jupyter_notebooks/A_Tour_of_TensorFlow_Probability.ipynb

I'll do some further cleanup (removing the evaluate calls - since just doing .numpy() on the result is about 30% faster and its less confusing), and extend out to the other examples. But should be an easy fix from here on out.

@kingsyard
Copy link

Maybe you Haven't installed Nightly Artifacts of TensorFlow. Nighty Artifacts is one core component in order to use TensorFlow eager Mode
To install the nightly pip packages, do:

# For CPU only
pip install tf-nightly  

# For GPU support
pip install -U tf-nightly-gpu

If it doesn't work, then add more detailed version by running the following in a Python shell:

import tensorflow as tf
print(tf.__version__)
print(tf.__git_version__)

For More Details, you can refer:- TensorFlow Eager Execution

thank you

@ankit025jain
Copy link

One easy way is you can pass your code written in TensorFlow 1.x to the below code to automatically upgrade it to TensorFlow 2.x.

$tf_upgrade_v2 \
--intree my_project/ \
--outtree my_project_v2/ \
--reportfile report.txt

The above code will replace all the commands which are deprecated in 2.x with the onces that are actually working in 2.x. And then you can run your code in TensorFlow 2.x.

In case if it throws an error and is unable to convert the complete code and then don't panic. Please 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.

Taadaa, just replace the commands that are throwing errors with the new ones.

Example:

If the command in TensorFlow 1.x is:

tf.contrib

Then the same command in Tensorflow 2.x is:

tf.compat.v1.estimator

In the above example replace "tf.contrib" with "tf.compat.v1.estimator" and that should solve the problem.

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

7 participants