6

I want to use my neural network without training the net again. I read about

save_path = saver.save(sess, "model.ckpt")
print("Model saved in file: %s" % save_path)

and now I have 3 files in the folder: checkpoint, model.ckpt, and model.ckpt.meta

I want, in another class in python to restore the data, get the weights of my neural network and make a single prediction.

How can I do this?

Uvuvwevwevwe
  • 971
  • 14
  • 30
Or Perets
  • 389
  • 1
  • 9
  • 21

1 Answers1

2

To save the model you can do like this:

model_checkpoint = 'model.chkpt'

# Create the model
...
...

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())

    # Create a saver so we can save and load the model as we train it
    tf_saver = tf.train.Saver(tf.all_variables())

    # (Optionally) do some training of the model
    ...
    ...

    tf_saver.save(sess, model_checkpoint)

I assume you have already done this, since you have gotten three files. When you want to load the model in another class, you can do it like this:

# The same file as we saved earlier
model_checkpoint = 'model.chkpt'

# Create the SAME model as before
...
...

with tf.Session() as sess:
    # Restore the model
    tf_saver = tf.train.Saver()
    tf_saver.restore(sess, model_checkpoint)

    # Now your model is loaded with the same values as when you saved,
    #   and you can do prediction or continue training
  • thanks. I got error message: ValueError: No variables to save – Or Perets Dec 08 '16 at 11:08
  • Do you get the error when you are trying to restore the model, or when trying to save it? – Ole Steinar Skrede Dec 08 '16 at 11:10
  • when I`m trying to restore it .. – Or Perets Dec 08 '16 at 11:12
  • Have you looked at this: [link](http://stackoverflow.com/questions/33759623/tensorflow-how-to-restore-a-previously-saved-model-python/) You need to setup the model in the exact same way both before you save it and restore it. Are you sure that you are trying to restore the **same** model? – Ole Steinar Skrede Dec 08 '16 at 11:16
  • yes, thank you. now when i want to restore it, first it train the net again.. but I want to make single prediction without trained again. do you have some suggestions? – Or Perets Dec 08 '16 at 11:23
  • maybe just to get the weights from net.. – Or Perets Dec 08 '16 at 11:23
  • 2
    When you call `restore()`, it restores all the variables/weights in the model. So first you would build the model(/graph), then you run training which changes the weights in the graph, and then you save the weights. When you run `restore()`, it restores the weights that you saved, but it does not restore the model/graph. So before restoring, you need to setup the same model as you had when you saved the weights, so that the weights you load fits into the model. After you have restored the weights, you can run a single prediction without having to train the weights first. – Ole Steinar Skrede Dec 08 '16 at 11:35
  • Seems like you don't actually have to have the exact same model. You can choose which variables you want to save. Have a look [here](http://stackoverflow.com/questions/41034888/export-weights-of-neural-network-using-tensorflow?rq=1) and [here](https://www.tensorflow.org/versions/r0.12/api_docs/python/state_ops.html#Saver). I think you still need to have equivalent variables present when you try to restore variables. But it might be easier to just save and restore all the weights first, and get that to work. But know that the saver only saves the variables, not the graph. – Ole Steinar Skrede Dec 08 '16 at 11:46
  • maybe there is a simple way just to save the weights instead of all variables and then do a quick math calculation? – Or Perets Dec 08 '16 at 11:57
  • I am not sure if I have understood you correctly.. Can you post your code? – Ole Steinar Skrede Dec 08 '16 at 12:09
  • I thing it will be too long for here but thanks anyway! – Or Perets Dec 08 '16 at 12:27