how to use eager execution to save and restore in TensorFlow?


April 2019


3 time


We always use tf.train.Saver() to save and restore weights, like the following example(

But how to use eager execution to save? how to change the following example?

Another question, is it a good idea to use eager?

I fond tf.contrib.eager.Saver in (, but it says "Saver's name-based checkpointing strategy is fragile". What does it mean?

# Create some variables.
v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer)

inc_v1 = v1.assign(v1+1)
dec_v2 = v2.assign(v2-1)

# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, initialize the variables, do some work, and save the
# variables to disk.
with tf.Session() as sess:
  # Do some work with the model.
  # Save the variables to disk.
  save_path =, "/tmp/model.ckpt")
  print("Model saved in path: %s" % save_path)

0 answers