我想从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)
答案 0 :(得分:1)
看看the source of the function you are trying to re-implement。重要的一点是,在循环的第一次迭代中没有设置重用标志,但是在所有其他循环中都设置了重用标志。所以在你的情况下,一个包含带有标志常量的循环的范围不会起作用,你将不得不做类似的事情
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)