python – Tensorflow变量重用
发布时间:2020-12-20 12:13:07 所属栏目:Python 来源:网络整理
导读:我已经建立了我的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.r
我已经建立了我的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,vocab_size,infer=True) 上面的代码给了我一个错误 Variable lstm_model/lstm_vars/W already exists,disallowed. Did you mean to set reuse=True in VarScope? 如果我设置了reuse = True,如下面的代码块所示 with tf.variable_scope('lstm_model',reuse=True) as scope: 我得到了一个不同的错误 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? 作为参考,我在下面附上了相关的型号代码. 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()) 我有Adam优化器的相应部分: optimizer = tf.train.AdamOptimizer(self.learning_rate) 解决方法
它似乎不是:
with tf.variable_scope('lstm_model') as scope: # Define LSTM Model lstm_model = LSTM_Model(rnn_size,vocab_size) scope.reuse_variables() test_lstm_model = LSTM_Model(rnn_size,infer_sample=True) 这解决了这个问题 # Define LSTM Model lstm_model = LSTM_Model(rnn_size,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,infer_sample=True) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |