TensorFlow基础教程:模型持久化(模型保存与读取)
2018-01-24 19:37
585 查看
TensorFlow可以保存训练过的模型,不仅在训练过程中断后,可以继续上次训练过程;还可以进行迁移学习,在别人的训练的模型基础上训练自己的模型。可谓十分方便。
TensorFlow保存模型checkpoint后生成以下文件:
|—checkpoint
|—model_name.data-00000-of-00001
|—model_name.index
|—model_name.meta
model_name为定义好的模型名字
model_name.meta为图文件
model_name.data为数据文件
保存模型
恢复模型
只加载数据
加载图和数据
所有程序代码(基于TensorFlow基础教程:搭建简单的DNN实现手写数字识别)改写
训练代码
模型恢复并计算测试集准确度
github源码下载
https://github.com/gamersover/tensorflow_basic_tutorial/tree/master/model_save_tutorial
TensorFlow保存模型checkpoint后生成以下文件:
|—checkpoint
|—model_name.data-00000-of-00001
|—model_name.index
|—model_name.meta
model_name为定义好的模型名字
model_name.meta为图文件
model_name.data为数据文件
保存模型
saver = tf.train.Saver() #创建saver对象 saver.save(sess, checkpoint_path) #将sess保存到定义好的路径下
恢复模型
只加载数据
saver.restore(sess, checkpoint_path) #从路径中恢复模型到会话sess
加载图和数据
meta_path = 'model_name.meta' #图路径 model_path = 'model_name' #模型路径 saver = tf.train.import_meta_graph(meta_path) #加载图 with tf.Session() as sess: saver.restore(sess, model_path) #恢复会话sess并加载数据 graph = tf.get_default_graph() x = graph.get_tensor_by_name('InputData:0') #从图中获取tensor
所有程序代码(基于TensorFlow基础教程:搭建简单的DNN实现手写数字识别)改写
训练代码
# coding: utf-8 from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) import tensorflow as tf learning_rate = 0.001 train_epochs = 10 batch_size = 64 checkpoint_path = 'checkpoint/' n_input = 784 n_hidden1 = 100 n_hidden2 = 100 n_classes = 10 #name参数,记录变量名字 x = tf.placeholder(tf.float32, shape=[None, n_input], name='InputData') y = tf.placeholder(tf.float32, shape=[None, n_classes], name='LabelData') weights = {'w1': tf.Variable(tf.random_normal([n_input, n_hidden1]), name='W1'), 'w2': tf.Variable(tf.random_normal([n_hidden1, n_hidden2]), name='W2'), 'w3': tf.Variable(tf.random_normal([n_hidden2, n_classes]), name='W3')} biases = {'b1': tf.Variable(tf.random_normal([n_hidden1]), name='b1'), 'b2': tf.Variable(tf.random_normal([n_hidden2]), name='b2'), 'b3': tf.Variable(tf.random_normal([n_classes]), name='b3')} def inference(input_x): layer_1 = tf.nn.relu(tf.matmul(x, weights['w1']) + biases['b1']) layer_2 = tf.nn.relu(tf.matmul(layer_1, weights['w2']) + biases['b2']) out_layer = tf.matmul(layer_2, weights['w3']) + biases['b3'] return out_layer #定义计算过程的名字 with tf.name_scope('Inference'): logits = inference(x) with tf.name_scope('Loss'): loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y)) with tf.name_scope('Optimizer'): optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss) with tf.name_scope('Accuracy'): pre_correct = tf.equal(tf.argmax(y, 1), tf.argmax(tf.nn.softmax(logits), 1)) accuracy = tf.reduce_mean(tf.cast(pre_correct, tf.float32), name='acc') print(accuracy) init = tf.global_variables_initializer() saver = tf.train.Saver() with tf.Session() as sess: sess.run(init) total_batch = int(mnist.train.num_examples / batch_size) checkpoint = tf.train.get_checkpoint_state(checkpoint_path) #获取checkpoint状态 if checkpoint and checkpoint.model_checkpoint_path: saver.restore(sess, checkpoint_path+'model.ckpt') #加载数据 print('continue last train!!') else: print('restart train!!') for epoch in range(train_epochs): for batch in range(total_batch): batch_x, batch_y = mnist.train.next_batch(batch_size) sess.run(train_op, feed_dict={x:batch_x, y:batch_y}) if (epoch+1) % 5 == 0: loss_, acc = sess.run([loss, accuracy], feed_dict={x:batch_x, y:batch_y}) print("epoch {}, loss {:.4f}, acc {:.3f}".format(epoch, loss_, acc)) saver.save(sess, checkpoint_path+'model.ckpt') #模型名字model.ckpt print("optimizer finished!") print("模型保存在", checkpoint_path)
模型恢复并计算测试集准确度
from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) import tensorflow as tf meta_path = 'checkpoint/model.ckpt.meta' #图路径 model_path = 'checkpoint/model.ckpt' #数据路径 saver = tf.train.import_meta_graph(meta_path) #加载图 with tf.Session() as sess: saver.restore(sess, model_path) #加载数据 graph = tf.get_default_graph() x = graph.get_tensor_by_name('InputData:0') #加载张量 y = graph.get_tensor_by_name('LabelData:0') accuracy = graph.get_tensor_by_name('Accuracy/acc:0') #计算测试集的准确度 test_acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels}) print('test accuracy', test_acc)
github源码下载
https://github.com/gamersover/tensorflow_basic_tutorial/tree/master/model_save_tutorial
相关文章推荐
- Tensorflow学习教程------模型参数和网络结构保存且载入,输入一张手写数字图片判断是几
- Tensorflow基础:模型持久化
- Tensorflow 实战google深度学习框架 09 计算图保存,与模型读取
- [TensorFlow]入门学习笔记(6)-Tensorboard简易教程和模型保存
- TensorFlow模型的保存和持久化
- 保存和读取 TensorFlow 模型
- Tensorflow学习教程------读取数据、建立网络、训练模型,小巧而完整的代码示例
- 如何保存训练好的神经网络直接进行测试-TensorFlow模型持久化
- 一个快速完整的教程,以保存和恢复Tensorflow模型。
- 10 Tensorflow模型保存与读取
- Tensorflow模型保存与读取
- TensorFlow模型文件保存和读取
- TensorFlow学习笔记--网络模型的保存和读取
- tensorflow 4——模型的保存、读取
- 一份快速完整的Tensorflow模型保存和恢复教程(译)
- TensorFlow保存读取模型
- TensorFlow基础笔记(1) 数据读取与保存
- TensorFlow变量管理、保存和读取(持久化)
- [Deep Learning] TensorFlow模型、参数的保存与读取
- 6.TensorFlow模型的保存和读取