恢复并继续培训

时间:2016-11-01 11:56:07

标签: python-2.7 tensorflow

我有一个mnist训练模型,输出10个类并保存在检查点,我想更新Netwok输出11个类,我试图恢复我以前训练过的继续训练。我怎么能这样做。我们假设我们有这个代码

image_size = 28
num_labels = 10
batch_size = 10
patch_size = 5
depth = 16
num_hidden = 64
with graph.as_default():

# Input data.
   tf_train_dataset = tf.placeholder(
   tf.float32, shape=(batch_size, image_size, image_size, num_channels))
   tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
   tf_valid_dataset = tf.constant(valid_dataset)
   tf_test_dataset = tf.constant(test_dataset)


  # Variables.
   layer1_weights = tf.Variable(tf.truncated_normal(
       [patch_size, patch_size, num_channels, depth], stddev=0.1))
   layer1_biases = tf.Variable(tf.zeros([depth]))
   layer2_weights = tf.Variable(tf.truncated_normal(
       [patch_size, patch_size, depth, depth], stddev=0.1))
   layer2_biases = tf.Variable(tf.constant(1.0, shape=[depth]))
   layer3_weights = tf.Variable(tf.truncated_normal(
       [image_size // 7 * image_size // 7 * depth, num_hidden], stddev=0.1))
   layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hidden]))
   layer4_weights = tf.Variable(tf.truncated_normal(
      [num_hidden, num_labels], stddev=0.1))
   layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]))

 # Model.
   def model(data):
     conv_1 = tf.nn.conv2d(data, layer1_weights, [1, 2, 2, 1], padding='SAME')
     hidden_1 = tf.nn.relu(conv_1 + layer1_biases)
     pool_1 = tf.nn.max_pool(hidden_1,ksize = [1,2,2,1], strides= [1,2,2,1],padding ='SAME' )
     conv_2 = tf.nn.conv2d(pool_1, layer2_weights, [1, 2, 2, 1], padding='SAME')
     hidden_2 = tf.nn.relu(conv_2 + layer2_biases)    
     shape = hidden_2.get_shape().as_list()  
     reshape = tf.reshape(hidden_2, [shape[0], shape[1] * shape[2] * shape[3]])
     hidden = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases)
     return tf.matmul(hidden, layer4_weights) + layer4_biases

   logits = model(tf_train_dataset)
   loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))

 # Optimizer.
   optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss)

 # Predictions for the training, validation, and test data.
   train_prediction = tf.nn.softmax(logits)
   valid_prediction = tf.nn.softmax(model(tf_valid_dataset))
   test_prediction = tf.nn.softmax(model(tf_test_dataset))
   init = initialize_all_variables()
   saver = tf.train.Saver(tf.all_Variables())

num_steps = 101
with tf.Session(graph=graph) as sess:
  if os.path.isfile(ckpt) :
    saver.restore(sess, ckpt)
  else:
    sess.run(init)
    for step in range(num_steps):
      offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
      batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
      batch_labels = train_labels[offset:(offset + batch_size), :]

      feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
      _, l, predictions = session.run(
  [optimizer, loss, train_prediction], feed_dict=feed_dict)

      if (step % 50 == 0):
        print('Minibatch loss at step %d: %f' % (step, l))
        print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))
        print('Validation accuracy: %.1f%%' % accuracy(
           valid_prediction.eval(), valid_labels))
    print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))

    save_path= saver.save(sess, "check_point_path.ckpt")
    print("Model saved in file: %s" % save_path)

0 个答案:

没有答案