(一)MNIST数字识别代码:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#常数定义
INPUT_NODE=784
OUTPUT_NODE=10
LAYER1_NODE=500#隐藏层节点数
BATCH_SIZE=100#一个batch中训练数据个数
LEARNING_RATE_BASE=0.8#基础学习率
LEARNING_RATE_DECAY=0.99#学习率的衰减率
REGULARIZATION_RATE=0.0001#描述模型复杂度的正则化项在损失函数中的系数
TRAINING_STEPS=30000
MOVING_AVERAGR_DECAY=0.99#滑动平均衰减率
#计算神经网络的前向传播结果
def infernce(input_tensor,avg_class,weights1,biases1,weights2,biase2):
if avg_class==None:#如果没有提供滑动平均类
layer1=tf.nn.relu(tf.matmul(input_tensor,weights1)+biases1)
return tf.matmul(layer1,weights2)+biase2
else:#使用avg_cla ss.average()函数计算变量的滑动平均值
layer1=tf.nn.relu(tf.matmul(input_tensor,avg_class.average(weights1))+avg_class.average(biases1))
return tf.matmul(layer1,avg_class.average(weights2))+avg_class.average(biase2)
#训练模型
def train(mnist):
#预先声明变量
x = tf.placeholder(tf.float32,[None,INPUT_NODE],name='x-input')
y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input')
weights1=tf.Variable(tf.truncated_normal([INPUT_NODE,LAYER1_NODE],stddev=0.1))
biases1 = tf.Variable(tf.constant(0.1,shape=[LAYER1_NODE]))
weights2=tf.Variable(tf.truncated_normal([LAYER1_NODE,OUTPUT_NODE],stddev=0.1))
biases2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE]))
y=infernce(x,None,weights1,biases1,weights2,biases2)#计算滑动平均的类为NONE时,滑动平均值
global_step = tf.Variable(0,trainable=False)
variable_averages=tf.train.ExponentialMovingAverage(MOVING_AVERAGR_DECAY,global_step)#初始化滑动平均类
variable_averages_op=variable_averages.apply(tf.trainable_variables())#在神经网络的所有参数上使用滑动平均
average_y=infernce(x,variable_averages,weights1,biases1,weights2,biases2)#计算使用了滑动平均之后的前向传播结果
#正则化损失函数
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)
regularizer=tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)#声明L2正则化损失函数,函数哦!!!
regularization=regularizer(weights1)+regularizer(weights2)#计算权重的正则化损失
loss=cross_entropy_mean+regularization
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)
#一个新函数tf.control_dependencies一次实现多个操作,反向传播更新参数,同时更新每一个参数的滑动平均值!!
with tf.control_dependencies([train_step,variable_averages_op]):
train_op=tf.no_op(name='train')
#检验前向传播结果的正确率
correct_prediction=tf.equal(tf.argmax(average_y,1),tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))#将布尔型数据转化为实数型,然后计算平均值
with tf.Session() as sess:
tf.global_variables_initializer().run()
validate_feed={x:mnist.validation.images,
y_:mnist.validation.labels}
test_feed={x:mnist.test.images,
y_:mnist.test.labels}
for i in range(TRAINING_STEPS):
xs,ys=mnist.train.next_batch(BATCH_SIZE) #产生batch的训练数据并运行
sess.run(train_op,feed_dict={x:xs,y_:ys})
if i%1000==0:
validate_acc=sess.run(accuracy,feed_dict=validate_feed)
print(i,validate_acc)
test_acc=sess.run(accuracy,feed_dict=test_feed)
print(TRAINING_STEPS,test_acc)
#主程序入口
def main(argv=None):
mnist=input_data.read_data_sets("C:/Users/Administrator/Documents/tensorflow/mnist",one_hot=True)
train(mnist)
#tf.app.run()函数调用上面的main函数
if __name__=='__main__':
tf.app.run()
难点:滑动平均模型到底是个啥?
(二)模型持久化
对以上代码进行改进,目标:
1、训练过程中每隔一段时间保存一次模型,以防程序死机;
2、将训练和测试分成两个独立的程序,将前向传播过程抽象成一个单独的库函数,供训练和测试模块的使用。
mnist_inference.py
# -* coding: utf-8 -*-
import tensorflow as tf
INPUT_NODE=784
OUTPUT_NODE=10
LAYER1_NODE=500
def get_weight_Variable(shape,regularizer):
weights=tf.get_variable("weights",shape,initializer=tf.truncated_normal_initializer(stddev=0.1))#训练时创建,测试时加载
if regularizer!=None:
tf.add_to_collection('losses',regularizer(weights))
return weights
def inference(input_tensor,regularizer):
with tf.variable_scope('layer1'):
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
# -* coding: utf-8 -*-
import tensorflow as tf
import os
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference
#配置神经网络参数
BATCH_SIZE=100
LEARNING_RATE_BASE=0.8
LEARNING_RATE_DECAY=0.99
REGULARIZATION_RATE=0.0001
TRAINING_STEPS=30000
MOVING_AVERAGR_DECAY=0.99
MODEL_SAVE_PATH="C:/Users/Administrator/Documents/tensorflow/mnist/"
MODEL_NAME="MODEL.ckpt"
def train(mnist):
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)
y=mnist_inference.inference(x,regularizer)#直接使用mnnst_inference.py中定义的前向传播过程
global_step=tf.Variable(0,trainable=False)
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGR_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_vale,step=sess.run([train_op,loss,global_step],feed_dict={x:xs,y_:ys})
if i%1000==0:
print(step,loss_vale)
saver.save(sess,os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step=global_step)#每1000轮保存一次训练好的模型
def main(argv=None):
mnist=input_data.read_data_sets("C:/Users/Administrator/Documents/tensorflow/mnist",one_hot=True)
train(mnist)
if __name__=='__main__':
tf.app.run()
mnist_eval.py
# -* coding: utf-8 -*-
import time
import tensorflow as tf
import os
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference
import mnist_train#加载以上两个定义的常量和函数
EVAL_INTERVAL_SECS = 10#每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_output')
validate_feed = {x:mnist.validation.images,y_:mnist.validation.labels}
y=mnist_inference.inference(x,None)#正则化损失函数设置为None
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))#argmax()得到未知样例的类别
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
variable_averages=tf.train.ExponentialMovingAverage(mnist_train.MOVING_AVERAGR_DECAY)#得到变量均值
variable_to_store=variable_averages.variables_to_restore()#加载模型
saver=tf.train.Saver(variable_to_store)
while True:
with tf.Session() as sess:
ckpt=tf.train.get_checkpoint_state(mnist_train.MODEL_SAVE_PATH)#此函数通过checkpoint文件自动找到目录中最新模型的文件名
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(global_step,accuracy_score)
else:
print('No checkpoint file found')
time.sleep(EVAL_INTERVAL_SECS)
def main(argv=None):
mnist=input_data.read_data_sets("C:/Users/Administrator/Documents/tensorflow/mnist",one_hot=True)
evaluate(mnist)
if __name__=='__main__':
tf.app.run()