python – Tensorflow在每个会话运行时创建一组新的已存在变量?

我终于使用我的LSTM模型来预测事物了.但是,我遇到了一个我不太了解的新问题.如果我尝试使用预测

sess.run(pred, feed_dict={x: xs})

它适用于第一次预测,但任何后续预测都会引发错误:

ValueError: Variable weight_def/weights already exists, disallowed. Did you mean to set reuse=True in VarScope?

现在,有很多关于此问题的主题 – 其中大部分都可以通过执行它所要求的方式轻松解决 – 只需在违规行周围创建一个变量范围,并使变量重用成为真.现在,如果我这样做,我会收到以下错误:

ValueError: Variable rnn_def/RNN/BasicLSTMCell/Linear/Matrix does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?

这让我很头疼.我一遍又一遍地阅读了Tensorflow Variable Sharing文档,我不能为我的生活弄清楚我做错了什么.这里有违规行

with tf.variable_scope("rnn_def"):
            outputs, states = rnn.rnn(self.__lstm_cell,
                                      self.__x,
                                      dtype=tf.float32)
            self.__outputs = outputs
            self.__states = states

我把这个代码嵌套在一个更大的类中,它只包含图的其余部分.为了训练它,我只是一遍又一遍地称呼我的“火车”方法.这似乎工作正常,问题最终是预测.

所以我的问题有两个:

>为什么我只在第一次预测后需要某种变量共享,但第一次调用没有失败?我需要什么来修复此代码,以便我可以多次预测而不会导致错误?
>什么时候变量共享很有用,为什么Tensorflow每次运行时都会创建新的变量?我该如何防止这种情况(我想阻止吗?)?

谢谢!

最佳答案 在该代码块中添加print语句.我怀疑它被多次调用.或者您可能正在创建该类的多个实例,其中每个类应具有自己的范围名称.

回答你的问题.

Why do I require some sort of variable sharing only after the first
prediction but the first call doesn’t fail? What do I need to fix this
code so I can predict more than once without causing an error?

不,你没有.创建RNN的代码块可能被意外地多次调用.

When is variable sharing useful, and why is Tensorflow creating new
variables each time I run it? How can I prevent this (do I want to
prevent it?)?

在以下情况下,它对我的​​部分图表有不同的输入源非常有用,具体取决于是训练还是预测.

x_train, h_train = ops.sharpen_cell(x_train, h_train, 2, n_features, conv_size, n_convs, conv_activation, 'upsampler')
self.cost += tf.reduce_mean((x_train - y_train) ** 2)

level_scope.reuse_variables()

x_gen, h_gen = ops.sharpen_cell(x_gen, h_gen, 2, n_features, conv_size, n_convs, conv_activation, 'upsampler')
self.generator_outputs.append(tf.clip_by_value(x_gen, -1, 1))

在这个例子中,重用了用训练师训练的发生器的变量.如果要在循环中展开和RNN,它也很有用.比如在这种情况下……

y = #initial value
state = #initial state
rnn = #some sort of RNN cell
with tf.variable_scope("rnn") as scope:
  for t in range(10):
    y, state = rnn(y, state)
    scope.reuse_variabled()

在这种情况下,它将在时间步长之间重用rnn权重,这是RNN的期望行为.

点赞