保存和读取 TensorFlow 模型
2017-07-06 21:04
681 查看
保存和读取 TensorFlow 模型
训练一个模型的时间很长。但是你一旦关闭了 TensorFlow session,你所有训练的权重和偏置项都丢失了。如果你计划在之后重新使用这个模型,你需要重新训练!幸运的是,TensorFlow 可以让你通过一个叫
tf.train.Saver的类把你的进程保存下来。这个类可以把任何
tf.Variable存到你的文件系统。
保存变量
让我们通过一个简单地例子来保存 weights和
biasTensors。第一个例子你只是存两个变量,后面会教你如何把一个实际模型的所有权重保存下来。
import tensorflow as tf # The file path to save the data # 文件保存路径 save_file = './model.ckpt' # Two Tensor Variables: weights and bias # 两个 Tensor 变量:权重和偏置项 weights = tf.Variable(tf.truncated_normal([2, 3])) bias = tf.Variable(tf.truncated_normal([3])) # Class used to save and/or restore Tensor Variables # 用来存取 Tensor 变量的类 saver = tf.train.Saver() with tf.Session() as sess: # Initialize all the Variables # 初始化所有变量 sess.run(tf.global_variables_initializer()) # Show the values of weights and bias # 显示变量和权重 print('Weights:') print(sess.run(weights)) print('Bias:') print(sess.run(bias)) # Save the model # 保存模型 saver.save(sess, save_file)
Weights:
[[-0.97990924 1.03016174 0.74119264]
[-0.82581609 -0.07361362 -0.86653847]]
Bias:
[ 1.62978125 -0.37812829 0.64723819]
weights和
biasTensors
用
tf.truncated_normal()函数设定了随机值。用
tf.train.Saver.save()函数把这些值被保存在
save_file位置,命名为
"model.ckpt",(".ckpt" 扩展名表示"checkpoint")。
如果你使用 TensorFlow 0.11.0RC1 或者更新的版本,还会生成一个包含了 TensorFlow graph 的文件 "model.ckpt.meta"。
加载变量
现在这些变量已经存好了,让我们把它们加载到新模型里。# Remove the previous weights and bias # 移除之前的权重和偏置项 tf.reset_default_graph() # Two Variables: weights and bias # 两个变量:权重和偏置项 weights = tf.Variable(tf.truncated_normal([2, 3])) bias = tf.Variable(tf.truncated_normal([3])) # Class used to save and/or restore Tensor Variables # 用来存取 Tensor 变量的类 saver = tf.train.Saver() with tf.Session() as sess: # Load the weights and bias # 加载权重和偏置项 saver.restore(sess, save_file) # Show the values of weights and bias # 显示权重和偏置项 print('Weight:') print(sess.run(weights)) print('Bias:') print(sess.run(bias))
Weights:
[[-0.97990924 1.03016174 0.74119264]
[-0.82581609 -0.07361362 -0.86653847]]
Bias:
[ 1.62978125 -0.37812829 0.64723819]
注意,你依然需要在 Python 中创建
weights和
biasTensors。
tf.train.Saver.restore()函数把之前保存的数据加载到
weights和
bias当中。
因为
tf.train.Saver.restore()设定了
TensorFlow 变量,这里你不需要调用
tf.global_variables_initializer()了。
保存一个训练好的模型
让我们看看如何训练一个模型并保存它的权重。从一个模型开始:
# Remove previous Tensors and Operations # 移除之前的 Tensors 和运算 tf.reset_default_graph() from tensorflow.examples.tutorials.mnist import input_data import numpy as np learning_rate = 0.001 n_input = 784 # MNIST 数据输入 (图片尺寸: 28*28) n_classes = 10 # MNIST 总计类别 (数字 0-9) # Import MNIST data # 加载 MNIST 数据 mnist = input_data.read_data_sets('.', one_hot=True) # Features and Labels # 特征和标签 features = tf.placeholder(tf.float32, [None, n_input]) labels = tf.placeholder(tf.float32, [None, n_classes]) # Weights & bias # 权重和偏置项 weights = tf.Variable(tf.random_normal([n_input, n_classes])) bias = tf.Variable(tf.random_normal([n_classes])) # Logits - xW + b logits = tf.add(tf.matmul(features, weights), bias) # Define loss and optimizer # 定义损失函数和优化器 cost = tf.reduce_mean(\ tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\ .minimize(cost) # Calculate accuracy # 计算准确率 correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
让我们训练模型并保存权重:
import math save_file = './train_model.ckpt' batch_size = 128 n_epochs = 100 saver = tf.train.Saver() # Launch the graph # 启动图 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # Training cycle # 训练循环 for epoch in range(n_epochs): total_batch = math.ceil(mnist.train.num_examples / batch_size) # Loop over all batches # 遍历所有 batch for i in range(total_batch): batch_features, batch_labels = mnist.train.next_batch(batch_size) sess.run( optimizer, feed_dict={features: batch_features, labels: batch_labels}) # Print status for every 10 epochs # 每运行10个 epoch 打印一次状态 if epoch % 10 == 0: valid_accuracy = sess.run( accuracy, feed_dict={ features: mnist.validation.images, labels: mnist.validation.labels}) print('Epoch {:<3} - Validation Accuracy: {}'.format( epoch, valid_accuracy)) # Save the model # 保存模型 saver.save(sess, save_file) print('Trained Model Saved.')
Epoch 0 - Validation Accuracy: 0.06859999895095825
Epoch 10 - Validation Accuracy: 0.20239999890327454
Epoch 20 - Validation Accuracy: 0.36980000138282776
Epoch 30 - Validation Accuracy: 0.48820000886917114
Epoch 40 - Validation Accuracy: 0.5601999759674072
Epoch 50 - Validation Accuracy: 0.6097999811172485
Epoch 60 - Validation Accuracy: 0.6425999999046326
Epoch 70 - Validation Accuracy: 0.6733999848365784
Epoch 80 - Validation Accuracy: 0.6916000247001648
Epoch 90 - Validation Accuracy: 0.7113999724388123
Trained Model Saved.
加载训练好的模型
让我们从磁盘中加载权重和偏置项,验证测试集准确率。saver = tf.train.Saver() # Launch the graph # 加载图 with tf.Session() as sess: saver.restore(sess, save_file) test_accuracy = sess.run( accuracy, feed_dict={features: mnist.test.images, labels: mnist.test.labels}) print('Test Accuracy: {}'.format(test_accuracy))
Test Accuracy: 0.7229999899864197
就是这样!你现在知道如何保存再加载一个 TensorFlow 的训练模型了。下一章节让我们看看如何把权重和偏置项加载到修改过的模型中。
相关文章推荐
- TensorFlow学习笔记--网络模型的保存和读取
- 10 Tensorflow模型保存与读取
- sklearn、TensorFlow、keras模型保存与读取
- Tensorflow模型保存与读取
- Tensorflow小样例-Saver模型保存读取
- TensorFlow模型文件保存和读取
- tensorflow 4——模型的保存、读取
- tensorflow-模型保存与读取
- Tensorflow保存和读取模型
- [Deep Learning] TensorFlow模型、参数的保存与读取
- TensorFlow基础教程:模型持久化(模型保存与读取)
- Tensorflow 实战google深度学习框架 09 计算图保存,与模型读取
- TensorFlow保存读取模型
- 6.TensorFlow模型的保存和读取
- TensorFlow模型文件保存和读取
- tensorflow笔记:模型的保存与训练过程可视化
- tensorflow学习之Faster R-CNN模型的保存与加载
- tensorflow保存模型,加载模型,tensorboard可视化
- java读取tensorflow中图像的分类模型
- TensorFlow模型保存和加载方法