This blog post is written when TensorFlow was version r0.12.
Stackoverflow and the TensorFlow documentations are pretty clear that Saver is what you want, but it is less clear how to use it in the code. It boils down to this:
- Define model
- Create session
- Initialize variables
- If restoring, call saver.restore, passing in the session and path to the directory containing checkpoint files
- When saving, call saver.save, passing in the session and the path to the save directory
# ... code to define model omitted ... # Initializing the variables init = tf.initialize_all_variables() saver = tf.train.Saver() with tf.Session() as sess: sess.run(init) if restore: ckpt = tf.train.get_checkpoint_state(save_path) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: # ... training code omitted ... saver.save(sess, save_path)
What if you want to train on one computer and restore it on a different computer? You will get an error: open the file named “checkpoint” with a text editor and edit the paths to the new path.