原文链接:https://blog.csdn.net/qq_25987491/article/details/80529435
saver = tf.train.Saver() #用于保存神经网络结构,构造方法可以传参数,参数可以是dict和list。不传参数时默认保存所有变量
with tf.Session() as sess:
tf.initialize_all_variables().run() #初始化所有变量
ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) #获取checkpoints对象
if ckpt and ckpt.model_checkpoint_path:##判断ckpt是否为空,若不为空,才进行模型的加载,否则从头开始训练
saver.restore(sess,ckpt.model_checkpoint_path)#恢复保存的神经网络结构,实现断点续训
for i in range(TRAINING_STEPS):
xs, ys = mnist.train.next_batch(BATCH_SIZE) #产生这一轮的训练数据
_, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: ys})
if i % 1000 == 0:
print("After %d training step(s), loss on training batch is %g." % (step, loss_value))
saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)#保存神经网络结构