python-tensorflowHow do I save a model in Python TensorFlow?
Saving a model in Python TensorFlow is done by using the tf.train.Saver() class. This class provides methods to save and restore variables from a TensorFlow checkpoint file.
Example code
# Create a saver object
saver = tf.train.Saver()
# Train the model
with tf.Session() as sess:
# Initialize all variables
sess.run(tf.global_variables_initializer())
# Train the model
for step in range(1001):
sess.run(train)
if step % 100 == 0:
saver.save(sess, 'my_model', global_step=step)
The code above creates a Saver object and saves the model every 100 steps. The model is saved with the name my_model and the global step number.
Code explanation
tf.train.Saver(): Creates a saver object.saver.save(sess, 'my_model', global_step=step): Saves the model in a TensorFlow checkpoint file with namemy_modeland the global step number.
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 TensorFlow Lite with XNNPACK in Python?
- How can I install and use TensorFlow on a Windows machine using Python?
- How can I check the compatibility of different versions of Python and TensorFlow?
- How do I use TensorFlow in Python?
- How can I disable warnings in Python TensorFlow?
- How do I read a CSV file using Python and TensorFlow?
- How do I train a model using Python and TensorFlow?
- How can I use Python and TensorFlow to implement reinforcement learning?
See more codes...