gpt4 book ai didi

machine-learning - Tensorflow:变量 rnn/basic_lstm_cell/kernel 已存在,不允许

转载 作者:行者123 更新时间:2023-11-30 08:36:12 25 4
gpt4 key购买 nike

这是我用 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

错误是:

Variable rnn/basic_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

最佳答案

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

tf.reset_default_graph()

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

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

关于machine-learning - Tensorflow:变量 rnn/basic_lstm_cell/kernel 已存在,不允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47670246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com