如何在tensorflow中恢复会话?

时间:2016-12-08 10:52:07

标签: python machine-learning tensorflow

我想使用我的神经网络而不再训练网络。 我读到了

save_path = saver.save(sess, "model.ckpt")
print("Model saved in file: %s" % save_path)

现在我在文件夹中有3个文件:checkpointmodel.ckptmodel.ckpt.meta

我希望在python的另一个类中恢复数据,获取神经网络的权重并进行单一预测。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

要保存模型,您可以这样做:

model_checkpoint = 'model.chkpt'

# Create the model
...
...

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())

    # Create a saver so we can save and load the model as we train it
    tf_saver = tf.train.Saver(tf.all_variables())

    # (Optionally) do some training of the model
    ...
    ...

    tf_saver.save(sess, model_checkpoint)

我假设您已经完成了这项工作,因为您已经获得了三个文件。 如果要在另一个类中加载模型,可以这样做:

# The same file as we saved earlier
model_checkpoint = 'model.chkpt'

# Create the SAME model as before
...
...

with tf.Session() as sess:
    # Restore the model
    tf_saver = tf.train.Saver()
    tf_saver.restore(sess, model_checkpoint)

    # Now your model is loaded with the same values as when you saved,
    #   and you can do prediction or continue training