module ‘tensorflow.compat.v1‘ has no attribute ‘contrib‘

1. AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’

使用了这篇文章中的解决办法,发现又引入了下列问题。

2. AttributeError: module ‘tensorflow.compat.v1’ has no attribute ‘contrib’

lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
drop = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob_) 
cell = tf.contrib.rnn.MultiRNNCell([drop] * lstm_layers)

以上是我的代码
解决办法:
打开tensorflow官方网站,在搜索框里边分别搜索BasicLSTMCell,DropoutWrapper,MultiRNNCell
module ‘tensorflow.compat.v1‘ has no attribute ‘contrib‘_第1张图片
可以看到需要使用tf.compat.v1.nn.rnn_cell.MultiRNNCell来替代,其他同理。将以上代码修改为

lstm = tf.nn.rnn_cell.BasicLSTMCell(lstm_size)
drop = tf.nn.rnn_cell.DropoutWrapper(lstm, output_keep_prob=keep_prob_)
cell = tf.nn.rnn_cell.MultiRNNCell([drop] * lstm_layers)

即可

你可能感兴趣的:(机器学习,tensorflow)