这是我在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?最初定义于:
答案 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)