1, Tensorflow the saver and checkpoint

1, Tensorflow model file

  • checkpoint
  • model.ckpt-200.data-00000-of-00001
  • model.ckpt-200.index
  • model.ckpt-200.meta

1.1 meta file

Saved model.ckpt-200.meta is a configuration file, simply is the network structure of the neural network. In general the network structure is not changed, so you can save only one on the line. We can use the following code only once saved meta file first.

saver.save(sess, 'my-model', global_step=step,write_meta_graph=False)

And may also be used tf.train.import_meta_graph ( 'model.ckpt-200.meta') can be introduced into the structure of FIG.

1.2 data file

model.ckpt-200.data-00000-of-00001 as a data file, it is stored in the network weights, bias, and so the operation.

1.3 index file

model.ckpt-200.index is not to become a string table, each key is a name tensor, which is a sequence of values ​​of BundleEntryProto. Each metadata describing BundleEntryProto tensor: "Data" file which contains the contents of the tensors, offset, and the checksum, the number of auxiliary data files and the like.

Note: The previous versions of the model tensorflow only save a file.

2 Save and restore Tensorflow model

2.1 Save Model

tf.train.Saver class provides methods for saving and restoring the model. Tf.train.Saver constructor or variable with respect to FIG specified list all variables save and restore op added to FIG. Saver object provides a method of operation of these op, specifies the path to write or read the checkpoint file.

In general, if you do not specify any parameters, tf.train.Saver will save all parameters. The following is a simple example:

import tensorflow as tf
# Create some variables.
v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer)

inc_v1 = v1.assign(v1+1)
dec_v2 = v2.assign(v2-1)

# 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, and save the
# variables to disk.
with tf.Session() as sess:
  sess.run(init_op)
  # Do some work with the model.
  inc_v1.op.run()
  dec_v2.op.run()
  # Save the variables to disk.
  save_path = saver.save(sess, "tmp/model.ckpt")
  print("Model saved in path: %s" % save_path)
View Code

The last will and op v1 and v2 are preserved. But if you just want to save v1 and v2, you can write.

tf.train.Saver(V1.values()+V2.values())

2.2 Load Model

Model loads need to use Saver.restore method. Can load fixed parameters, you can also add in all the parameters.

saver.restore(sess,model_path)

2.3 Load model restrictions

pre-trained model commonly used to transfer learning, but there is a limit, that is, one must be consistent in front of the network to vgg16 example, if you use several layers of extracting features front, front layers of the network and have to vgg consistent. Behind the network parameters are initialized randomly.

references

[1] https://blog.csdn.net/gzj_1101/article/details/80299610

Guess you like

Origin www.cnblogs.com/ai-learning-blogs/p/11530553.html