close

[Solved] How to solve AttributeError: module ‘tensorflow_core.compat.v1’ has no attribute ‘contrib’?

Hello Guys How Are You All ? Hope You all are fine. Today I Have Faced How to AttributeError: module ‘tensorflow_core.compat.v1’ has no attribute ‘contrib’ In Python.How to AttributeError: module ‘tensorflow_core.compat.v1’ has no attribute ‘contrib’ So Here I am Explain to you all the possible solutions Here.

Without Wasting your time, Lets start This Article to Solve This Error in Anaconda

How to solve AttributeError: module ‘tensorflow_core.compat.v1’ has no attribute ‘contrib’?

x = tf.placeholder(dtype = tf.float32, shape = [None, 28, 28])
y = tf.placeholder(dtype = tf.int32, shape = [None])
images_flat = tf.contrib.layers.flatten(x)
logits = tf.contrib.layers.fully_connected(images_flat, 62, tf.nn.relu)
loss = 
tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( 
   labels = y, logits = logits))
   train_op = 
   tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
   correct_pred = tf.argmax(logits, 1)
   accuracy = tf.reduce_mean(tf.cast(correct_pred, 
   tf.float32))

   print("images_flat: ", images_flat)
   print("logits: ", logits)
   print("loss: ", loss)
   print("predicted_labels: ", correct_pred)


AttributeError                            Traceback (most recent call last)
<ipython-input-17-183722ce66a3> in <module>
      1 x = tf.placeholder(dtype = tf.float32, shape = [None, 28, 28])
      2 y = tf.placeholder(dtype = tf.int32, shape = [None])
----> 3 images_flat = tf.contrib.layers.flatten(x)
      4 logits = tf.contrib.layers.fully_connected(images_flat, 62, tf.nn.relu)
      5 loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y, logits = logits))

AttributeError: module 'tensorflow_core.compat.v1' has no attribute 'contrib'

2.This is my code in Jupyter Notebook. I just started with python and get the error I mentioned in the headline. I would be very thankful if someone could help me wizh a code example to solve the problem.

Solution

How to solve AttributeError: module ‘tensorflow_core.compat.v1’ has no attribute ‘contrib’?

Solution : 1

tf.contrib was removed from TensorFlow once with TensorFlow 2.0 alpha version.

Most likely, you are already using TensorFlow 2.0.

You can find more details here: https://github.com/tensorflow/tensorflow/releases/tag/v2.0.0-alpha0

For using specific versions of tensorflow, use

pip install tensorflow==1.14

or

pip install tensorflow-gpu==1.14

Solution : 2


I think you need to add the following line in your python file which you are going to execute it.


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

Solution : 3


contrib is a headache of Google Team. We have to deal with the issue of contrib case by case. I just take two examples as follows.

1.With regard to CNN, it has the following method

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

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

2.With regard to RNN/LSTM, it has the following different method.

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)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also Read: SyntaxError: invalid syntax to repo init in the AOSP code

Leave a Comment