0

I have finished running a big model in tensorflow python. But I have not saved it inside the session. Now that the training is over, I want to save the variables. I am doing the following:

saver=tf.train.Saver()
with tf.Session(graph=graph) as sess:  
    save_path = saver.save(sess, "86_model.ckpt")
    print("Model saved in file: %s" % save_path)

This returns : ValueError: No variables to save. According to their website what is missing is initialize_all_variables(). The documentation says little about what exactly that does. The word "initialize" scares me, I do not want to reset all my trained values. Any way to save my model without re-running it?

maininformer
  • 967
  • 2
  • 17
  • 31
  • `saver.save` saves everything in `tf.get_collection(tf.GraphKeys.VARIABLES)`. That's a collection that gets updated every time you create a variable with `tf.Variable` or `tf.get_variable`. How did you create your variables? – Yaroslav Bulatov Mar 25 '16 at 20:28
  • Cool, thanks for clarification, I created all my variables in a graph, and trained that network over 100K iterations, got my testing error and the session is closed. I created my variables using tf.Variable(). I want to access those trained variables and save them, but I do not want to use initialize as I assume that will reset my trained variables. I am following Udacity's course as a reference. – maininformer Mar 25 '16 at 20:49
  • yeah, when you close the session you lose the variables – Yaroslav Bulatov Mar 25 '16 at 22:00

3 Answers3

1

It seems like from the tensorflow documentation, the "session" is the thing that holds the information from the trained model. So presumably somewhere you called sess.run() to train your model - what you want to do is call sess.save() using THAT session, not a new one you create with this saver object.

mprat
  • 2,451
  • 15
  • 33
1

I believe its because you are not initializing all of your variables in the saver. This should work

with tf.Session() as sess:
      tf.initialize_all_variables().run()
      saver = tf.train.Saver(tf.all_variables())
     -------everything your session does -------------
      checkpoint_path = os.path.join(save_dir, 'model.ckpt')
      saver.save(sess, checkpoint_path, global_step = your_global_step)
0

How about using skflow ? With skflow(now skflow is integrated in tensorflow) you can specify the parameter model_dir on your constructor and that automatically will save your model while training(it will save checkpoints so if something goes wrong during training, you can restart from last checkpooint).

Luis Leal
  • 3,388
  • 5
  • 26
  • 49
  • There is this problem with skflow: http://stackoverflow.com/questions/36597519/adding-regularizer-to-skflow I ended up using Keras, worked perfectly. – maininformer Jul 22 '16 at 07:28