Tensorflow:变量rnn / basic_lstm_cell / kernel已经存在,不允许

时间:2017-12-06 08:54:07

标签: machine-learning tensorflow deep-learning lstm rnn

这是我在jupyter中编写的tensorflow RNN网络代码的一部分。整个代码第一次运行完美,但是,运行它会产生错误。代码:

x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)

def recurrent_nn_model(x):
   x = tf.transpose(x, [1,0,2])
   x = tf.reshape(x, [-1, chunk_size])
   x = tf.split(x, n_chunks, 0)

   lstm_layer = {'hidden_state': tf.zeros([n_batches, lstm_size]),
              'current_state': tf.zeros([n_batches, lstm_size])}
   layer = {'weights': tf.Variable(tf.random_normal([lstm_size, n_classes])),
         'biases': tf.Variable(tf.random_normal([n_classes]))}
   lstm = rnn_cell.BasicLSTMCell(lstm_size)
   rnn_outputs, rnn_states = rnn.static_rnn(lstm, x, dtype=tf.float32)
   output = tf.add(tf.matmul(rnn_outputs[-1], layer['weights']), 
            layer['biases'])

   return output

错误是:

  

变量rnn / basic_lstm_cell / kernel已经存在,不允许。难道   你的意思是在VarScope中设置reuse = True?最初定义于:

1 个答案:

答案 0 :(得分:1)

如果recurrent_nn_model是整个网络,只需添加此行以重置之前定义的图表:

tf.reset_default_graph()

如果您有意多次调用recurrent_nn_model并将这些RNN合并为一个图表,则应为每个图形使用不同的变量范围:

 with tf.variable_scope('lstm1'):
   recurrent_nn_model(x1)
 with tf.variable_scope('lstm2'):
   recurrent_nn_model(x2)