Tensorflow 在每个会话 运行 中创建一组新的已经存在的变量?
Tensorflow creates a new set of already existing variables each session run?
我终于开始使用我的 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
我将这段代码嵌套在一个更大的 class 中,它只包含图表的其余部分。为了训练它,我只是一遍又一遍地调用我的 "train" 方法。这似乎工作正常,问题最终是 prediction.
所以我的问题有两个:
为什么我只在 first 预测之后才需要某种变量共享,但第一次调用不会失败?我需要什么来修复此代码,以便我可以多次预测而不会导致错误?
变量共享什么时候有用,为什么每次我 运行 Tensorflow 都会创建新变量?我怎样才能防止这种情况(我想防止它吗?)?
谢谢!
向该代码块添加打印语句。我怀疑它被多次调用。或者您正在创建 class 的多个实例,其中每个 class 都应该有自己的作用域名称。
回答您的问题。
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 所需的行为。
我终于开始使用我的 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
我将这段代码嵌套在一个更大的 class 中,它只包含图表的其余部分。为了训练它,我只是一遍又一遍地调用我的 "train" 方法。这似乎工作正常,问题最终是 prediction.
所以我的问题有两个:
为什么我只在 first 预测之后才需要某种变量共享,但第一次调用不会失败?我需要什么来修复此代码,以便我可以多次预测而不会导致错误?
变量共享什么时候有用,为什么每次我 运行 Tensorflow 都会创建新变量?我怎样才能防止这种情况(我想防止它吗?)?
谢谢!
向该代码块添加打印语句。我怀疑它被多次调用。或者您正在创建 class 的多个实例,其中每个 class 都应该有自己的作用域名称。
回答您的问题。
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 所需的行为。