Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about **How to save/restore a model after training** **in Python**. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

## How to save/restore a model after training?

**How to save/restore a model after training?**The model definition can be restored using

`tf.import_graph_def`

, and the weights are restored using`Saver`

.**save/restore a model after training**The model definition can be restored using

`tf.import_graph_def`

, and the weights are restored using`Saver`

.

## Method 1

For TensorFlow version < 0.11.0RC1:

The checkpoints that are saved contain values for the `Variable`

s in your model, not the model/graph itself, which means that the graph should be the same when you restore the checkpoint.

Here’s an example for a linear regression where there’s a training loop that saves variable checkpoints and an evaluation section that will restore variables saved in a prior run and compute predictions. Of course, you can also restore variables and continue training if you’d like.

x = tf.placeholder(tf.float32) y = tf.placeholder(tf.float32) w = tf.Variable(tf.zeros([1, 1], dtype=tf.float32)) b = tf.Variable(tf.ones([1, 1], dtype=tf.float32)) y_hat = tf.add(b, tf.matmul(x, w)) ...more setup for optimization and what not... saver = tf.train.Saver() # defaults to saving all variables - in this case w and b with tf.Session() as sess: sess.run(tf.initialize_all_variables()) if FLAGS.train: for i in xrange(FLAGS.training_steps): ...training loop... if (i + 1) % FLAGS.checkpoint_steps == 0: saver.save(sess, FLAGS.checkpoint_dir + 'model.ckpt', global_step=i+1) else: # Here's where you're restoring the variables w and b. # Note that the graph is exactly as it was when the variables were # saved in a prior training run. ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: ...no checkpoint found... # Now you can run the model to get predictions batch_x = ...load some data... predictions = sess.run(y_hat, feed_dict={x: batch_x})

## Method 2

There are two parts to the model, the model definition, saved by `Supervisor`

as `graph.pbtxt`

in the model directory and the numerical values of tensors, saved into checkpoint files like `model.ckpt-1003418`

.

The model definition can be restored using `tf.import_graph_def`

, and the weights are restored using `Saver`

.

However, `Saver`

uses special collection holding list of variables that’s attached to the model Graph, and this collection is not initialized using import_graph_def, so you can’t use the two together at the moment (it’s on our roadmap to fix). For now, you have to use approach of Ryan Sepassi — manually construct a graph with identical node names, and use `Saver`

to load the weights into it.

(Alternatively you could hack it by using by using `import_graph_def`

, creating variables manually, and using `tf.add_to_collection(tf.GraphKeys.VARIABLES, variable)`

for each variable, then using `Saver`

)

**Conclusion**

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

**Also, Read**