python - Tensorflow 在每次 session 运行时创建一组新的已经存在的变量?

标签 python variables scope tensorflow prediction

我终于可以使用我的 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

我将此代码嵌套在一个更大的类中,该类仅包含图表的其余部分。为了训练它,我只是一遍又一遍地调用我的“训练”方法。这似乎工作正常,问题最终是预测

所以我的问题有两个方面:

  1. 为什么我只在第一次 预测之后才需要某种变量共享,但第一次调用不会失败?我需要做什么来修复此代码,以便我可以多次预测而不会导致错误?

  2. 什么时候变量共享有用,为什么每次运行 Tensorflow 都会创建新变量?我怎样才能防止这种情况(我想防止它吗?)?

谢谢!

最佳答案

向该代码块添加打印语句。我怀疑它被多次调用。或者您可能正在创建类的多个实例,其中每个类都应该有自己的范围名称。

回答您的问题。

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 的理想行为。

关于python - Tensorflow 在每次 session 运行时创建一组新的已经存在的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40833725/

相关文章:

python - odoo中的slug有什么用

ruby-on-rails - Rails 5 - 使用 Pundit Scopes 和 Statesman 状态机 : structurally incompatible?

python - django ModelChoiceField从views.py设置查询集

python - 覆盖设置和包默认值

variables - 在 sed 命令中使用变量而不是值来替换文本文件中的单词

xcode - xcode7 调试器发生了什么变化,我无法再使用 "po"/Swift 查看变量

python OOP 类方法保留变量。奇怪的范围界定事情

JavaScript - 嵌套函数删除数组的 .length

python - Tkinter 将文本调整为内容

html - 在 CSS 旋转函数中使用 CSS 变量