5.1 MNIST数据集
MNIST数据集:提供6w张28*28像素点的0~9手写数字图片和标签,用于训练。
? ? ? ? ? ? ? ? ? ? ? ? ?提供1w张28*28像素点的0~9手写数字图片和标签,用于测试。
? ? ? ? ? ? ? ? ? ? ? ? ?黑底白字,黑底用0表示,白字用0~1之间浮点数表示,越接近1 颜色越白。
每张图片的784个像素点(28*28)组成长度为784的一维数组,作为输入特征。
eg: [0.,? 0. ,? ? ? .......? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0.234,? 0.,??0. , 0.]?
图片的标签以一维数组形式给粗,每个元素表示对应分类出现的概率。
eg:[0., 0.,?0., 0.,?0., 0., 1.,0., 0.,?0., ]? ? ? ? ? ? ?表示 图片为数字6的概率为1,为其他数字的概率为0
导入数据集
form tensorflow.examples.tutorials.mnist import input_data
#在data目录下读入数据集合,如果没有,则自动导入数据集
mnist = input_data.read_data_sets('./data/', one_hot=True)
数据集常用操作
# 显示 训练集 校验集 测试集 大小
print "train data size :", mnist.train.num_examples
print "validation data size :", mnist.validation.num_examples
print "test data size :", mnist.test.num_examples
# 显示结果
train data size : 55000
validation data size : 5000
test data size : 10000
# 返回标签和数据
mnist.train.labels[0] # 返回第0张图的标签 是数字几的概率
mnist.train.images[0] # 返回784个像素点
#取一小撮数据,准备喂入神经网络训练
BATCH_SIZE = 200 # 定义一小撮是多少
xs, ys = mnist.train.next_batch(BATCH_SIZE)
print "xs shape:", xs.shape
print "ys shape:", ys.shape
# 显示结果
xs shape: (200, 784)
ys shape: (200,10)
常用函数
tf.get_collection("") | 从集合中取出全部变量, 生成一个列表 | tf.add_n([ ]) | 把列表内对应元素相加 | tf.cast(x, dtype) | 把x转为dtype | tf.argmax(x, axis) | 返回最大值所作的索引号,如:tf.argmax([1, 0, 0],1) 返回0 | os.path.join("home", "name") | 将字符串按路径的命名规则拼接,返回home/name | 字符串.split() | 按指定拆分符对字符串切片, 返回分割后的列表。 如:’./model/mnist_model-1000'.split('/')[-1].split('-')[-1]? ? (要是看不懂,自己用python试一下就明白了,[-1]是split()返回列表中最后一项的意思) | with tf.Graph().as_default() as g: | 其定义的节点在计算图g中,一般用这种方法复现已经定义好的神经网络 |
保存模型
# 保存模型
saver = tf.train.Saver()
tf.Session() as sess:
for i in range(STEPS):
if i%轮数 == 0:
# 拼接成 ./MODEL_SAVE_PATN/MODEL_NAME-global_step MODEL_SAVE_PATN 为字符串常量, MODEL_NAME同
# global体现在哪里?
saver.save(sess, os.path.jion(MODEL_SAVE_PATN, MODEL_NAME), global_step = global_step)
# 加载模型
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(存储路径)
if ckpt and ckpt.modal_checkpoint_path :
saver.restore(sess, ckpt.modal_checkpoint_path)
# 实例化可还原滑动平均值的saver
# ema 相关内容可以复习第4节的内容
ema = tf.train.ExponentialMovingAverage(滑动平均基数)
ema_restore = ema.variables_to_restore()
saver = tf.train.Saver(ema_restore)
# 准确率计算方法
correct_prediction = tf.equal(tf.argmax(y, 1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
5.2 模块化搭建神经网络八股
这部分是上一节内容的复习
# forward.py 伪代码
def forward(x, regularizer):
w =
b =
y =
return y
def get_weight(shape, regularizer):
def get_bias(shape):
----------------------------------------------------------------------------------
# backward.py 伪代码
def backward(mnist):
x = tf.placeholder
y_ =
y=
global_step =
loss =
< 正则化, 指数衰减学习率,滑动平均>
train_step =
实例化 saver
with tf.Session() as sess:
初始化op
for i in range(STEPS):
sess.run(train_step, feed_dict = {x: ,y_:})
if i%轮数 ==0:
print
saver.save()
----------------------------------------------------------------------------------
# 损失函数loss 含正则化regularization
# backwoard.py 中加入 正则化
# 交叉熵和正则化一起使用
ce = tf.nn.sparse_soft_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
cem = tf.reduce_mean(ce)
loss = cem + tf.add_n(tf.get_collection("losses"))
# forward.py 中加入
if regularizer != None:tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w))
----------------------------------------------------------------------------------
# 学习率 learning_rate
# backward.py中加入
learning_rate = tf.train.exponential_decay(
LERANING_RATE_BASE,
global_step,
LEARNING_RATE_STEP,
LEARNING_RATE_DECAY,
staircase=True) # staircase 楼梯
----------------------------------------------------------------------------------
# 滑动平均 ema
# backward.py 中加入
ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECY,global_step)
ema_op = ema.apple(tf.trainable_variables())
with tf.control_dependencies([train_step, ema_op]):
train_op = tf.no_op(name='train')
----------------------------------------------------------------------------------
# test.py 伪代码
def test(mnist):
with tf.Graph().as_default as g:
定义x, y_,y
实例化课还原滑动平均值的saver
计算争取率
while True:
with tf.Session() as sess:
加载ckpt模块 ckpt = tf.train.get_checkpoint_state(存储路径)
如果已有ckpt模型则恢复 if ckpt and ckpt.moddel_checkpoint_path:
恢复会话 saver.restore(sess, ckpt.moddel_checkpoint_path)
恢复轮数 global_step = ckpt.moddel_checkpoint_pathsplit('/')[-1].split('-')[-1]
计算准确率 accuracy_score = sess.run(accuary, feed_dict={x:mnist.test.images, y_::mnist.test.labels})
打印提示 print global_step,accuracy_score
如果没有模型 else:
给出提示 print 'No checkpiont file found'
return
def main():
mnist = input_data.read_data_sets('./data/', one_hot = True)
test(mnist)
if __name__ == '__main__':
main()
5.3 手写数字识别准确率输出
直接上课程中的代码,代码参考(https://github.com/upseaup/Tensorflow/tree/master/Tensorflow%205%20fc1)
import tensorflow as tf
INPUT_NODE = 784
OUTPUT_NODE = 10
LAYER1_NODE = 500
def get_weight(shape, regularizer):
w = tf.Variable(tf.truncated_normal(shape,stddev=0.1))
# 判断是否使用正则化
if regularizer != None :tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w))
return w
def get_bias(shape):
b = tf.Variable(tf.zeros(shape))
return b
def forward(x, regularizer):
w1 = get_weight([INPUT_NODE, LAYER1_NODE], regularizer)
b1 = get_bias([LAYER1_NODE])
# 输出过激活函数
y1 = tf.nn.relu(tf.matmul(x, w1) + b1)
w2 = get_weight ([LAYER1_NODE, OUTPUT_NODE],regularizer)
b2 = get_bias([OUTPUT_NODE])
# 输出不过激活函数,并且不需要softmax
y = tf.matmul(y1,w2)+b2
return y
###not use softmax how to mut_classity???? p97
前向传播代码, 这段代码都比较熟悉了,需要注意的是,代码中forward函数y1过激活函数,y却没有过激活函数,而且数字识别是一个多分类问题,代码中竟然没有使用softmax函数。这是因为我们在求损失函数时,使用的是sparse_softmax_cross_entropy_with_logits()函数。根据tf官网(https://tensorflow.google.cn/api_docs/python/tf/nn/sparse_softmax_cross_entropy_with_logits?hl=en)给出的解释:
Warning:?This op expects unscaled logits, since it performs a?softmax ?on?logits ?internally for efficiency. Do not call this op with the output of?softmax , as it will produce incorrect results.
反向传播代码
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_forward
import os
BATCH_SIZE = 200
LEARNING_RATE_BASE = 0.1
LEARNING_RATE_DECAY = 0.99
REGULARIZER = 0.0001
STEPS = 50000
MOVING_AVERAGE_DECAY = 0.99
MODEL_SAVE_PATH = ".model/"
MODEL_NAME = './mnist_model'
def backward(mnist):
x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
y_ = tf.placeholder(tf.float32, [None,mnist_forward.OUTPUT_NODE])
y = mnist_forward.forward(x, REGULARIZER)
global_step = tf.Variable(0, trainable=False)
# 以下两句可以使用 cem = tf.losses.sparse_softmax_cross_entropy(logits=y,
# onehot_labels=y_)
# 替代,根据tf官网, tf.losses.sparse_softmax_cross_entropy是用
# tf.nn.sparse_softmax_cross_entropy_with_logits 实现的
ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_,1))
cem= tf.reduce_mean(ce)
loss = cem + 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,
staircase = True)
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step = global_step)
# 这里将前向传播网络的参数w b 进行滑动平均
ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
ema_op = ema.apply(tf.trainable_variables())
# 将两个操作合为一个操作, 具体可参考《TensorFlow实战Google学习框架》
# 下面两行 和 train_op = tf.group(train_step, ema_op)是等价的
with tf.control_dependencies([train_step, ema_op]):
train_op = tf.no_op(name = 'train')
saver = tf.train.Saver()
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
for i in range(STEPS):
xs, ys = mnist.train.next_batch(BATCH_SIZE)
# sess.run arg ???
_, loss_value, step = sess.run([train_op, loss, global_step,], feed_dict = {x:xs, y_: ys})
if i % 1000 == 0 :
print 'After %d train steps, loss on train batch is %g. ' %(step, loss_value)
# lobal_step = global_step 将global_step 写入文件名中
saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step = global_step)
def main():
mnist = input_data.read_data_sets('./data/', one_hot = True)
backward(mnist)
if __name__ == "__main__":
main()
根据《TensorFlow实战Google学习框架》p97? inference 函数 中关于滑动平均的例子说明,本文中应该是没有成功调用滑动平均。欢迎各位大佬讨论本例程是否成功配置滑动平均。
测试程序:
#coding:utf-8
import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_forward
import mnist_backward
TEST_INTERVAL_SECS = 5
def test(mnist):
with tf.Graph().as_default() as g:
x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
y = mnist_forward.forward(x, None)
ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
# variables_to_restore()
ema_restore = ema.variables_to_restore()
# tf.train.Saver类可以参考 《TensorFlow实战Google学习框架》p111部分内容
# 以及tf官网部分内容
saver = tf.train.Saver(ema_restore)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
while True :
with tf.Session() as sess:
ckpt =tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess,ckpt.model_checkpoint_path)
# 通过存储文件名恢复 global_step
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
accuracy_score = sess.run(accuracy, feed_dict={x:mnist.test.images, y_: mnist.test.labels})
print 'after %s training step(s), test accuracy = %g' %(global_step, accuracy_score)
else:
print 'No checkpiont flie found'
time.sleep(TEST_INTERVAL_SECS)
def main():
mnist = input_data.read_data_sets('./data', one_hot =True)
test(mnist)
if __name__ == '__main__':
main()
? tf.train.Saver类可以参考 《TensorFlow实战Google学习框架》p111部分内容?,以及tf官网部分内容
(https://tensorflow.google.cn/versions/r1.15/api_docs/python/tf/train/Saver?hl=en)
|