module ‘tensorflow_core.compat.v1‘ has no attribute ‘contrib‘问题的完美解决

问题描述:

Instructions for updating:
Use keras.layers.Dense instead.
Traceback (most recent call last):
  File "run_cnn.py", line 200, in <module>
    model = TextCNN(config)
  File "D:\MY DATA\学习资料\研究生\深度学习\text-classification-cnn-rnn-master\cnn_model.py", line 43, in __init__
    self.cnn()
  File "D:\MY DATA\学习资料\研究生\深度学习\text-classification-cnn-rnn-master\cnn_model.py", line 61, in cnn
    fc = tf.contrib.layers.dropout(fc, self.keep_prob)
AttributeError: module 'tensorflow_core.compat.v1' has no attribute 'contrib'

原因:
TensorFlow 2 删除了部分v1的库
很多代码大佬们都是用v1版本写的,所以一种解决办法是将tensorflow版本降回v1,我这么干了,结果引发更大的问题,因为菜啊。。。找了好久,下面这种方法完美解决。
解决办法:

cell = tf.contrib.rnn.BasicLSTMCel

cell = tf.compat.v1.nn.rnn_cell.BasicLSTMCell
代替就可以完美的解决出现的问题。得到的结果没啥区别。

你可能感兴趣的:(深度学习,tensorflow,深度学习)