How to restore a session in a tensor flow?

I want to use my neural network without training the network again. I read about

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

and now I have 3 files in the folder: checkpoint model.ckpt model.ckpt.meta p>

I want in another python class to recover data, get the weight of my neural network and make one prediction.

How can i do this?

+4
source share
1 answer

To save the model, you can do the following:

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)

I assume that you have already done this, since you have three files. When you want to load a model into another class, you can do it like this:

# 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
+2
source

Source: https://habr.com/ru/post/1663220/


All Articles