Tensorflow变量重用

car*_*ice 3 python variables tensorflow

我已经建立了我的LSTM模型.理想情况下,我希望稍后使用重用变量来定义测试LSTM模型.

with tf.variable_scope('lstm_model') as scope:
    # Define LSTM Model
    lstm_model = LSTM_Model(rnn_size, batch_size, learning_rate,
                     training_seq_len, vocab_size)
    scope.reuse_variables()
    test_lstm_model = LSTM_Model(rnn_size, batch_size, learning_rate,
                     training_seq_len, vocab_size, infer=True)
Run Code Online (Sandbox Code Playgroud)

上面的代码给了我一个错误

Variable lstm_model/lstm_vars/W already exists, disallowed. Did you mean to set reuse=True in VarScope? 
Run Code Online (Sandbox Code Playgroud)

如果我设置了reuse = True,如下面的代码块所示

with tf.variable_scope('lstm_model', reuse=True) as scope:
Run Code Online (Sandbox Code Playgroud)

我得到了一个不同的错误

Variable lstm_model/lstm_model/lstm_vars/W/Adam/ does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?
Run Code Online (Sandbox Code Playgroud)

作为参考,我在下面附上了相关的型号代码.LSTM模型中的相应部分,我有权重

with tf.variable_scope('lstm_vars'):
    # Softmax Output Weights
    W = tf.get_variable('W', [self.rnn_size, self.vocab_size], tf.float32, tf.random_normal_initializer())
Run Code Online (Sandbox Code Playgroud)

我有Adam优化器的相应部分:

optimizer = tf.train.AdamOptimizer(self.learning_rate)
Run Code Online (Sandbox Code Playgroud)

car*_*ice 8

它似乎不是:

with tf.variable_scope('lstm_model') as scope:
    # Define LSTM Model
    lstm_model = LSTM_Model(rnn_size, batch_size, learning_rate,
                     training_seq_len, vocab_size)
    scope.reuse_variables()    
    test_lstm_model = LSTM_Model(rnn_size, batch_size, learning_rate,
                     training_seq_len, vocab_size, infer_sample=True)
Run Code Online (Sandbox Code Playgroud)

这解决了这个问题

# Define LSTM Model
lstm_model = LSTM_Model(rnn_size, batch_size, learning_rate,
                        training_seq_len, vocab_size)

# Tell TensorFlow we are reusing the scope for the testing
with tf.variable_scope(tf.get_variable_scope(), reuse=True):
    test_lstm_model = LSTM_Model(rnn_size, batch_size, learning_rate,
                                 training_seq_len, vocab_size, infer_sample=True)
Run Code Online (Sandbox Code Playgroud)


Vla*_*cky 5

如果您使用一个变量两次(或多次),则应该第一次使用,with tf.variable_scope('scope_name', reuse=False):然后再一次with tf.variable_scope('scope_name', reuse=True):。

或者你可以使用方法 tf.variable_scope.reuse_variables()

with tf.variable_scope("foo") as scope:
    v = tf.get_variable("v", [1])
    scope.reuse_variables()
    v1 = tf.get_variable("v", [1])
Run Code Online (Sandbox Code Playgroud)

在上面的代码中v,v1是相同的变量。