我按照语言建模教程:https://www.tensorflow.org/tutorials/recurrent并阅读相应的代码:https://github.com/tensorflow/models/blob/master/tutorials/rnn/ptb/ptb_word_lm.py .

我发现作者从第237行开始编码:

outputs = []
with tf.variable_scope("RNN"):
  for time_step in range(self.num_steps):
    if time_step > 0: tf.get_variable_scope().reuse_variables()
    (cell_output, state) = cell(inputs[:, time_step, :], state)
    outputs.append(cell_output)
output = tf.reshape(tf.concat(outputs, 1), [-1, config.hidden_size])
return output, state

我知道在调用方法 __call__ 时会创建 RNNCell (或任何其他单元格)中的变量,并且应该在第一个时间步之后共享变量 . 但在我的实验中,我删除了这一行:

if time_step > 0: tf.get_variable_scope().reuse_variables()

并打印 tf.all_variables()

enter image description here

看来我不需要编写代码来重用变量,我错了吗?

(我也在tensorflow r0.12(相应的代码:https://github.com/tensorflow/tensorflow/blob/r0.12/tensorflow/models/rnn/ptb/ptb_word_lm.py)做了这个实验,如果我删除那行,它会引发一个

ValueError:变量模型/ RNN / MultiRNNCell / Cell0 / BasicLSTMCell / Linear / Matrix已经存在,不允许 . )

此外,如果我在同一个变量范围内创建多个 RNNCell 对象,我认为它们共享相同的变量,但实际上每个对象都会创建自己的变量 . 我真的很困惑 .