Let's suppose we want to use the results of this trained model repeatedly, but without re-training the model each time.
How to save and restore a TensorFlow model
Saving a model
To save a model, we use the Saver() class. It saves the graph structure using checkpoints: these are binary files in a proprietary format, which map variable names to tensor values. The following code saves the model into our current working directory as two files:
- softmax_mnist.ckpt, which contains the weights
- softmax_mnist.ckpt.meta, which contains the graph definition
The following code must be inserted at the end of the previous model:
saver = tf.train.Saver()...
save_path = saver.save(sess, "softmax_mnist")
print("Model saved to %s" % save_path)