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:
- Create a Saver object, specifying the variables you want to save:
saver = tf.compat.v1.train.Saver(var_list=tf.global_variables())
- Call the
save()
method of the Saver object, specifying the path where you want to save the model:
saver.save(sess, './my_model.ckpt')
- To restore the model from a saved checkpoint, use the
restore()
method of the Saver object:
saver.restore(sess, './my_model.ckpt')
- To save the model as a SavedModel, use the
tf.saved_model.save()
method:
tf.saved_model.save(sess, './my_model')
- To restore the model from a SavedModel, use the
tf.saved_model.load()
method:
tf.saved_model.load(sess, './my_model')
- 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'])
- 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
More of Python Tensorflow
- How can I use Python and TensorFlow to handle illegal hardware instructions in Zsh?
- ¿Cómo implementar reconocimiento facial con TensorFlow y Python?
- How do I resolve a SymbolAlreadyExposedError when the symbol "zeros" is already exposed as () in TensorFlow Python util tf_export?
- How can I download Python TensorFlow?
- How do I use the Python TensorFlow documentation?
- How can I use Python and TensorFlow to implement YOLO object detection?
- How can I use Python and TensorFlow to implement YOLOv4?
- How do I use TensorFlow 1.x with Python?
- How do I use TensorFlow in Python?
- How do I use the Xception model in TensorFlow with Python?
See more codes...