9951 explained code solutions for 126 technologies


python-tensorflowHow do I save a trained model using Python and TensorFlow?


Saving a trained model using Python and TensorFlow is a simple process. The following steps outline the process:

  1. Create a Saver object, specifying the variables you want to save:
saver = tf.compat.v1.train.Saver(var_list=tf.global_variables())
  1. Call the save() method of the Saver object, specifying the path where you want to save the model:
saver.save(sess, './my_model.ckpt')
  1. To restore the model from a saved checkpoint, use the restore() method of the Saver object:
saver.restore(sess, './my_model.ckpt')
  1. To save the model as a SavedModel, use the tf.saved_model.save() method:
tf.saved_model.save(sess, './my_model')
  1. To restore the model from a SavedModel, use the tf.saved_model.load() method:
tf.saved_model.load(sess, './my_model')
  1. To save the model as a frozen graph, use the tf.graph_util.convert_variables_to_constants() method:
tf.graph_util.convert_variables_to_constants(
    sess, sess.graph_def, ['output_node_name'])
  1. To restore the model from a frozen graph, use the tf.import_graph_def() method:
graph_def = tf.GraphDef()
with tf.gfile.GFile('frozen_model.pb', 'rb') as f:
    graph_def.ParseFromString(f.read())

tf.import_graph_def(graph_def)

Helpful links

Edit this code on GitHub