1

I imported tensorflow module but I'm unable to use tf.contrib. I don't know what the problem is. I tried running it in different versions but I keep getting the same output.

ModulesImported:

import tensorflow.compat.v1 as tf1
tf1.disable_v2_behavior() 
import tensorflow as tf2

Code:

tf2.contrib.rnn.LSTMCell(num_units=num_nodes[li],
                            state_is_tuple=True,
                            initializer= tf.contrib.layers.xavier_initializer()
                           )

Output:

AttributeError: module 'tensorflow' has no attribute 'contrib'
hypocrite
  • 25
  • 1
  • 4
  • I think [this post](https://stackoverflow.com/questions/55870127/module-tensorflow-has-no-attribute-contrib) maybe help you! I hope it works you Best! – Floriana Loiacono Apr 06 '20 at 18:17

1 Answers1

6

I think the problem is in the version, i have tried it in 1.15.2 version and it worked for me. After installing the mentioned version try the below code, it should work.

import tensorflow.compat.v1 as tf1
tf1.disable_v2_behavior() 
import tensorflow as tf2 #Tensorflow 1.15.2
from tensorflow.contrib.rnn import LSTMCell


LSTMCell(num_units=num_nodes[li],
                            state_is_tuple=True,
                            initializer= tf.contrib.layers.xavier_initializer()
                           )

But if you are using TensorFlow 2.x version contrib has been deprecated, you can use the below code. Since xavier_initializer is also is using contrib you can use GlorotUniform initializer which is the same as xavier_initializer. Follow the below code.

import tensorflow.compat.v1 as tf1
tf1.disable_v2_behavior() 
import tensorflow as tf2 #Tensorflow 2.x

tf2.compat.v1.nn.rnn_cell.LSTMCell(num_units=10,
                            state_is_tuple=True,
                            initializer= tf2.initializers.GlorotUniform()
                           )