9951 explained code solutions for 126 technologies


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

  1. tf.train.Saver(): Creates a saver object.
  2. saver.save(sess, 'my_model', global_step=step): Saves the model in a TensorFlow checkpoint file with name my_model and the global step number.

Helpful links

Edit this code on GitHub