Tensorflow_restore_model

Save and restore model¶

Now we can load the best model (the one with highest vadidation accuracy) and verify the accuracy on the test set.

In [1]:
# First, load the test set
import numpy as np
import tensorflow as tf

# Reset the graph so there is no error when running in parallel
tf.reset_default_graph()

# Create new session
sess = tf.Session()

#Load data
MNIST_data = np.load('/home/vietanh/data/MNIST/MNIST_data.npz')
test_data = MNIST_data['test_data']
test_labels = MNIST_data['test_labels']

# Load the best model
saver = tf.train.import_meta_graph('/home/vietanh/data/MNIST/model.meta')
saver.restore(sess,'/home/vietanh/data/MNIST/model')
graph = tf.get_default_graph()

# We don't need to build the graph from scratch. 
# However, we need to define all place holders. 

x = graph.get_tensor_by_name("x:0")  
y_ = graph.get_tensor_by_name("y_:0") 
accuracy = graph.get_tensor_by_name("accuracy:0")  

accuracy_test = sess.run(accuracy, feed_dict={x: test_data, y_: test_labels})
print 'Accuracy on test set:',accuracy_test
INFO:tensorflow:Restoring parameters from /home/vietanh/data/MNIST/model
Accuracy on test set: 0.9211