我试图根据教程保存和还原LSTM模型。实际上,它可以在普通模型保存和恢复CNN模型中起作用。但是,当我尝试还原LSTM模型时,它会引发错误
FailedPreconditionError(请参阅上文有关追溯):尝试使用非专业化值rnn_model/rnn/multi_rnn_cell/cell_0/basic_lstm_cell/kernel/kernel
关键代码显示的内容如下:
with tf.Session() as sess:
saver.restore(sess, saver_path)
with tf.variable_scope('RNN_model', reuse=None):
train_rnn = RNNmodel.LSTMmodel(True, RNNmodel.TRAIN_BATCH_SIZE, RNNmodel.NUM_STEP)
with tf.variable_scope('RNN_model', reuse=True):
test_rnn = RNNmodel.LSTMmodel(False, RNNmodel.EVAL_BATCH_SIZE, RNNmodel.NUM_STEP)
我想知道保存和还原中的普通模型和LSTM模型之间是否有任何区别。请帮助
编辑:我尝试移动restore
并起作用,但是当我运行模型时,它仍然会引发相同的错误,我的run_epoch代码如下:
def run_epoch(session, model, datas, train_op, is_log, epoch=3000):
state = session.run(model.initiate_state)
total_cost = 0
for i in range(epoch):
data, label = random_get_data(datas, model.batch_size, num_step=RNNmodel.NUM_STEP)
feed_dict = {
model.input_data: data,
model.target: label,
model.initiate_state: state
}
cost, state, argmax_logit, target, _ = session.run([model.loss, model.final_state, model.argmax_target, model.target, train_op], feed_dict)
日志位于:
处的错误cost, state, argmax_logit, target, _ = session.run([model.loss, model.final_state, model.argmax_target, model.target, train_op], feed_dict)
,日志显示如下:
tensorflow.python.framework.errors_impl.failedpreconditionError:尝试使用非专业的值
看来restore
没有还原LSTM内核操作。我应该做任何事情来启动LSTM操作吗?
edit2 :我终于查看了checkpoint
文件,我确定save
操作剂量不能保存有关LSTM单元的变量,我不知道为什么。似乎我必须明确地命名变量,否则我将无法保存,并且Basicallstmcell class init ()剂量没有name
参数。
没有区别,rnns使用普通变量。
我认为你必须移动
saver.restore(sess, saver_path)
创建LSTMMODEL后。否则,当您调用还原时,它的变量就不在图中 - 因此不会恢复它们。
我终于弄清楚了。根据节省和恢复受过张量的LSTM,以及 jeronimo garcia-loygorri 的答案,我在LSTM模型的定义后移动Saver
的创建,然后每个问题都消失了!