我想重新实现来自https://www.tensorflow.org/api_docs/python/tf/contrib/rnn/static_rnn的RNN步骤循环,但对我来说不起作用。我在不重用时得到“变量 test/basic_lstm_cell/weights 已存在”的错误,而在将重用设置为True时得到“变量 test/basic_lstm_cell/weights 不存在”的错误。
import tensorflow as tfbatch_size = 32n_steps = 10lstm_size = 10n_input = 17words = 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)
回答:
请查看你试图重新实现的函数的源代码。重要的是,在循环的第一次迭代中不设置重用标志,但在所有其他迭代中设置。所以在你的情况下,一个包含循环的作用域并在整个作用域内保持标志恒定是行不通的,你需要做类似下面的操作:
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)