如何在tensorflow中恢复会话?

Or *_*ets 6 python machine-learning tensorflow

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

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

现在我把文件夹中的3个文件:checkpoint,model.ckpt,和model.ckpt.meta

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

我怎样才能做到这一点?

Ole*_*ede 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)
Run Code Online (Sandbox Code Playgroud)

我假设您已经完成了此操作,因为您已经获得了三个文件。当你想在另一个类中加载模型时,你可以这样做:

# 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
Run Code Online (Sandbox Code Playgroud)

  • 当您调用“restore()”时,它会恢复模型中的所有变量/权重。因此,首先您将构建模型(/图表),然后运行改变图表中权重的训练,然后保存权重。当您运行“restore()”时,它会恢复您保存的权重,但不会恢复模型/图形。因此,在恢复之前,您需要设置与保存权重时相同的模型,以便加载的权重适合模型。恢复权重后,您可以运行单个预测,而无需先训练权重。 (2认同)