来自教程的RNN示例代码中的“可变权重已存在”

时间:2021-02-11 20:10:50

I want re-implement RNN step loop from https://www.tensorflow.org/api_docs/python/tf/contrib/rnn/static_rnn but it doesn't work for me. I get "Variable test/basic_lstm_cell/weights already exists" without reuse and "Variable test/basic_lstm_cell/weights does not exist" when reuse is set to True.

我想从https://www.tensorflow.org/api_docs/python/tf/contrib/rnn/static_rnn重新实现RNN步骤循环,但它对我不起作用。当重用设置为True时,我得到“变量test / basic_lstm_cell / weights已存在”而没有重用,并且“Variable test / basic_lstm_cell / weights不存在”。

import tensorflow as tf
batch_size = 32
n_steps = 10
lstm_size = 10
n_input = 17

words = tf.placeholder(tf.float32, [batch_size, n_steps, n_input])
words = tf.transpose(words, [1, 0, 2])
words = tf.reshape(words, [-1, n_input])
words = tf.split(words, n_steps, 0)

with tf.variable_scope('test', reuse=True):
    cell = tf.contrib.rnn.BasicLSTMCell(lstm_size)
    state = cell.zero_state(batch_size, dtype=tf.float32)
    outputs = []
    for input_ in words:
        output, state = cell(input_, state)
        outputs.append(output)

1 个解决方案

#1


1  

Have a look at the source of the function you are trying to re-implement. The important bit is that the reuse flag is not set in the first iteration of the loop, but it is set in all others. So in your case one scope which contains the loop with the flag constant for the scope won't work, you'll have to do something like

看看您尝试重新实现的功能的来源。重要的一点是,在循环的第一次迭代中没有设置重用标志,但是在所有其他循环中都设置了重用标志。所以在你的情况下,一个包含带范围标志常量的循环的范围将不起作用,你将不得不做类似的事情

with tf.variable_scope('test') as scope:
    cell = tf.contrib.rnn.BasicLSTMCell(lstm_size)
    state = cell.zero_state(batch_size, dtype=tf.float32)
    outputs = []
    for step, input_ in enumerate(words):
        if step > 0:
            scope.reuse_variables()
        output, state = cell(input_, state)
        outputs.append(output)

#1


1  

Have a look at the source of the function you are trying to re-implement. The important bit is that the reuse flag is not set in the first iteration of the loop, but it is set in all others. So in your case one scope which contains the loop with the flag constant for the scope won't work, you'll have to do something like

看看您尝试重新实现的功能的来源。重要的一点是,在循环的第一次迭代中没有设置重用标志,但是在所有其他循环中都设置了重用标志。所以在你的情况下,一个包含带范围标志常量的循环的范围将不起作用,你将不得不做类似的事情

with tf.variable_scope('test') as scope:
    cell = tf.contrib.rnn.BasicLSTMCell(lstm_size)
    state = cell.zero_state(batch_size, dtype=tf.float32)
    outputs = []
    for step, input_ in enumerate(words):
        if step > 0:
            scope.reuse_variables()
        output, state = cell(input_, state)
        outputs.append(output)