Tensorflow allows us to save/load model's structure, using method tf.train.write_graph, so that we can restore it in the future to continue our training session. However, I'm wondering that if this is necessary because I can create a module, e.g GraphDefinition.py, and use this module to re-create the model. So, which is the better way to save the model structure or are there any rule of thumb that suggest which way should I use when saving a model?
Asked
Active
Viewed 2.1k times
2
-
Could you supply some code example? Or further information? I don't get why creating a module matters in order to save the weights of a TensorFlow model. You can always save the whole graph (looking at the size there's some overhead, I guess) or specify which weights should be saved. However, despite the used memory there isn't much of a difference. – daniel451 Aug 14 '16 at 05:32
1 Answers
3
First of all you have to understand, that tensorflow graph does not have current weights in it (until you save them manually there) and if you load model structure from graph.pb, you will start you train from the very beginning. But if you want to continue train or use your trained model, you have to save checkpoint (using tf Saver) with the values of the variables in it, not only the structure. Check out this tread: Tensorflow: How to restore a previously saved model (python)
-
Yeah. I understand that I have to use tf.Saver to save the weight. What I couldn't get is that is it necessary to save model structure to the graph.pb and reload the graph from that file or should I re-create the graph by myself? – user3425082 Aug 15 '16 at 09:39
-
You should have the same names (not python\c++ names, but tf, ie: a = tf.Variable(value, name='define-tf-name-here')) for important ops and variables in graph and checkpoint file to load values correctly, but how to get this - is totally up to you. Saving and restoring your graph definition file or recreating it with code both works. – Alex Joz Aug 15 '16 at 11:15