My training process in tensorflow involves switching between two models. While using tf.saver and restore the model from the hard disk is really time-consuming (In my code, the switching is frequent), therefore, I wonder if there is a way to store the model parameters in the memory and restore them just from memory. My model is rather small, which can be definitely stored in RAM. There is one answer from stackoverflow. Storing tensorflow models in memory However, I do not quite understand how this can work. Does anyone know how to achieve this goal? Thank you.
Asked
Active
Viewed 2,288 times
2
-
What specifically do you need help with understanding? The question you linked does have an answer. – msitt Apr 26 '17 at 15:30
-
I do not understand how to use it. What does Comp1 and Comp2 actually mean? – Mengxiao Zhang Apr 27 '17 at 00:49
1 Answers
2
You should just use two separate graphs like this:
g1 = tf.Graph()
g2 = tf.Graph()
with g1.as_default():
# build your 1st model
sess1 = tf.Session(graph=g1)
# do some work with sess1 on g1
sess1.run(...)
with g2.as_default():
# build your 2nd model
sess2 = tf.Session(graph=g2)
# do some work with sess2 on g2
sess2.run(...)
with g1.as_default():
# do some more work with sess1 on g1
sess1.run(...)
with g2.as_default():
# do some more work with sess2 on g2
sess2.run(...)
sess1.close()
sess2.close()
You don't actually need the with
statements, once you've created sess1
and sess2
you can just use them, they'll refer to the correct graph, but it's probably good form to set the default graph whenever you're working with that graph while you're still getting used to how TF juggles global variables.

David Parks
- 30,789
- 47
- 185
- 328
-
Really thank you for your answer, and I also want to know how I can copy all the parameters in model 1 to model 2 (Actually they are in the same structure). Now what I do is store model 1, restore model 2, and then restore model 1, which is time-consuming – Mengxiao Zhang Apr 27 '17 at 03:01
-
I haven't tried that, but it sounds like it's easy from a quick google search: http://stackoverflow.com/questions/36438800/tensorflow-transferring-variables-across-graphs https://www.tensorflow.org/programmers_guide/variable_scope – David Parks Apr 27 '17 at 15:25
-
This one might also be useful: http://stackoverflow.com/questions/41600321/distributed-tensorflow-the-difference-between-in-graph-replication-and-between/41601168#41601168 – David Parks Apr 27 '17 at 15:27