【问题标题】:Tensorflow: Variable rnn/basic_lstm_cell/kernel already exists, disallowedTensorflow:变量 rnn/basic_lstm_cell/kernel 已经存在,不允许
【发布时间】:2017-12-06 08:54:06
【问题描述】:

这是我用 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?最初定义于:

【问题讨论】:

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


    【解决方案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)
    

    【讨论】:

    • 我尝试了你提到的两种方法,但我得到了错误。以下是我使用第一种方法得到的错误: ValueError: Input graph and Layer graph are not the same: Tensor("split:0", shape=(?, 28), dtype=float32) is not from the pass-in图表。
    • 这意味着您正在重置图形,但不会重新定义某些节点。这一切都归结为您的笔记本的组织方式
    • 把 tf.reset_default_graph() 放在代码的开头就可以了!
    猜你喜欢
    • 1970-01-01
    • 2018-02-07
    • 2017-11-20
    • 1970-01-01
    • 2018-02-13
    • 2019-03-05
    • 2017-12-26
    • 2020-05-03
    • 2018-03-19
    相关资源
    最近更新 更多