Python 保存/恢复动态模型时出现TensorFlow错误
如果模型是CNN,我可以保存和恢复模型,但我不能恢复RNN 我制作了这样的RNN网络。 我想保存经过训练的体重和偏见或模型。我想在没有训练的情况下进行预测。下面是main.pyPython 保存/恢复动态模型时出现TensorFlow错误,python,tensorflow,Python,Tensorflow,如果模型是CNN,我可以保存和恢复模型,但我不能恢复RNN 我制作了这样的RNN网络。 我想保存经过训练的体重和偏见或模型。我想在没有训练的情况下进行预测。下面是main.py #main.py tf_x = tf.placeholder(tf.float32, [None, seq_length, data_dim], name='tf_x') tf_y = tf.placeholder(tf.int32, [None, output_dim], name='tf_y') rnn_cell
#main.py
tf_x = tf.placeholder(tf.float32, [None, seq_length, data_dim], name='tf_x')
tf_y = tf.placeholder(tf.int32, [None, output_dim], name='tf_y')
rnn_cell = tf.contrib.rnn.BasicLSTMCell(num_units=hidden_dim)
outputs, (h_c, h_n) = tf.nn.dynamic_rnn( rnn_cell,
tf_x,
initial_state=None,
dtype=tf.float32,
time_major=False )
output = tf.layers.dense(outputs[:, -1, :], output_dim, name='dense_output')
loss = tf.losses.softmax_cross_entropy(onehot_labels=tf_y, logits=output)
train_op = tf.train.AdamOptimizer(LR).minimize(loss)
accuracy = tf.metrics.accuracy( labels=tf.argmax(tf_y, axis=1), predictions=tf.argmax(output, axis=1),)[1]
with tf.Session as sess:
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) # the local var is for accuracy_op
sess.run(init_op) # initialize var in graph
...(training)
saver = tf.train.Saver()
save_path = saver.save(sess, "Save data/RNN-model")
saver.export_meta_graph(filename="Save Data/RNN-model.meta", as_text=True)
在“run.py”中,我尝试加载这些数据
#run.py
...(same as main.py)
saver = tf.train.Saver()
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state('Save data/')
saver.restore(sess, ckpt.model_checkpoint_path)
saver = tf.train.import_meta_graph("Save data/RNN-model.meta")
... (prediction)
结果是
tensorflow.python.framework.errors_impl.NotFoundError: Key dense/bias not found in checkpoint
您认为问题出在哪里?(1)在run.py中,使用block将
saver
放入中(2)仔细检查使用python代码构建的模型“…(与main.py相同)”实际上是相同的(将公共代码重构为单独的文件以避免复制粘贴错误)(3)如果您使用代码构建模型,然后恢复权重,则无需导入元图(一种或另一种),这可能会对您有所帮助:@Drop谢谢。我把这个保护程序放在里面,删除关于元图的代码并检查代码。但现在出现了尝试使用未初始化的值精度/总计的失败预处理错误。