AttributeError: module tensorflowhas no attribute placeholder
报错原因:tensorflow 2.0版本去掉了placeholder,而tensorflow 1.*版本才有。
import tensorflow as tf
改成:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
这类语句:
X=tf.placeholder(“float”) Y=tf.placeholder(“float”)
改为:
X=tf.compat.v1.placeholder(“float”)
Y=tf.compat.v1.placeholder(“float”)
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,
可以看到需要使用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)