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 do I resolve a SymbolAlreadyExposedError when the symbol "zeros" is already exposed as () in TensorFlow Python util tf_export?
- How can I use Python and TensorFlow to handle illegal hardware instructions in Zsh?
- How can I use TensorFlow Lite with XNNPACK in Python?
- ¿Cómo implementar reconocimiento facial con TensorFlow y Python?
- How can I use YOLOv3 with Python and TensorFlow?
- How can I use Python and TensorFlow to implement YOLO object detection?
- How can I use Python and TensorFlow to create an XOR gate?
- How do I install Tensorflow with a Python wheel (whl) file?
- How can I use Python and TensorFlow together?
- How can I use Tensorflow 1.x with Python 3.8?
See more codes...