How to restore a model by filename in Tensorflow r12?

前端 未结 6 563
隐瞒了意图╮
隐瞒了意图╮ 2020-12-28 08:44

I have run the distributed mnist example: https://github.com/tensorflow/tensorflow/blob/r0.12/tensorflow/tools/dist_test/python/mnist_replica.py

Though I have set th

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-28 09:16

    I also used Tensorlfow r0.12 and I didn't think there is any issue for saving and restoring model. The following is a simple code that you can have a try:

    import tensorflow as tf
    
    # Create some variables.
    v1 = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name="v1")
    v2 = tf.Variable(tf.random_normal([784, 200], stddev=0.35), 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)
    
    # Later, launch the model, use the saver to restore variables from disk, and
    # do some work with the model.
    with tf.Session() as sess:
      # Restore variables from disk.
      saver.restore(sess, "/tmp/model.ckpt")
      print("Model restored.")
      # Do some work with the model
    

    although in r0.12, the checkpoint is stored in multiple files, you can restore it by using the common prefix, which is 'model.ckpt' in your case.

提交回复
热议问题