TensorFlow, why are there 3 files after saving the model?

After reading the docs , I saved the model in TensorFlow , here is my demo code:

 # Create some variables. v1 = tf.Variable(..., name="v1") v2 = tf.Variable(..., name="v2") ... # Add an op to initialize the variables. init_op = tf.global_variables_initializer() # Add ops to save and restore all the variables. saver = tf.train.Saver() # Later, launch the model, initialize the variables, do some work, save the # variables to disk. with tf.Session() as sess: sess.run(init_op) # Do some work with the model. .. # Save the variables to disk. save_path = saver.save(sess, "/tmp/model.ckpt") print("Model saved in file: %s" % save_path) 

but after that I found that there are 3 files

 model.ckpt.data-00000-of-00001 model.ckpt.index model.ckpt.meta 

And I can’t restore the model by restoring the model.ckpt file, since there is no such file. Here is my code

 with tf.Session() as sess: # Restore variables from disk. saver.restore(sess, "/tmp/model.ckpt") 

So why are there 3 files?

+42
tensorflow
Dec 21 '16 at 14:23
source share
3 answers

Try the following:

 with tf.Session() as sess: saver = tf.train.import_meta_graph('/tmp/model.ckpt.meta') saver.restore(sess, "/tmp/model.ckpt") 

The TensorFlow save method saves three types of files because it preserves the graph structure separately from the variable values. . The .meta file describes the saved chart structure, so you need to import it before restoring the breakpoint (otherwise it does not know which variables correspond to the stored breakpoint values).

Alternatively, you can do this:

 # Recreate the EXACT SAME variables v1 = tf.Variable(..., name="v1") v2 = tf.Variable(..., name="v2") ... # Now load the checkpoint variable values with tf.Session() as sess: saver = tf.train.Saver() saver.restore(sess, "/tmp/model.ckpt") 

Despite the absence of a file named model.ckpt , you still refer to a saved breakpoint by that name when restoring it. From saver.py source code : "Users only need to interact with the user-specified prefix ... instead of any physical path."

+47
Dec 21 '16 at 22:58
source share
  • metafile : describes the saved chart structure, including GraphDef, SaverDef, etc .; then apply tf.train.import_meta_graph('/tmp/model.ckpt.meta') , restore Saver and Graph .

  • index file : this is an immutable string string table (tensorflow :: table :: Table). Each key is a tensor name, and its value is a serialized BundleEntryProto. Each BundleEntryProto describes tensor metadata: which of the β€œdata” contains the contents of the tensor, offset to this file, checksum, some auxiliary data, etc.

  • data file : this is a TensorBundle collection that stores the values ​​of all variables.

+14
Jul 11 '17 at 11:38 on
source share

I am restoring teachable vocabulary inserts from the Word2Vec tenor tutorial.

If you created multiple breakpoints:

eg. the generated files are as follows

model.ckpt-55695.data-00000-of-00001

model.ckpt-55695.index

model.ckpt-55695.meta p>

try it

 def restore_session(self, session): saver = tf.train.import_meta_graph('./tmp/model.ckpt-55695.meta') saver.restore(session, './tmp/model.ckpt-55695') 

when calling restore_session ():

 def test_word2vec(): opts = Options() with tf.Graph().as_default(), tf.Session() as session: with tf.device("/cpu:0"): model = Word2Vec(opts, session) model.restore_session(session) model.get_embedding("assistance") 
+1
Oct. 20 '17 at 9:10
source share



All Articles