在上一篇《TensorFlow入门之MNIST样例代码分析》中,我们讲解了如果来用一个三层全连接网络实现手写数字识别。但是在实际运用中我们需要更有效率,更加灵活的代码。在TensorFlow实战这本书中给出了更好的实现,他将程序分为三个模块,分别是前向传播过程模块,训练模块和验证检测模块。并且在这个版本中添加了模型持久化功能,我们可以将模型保存下来,方便之后的模型检验,并且我们可以一边训练新的模型,一边来检验模型,代码更加的灵活高效。

前向传播模块

首先将前向传播过程抽象出来,作为一个可以作为训练测试共享的模块,取名为mnist_inference.py,将这个过程抽象出来的好处是,一是可以保证在训练或者测试的过程中前向传播的一致性,提高代码的复用性。还有一点是我们可以更好地将其与滑动平均模型与模型持久化功能结合,更加灵活的来检验新的模型。mnist_inference.py代码如下:

# -*- coding: utf-8 -*-
import tensorflow as tf # 定义神经网络结构相关的参数
INPUT_NODE = 784
OUTPUT_NODE = 10
LAYER1_NODE = 500 # 通过tf.get_variable函数来获取变量。在训练神经网络时会创建这些变量;在测试时会通
# 过保存的模型加载这些变量的取值。而且更加方便的是,因为可以在变量加载时将滑动平均变
# 量重命名,所以可以直接通过相同的名字在训练时使用变量自身,而在测试时使用变量的滑动
# 平均值。在这个函数中也会将变量的正则化损失加入到损失集合。
def get_weight_variable(shape, regularizer):
weights = tf.get_variable(
"weights", shape,
initializer=tf.truncated_normal_initializer(stddev=0.1)
)
# 当给出了正则化生成函数时,将当前变量的正则化损失加入名字为losses的集合。在这里
# 使用了add_to_collection函数将一个张量加入一个集合,而这个集合的名称为losses。
# 这是自定义的集合,不在TensorFlow自动管理的集合列表中。
if regularizer != None:
tf.add_to_collection('losses', regularizer(weights))
return weights # 定义神经网络的前向传播过程
def inference(input_tensor, regularizer):
# 声明第一层神经网络的变量并完成前向传播过程。
with tf.variable_scope('layer1'):
# 这里通过tf.get_variable或者tf.Variable没有本质区别,因为在训练或者测试
# 中没有在同一个程序中多次调用这个函数。如果在同一个程序中多次调用,在第一次
# 调用之后需要将reuse参数设置为True。
weights = get_weight_variable(
[INPUT_NODE, LAYER1_NODE], regularizer
)
biases = tf.get_variable(
"biases", [LAYER1_NODE],
initializer=tf.constant_initializer(0.0)
)
layer1 = tf.nn.relu(tf.matmul(input_tensor, weights)+biases) # 类似的声明第二层神经网络的变量并完成前向传播过程。
with tf.variable_scope('layer2'):
weights = get_weight_variable(
[LAYER1_NODE, OUTPUT_NODE], regularizer
)
biases = tf.get_variable(
"biases", [OUTPUT_NODE],
initializer=tf.constant_initializer(0.0)
)
layer2 = tf.matmul(layer1, weights) + biases # 返回最后前向传播的结果
return layer2

训练模块

将训练模型的模块提取出来,训练模块命名为mnist_train.py,在下面的代码中每过1000个step我们就保存一次模型。代码如下:

# -*- coding: utf-8 -*-
import os import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data # 加载mnist_inference.py中定义的常量和前向传播的函数。
import mnist_inference # 配置神经网络的参数。
BATCH_SIZE = 100
LEARNING_RATE_BASE = 0.8
LEARNING_RATE_DECAY = 0.99
REGULARIZATION_RATE = 0.0001
TRAINING_STEPS = 30000
MOVING_AVERAGE_DECAY = 0.99 # 模型保存的路径和文件名
MODEL_SAVE_PATH = "./model/"
MODEL_NAME = "model.ckpt" def train(mnist):
# 定义输入输出placeholder。
x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input')
y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input') regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
# 直接使用mnist_inference.py中定义的前向传播过程
y = mnist_inference.inference(x, regularizer)
global_step = tf.Variable(0, trainable=False) # 定义损失函数、学习率、滑动平均操作以及训练过程
variable_averages = tf.train.ExponentialMovingAverage(
MOVING_AVERAGE_DECAY, global_step
)
variable_averages_op = variable_averages.apply(
tf.trainable_variables()
)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=y, labels=tf.argmax(y_, 1)
)
cross_entropy_mean = tf.reduce_mean(cross_entropy)
loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses'))
learning_rate = tf.train.exponential_decay(
LEARNING_RATE_BASE,
global_step,
mnist.train.num_examples / BATCH_SIZE,
LEARNING_RATE_DECAY
)
train_step = tf.train.GradientDescentOptimizer(learning_rate)\
.minimize(loss, global_step=global_step)
with tf.control_dependencies([train_step, variable_averages_op]):
train_op = tf.no_op(name='train') # 初始化TensorFlow持久化类
saver = tf.train.Saver()
with tf.Session() as sess:
tf.global_variables_initializer().run() # 在训练过程中不再测试模型在验证数据上的表现,验证和测试的过程将会有一个独
# 立的程序来完成。
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})
# 每1000轮保存一次模型
if i % 1000 == 0:
# 输出当前的训练情况。这里只输出了模型在当前训练batch上的损失
# 函数大小。通过损失函数的大小可以大概了解训练的情况。在验证数
# 据集上正确率的信息会有一个单独的程序来生成
print("After %d training step(s), loss on training "
"batch is %g." % (step, loss_value))
# 保存当前的模型。注意这里给出了global_step参数,这样可以让每个
# 被保存的模型的文件名末尾加上训练的轮数,比如“model.ckpt-1000”,
# 表示训练1000轮之后得到的模型。
saver.save(
sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME),
global_step=global_step
) def main(argv=None):
mnist = input_data.read_data_sets("./data", one_hot=True)
train(mnist) if __name__ == "__main__":
tf.app.run()

验证与测试模块

验证模块与测试模块可以对保存好的训练模型进行验证与测试,在下面的代码中我们选择每过10秒钟验证一个最新的模型。这样做的好处是可以将训练与验证或者测试分割开来,同时进行。该模块命名为mnist_eval.py

# -*- coding: utf-8 -*-
import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data # 加载mnist_inference.py 和mnist_train.py中定义的常量和函数。
import mnist_inference
import mnist_train # 每10秒加载一次最新的模型,并且在测试数据上测试最新模型的正确率
EVAL_INTERVAL_SECS = 10 def evaluate(mnist):
with tf.Graph().as_default() as g:
# 定义输入输出的格式。
x = tf.placeholder(
tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input'
)
y_ = tf.placeholder(
tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input'
)
validate_feed = {x: mnist.validation.images,
y_: mnist.validation.labels} # 直接通过调用封装好的函数来计算前向传播的结果。因为测试时不关注ze正则化损失的值
# 所以这里用于计算正则化损失的函数被设置为None。
y = mnist_inference.inference(x, None) # 使用前向传播的结果计算正确率。如果需要对未知的样例进行分类,那么使用
# tf.argmax(y,1)就可以得到输入样例的预测类别了。
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 通过变量重命名的方式来加载模型,这样在前向传播的过程中就不需要调用求滑动平均
# 的函数来获取平均值了。这样就可以完全共用mnist_inference.py中定义的
# 前向传播过程。
variable_averages = tf.train.ExponentialMovingAverage(
mnist_train.MOVING_AVERAGE_DECAY
)
variables_to_restore = variable_averages.variables_to_restore()
saver = tf.train.Saver(variables_to_restore) # 每隔EVAL_INTERVAL_SECS秒调用一次计算正确率的过程以检验训练过程中正确率的
# 变化。
while True:
with tf.Session() as sess:
# tf.train.get_checkpoint_state函数会通过checkpoint文件自动
# 找到目录中最新模型的文件名。
ckpt = tf.train.get_checkpoint_state(
mnist_train.MODEL_SAVE_PATH
)
if ckpt and ckpt.model_checkpoint_path:
# 加载模型。
saver.restore(sess, ckpt.model_checkpoint_path)
# 通过文件名得到模型保存时迭代的轮数。
global_step = ckpt.model_checkpoint_path\
.split('/')[-1].split('-')[-1]
accuracy_score = sess.run(accuracy,
feed_dict=validate_feed)
print("After %s training step(s), validation "
"accuracy = %g" % (global_step, accuracy_score))
else:
print("No checkpoint file found")
return
time.sleep(EVAL_INTERVAL_SECS) def main(argv=None):
mnist = input_data.read_data_sets("./data", one_hot=True)
evaluate(mnist) if __name__ == "__main__":
tf.app.run()

总结

这个样例是一个非常好的可以用来理解TensorFlow的程序,特别是TensorFlow的计算图的理解,还有模型持久化与恢复,变量的管理,滑动平均模型的实现等等。还有这种灵活的模块分块的思想也值得学习。

最新文章

  1. .Net客户端监听ZooKeeper节点数据变化
  2. C#如何防止程序多次运行的技巧
  3. http的一些事
  4. 图片延迟加载(用jq自己写的方法)
  5. 黑马程序员_Java基础:多功能小窗口,swing,io,net综合应用
  6. genome MuSic安装
  7. 回车键Enter
  8. 【ZZ】快速學會開發Android App
  9. iOS 8安装教程图解
  10. autoscan; aclocal; autoconf; automake --add-missing; ./configure; make
  11. Ajax中解析Json的两种方法详解
  12. [转载]MongoDB学习 (五):查询操作符(Query Operators).1st
  13. Linux下查看所有用户(shell脚本获取)
  14. leetcode LRU Cache python
  15. java多线程基本概述(十三)——Executor
  16. ABP官方文档翻译 2.6 定时
  17. 微信小游戏爆款秘笈 数据库MongoDB攻略篇
  18. 控制台程序(C#)不弹出认证窗口连接到Dynamics CRM Online的Web API
  19. 阿里面试题BIO和NIO数量问题附答案和代码
  20. Linux目录结构 重要目录结构详细

热门文章

  1. git push失败
  2. Spring入门学习笔记(3)——事件处理类
  3. [文章存档]Kudu 的 Debug Console 窗口如何查看更多文件
  4. [Hanani]高数相关知识记录
  5. IPC_Binder_java_2
  6. 君学,佳一tvodp文件破解
  7. Redis学习(一):CentOS下redis安装和部署
  8. hadoop在章鱼大数据平台下的安装与配置
  9. No.101_第二次团队会议
  10. Alpha版会议总结