#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Sep 19 09:42:22 2017 @author: myhaspl """ import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE=784 OUTPUT_NODE=10 LAYER1_NODE=500 BATCH_SIZE=100 LEARNING_RATE_BASE=0.8 LEARNING_RATE_DECAY=0.99 REGULARIZATION_RATE=0.0001 TRANING_STEPS=30000 MOVING_AVERAGE_DECAY=0.99 def inference(input_tensor,avg_class,weights1,biases1,weights2,biases2): if avg_class==None:#非滑动平均 layer1=tf.nn.relu(tf.matmul(input_tensor,weights1)+biases1) return tf.matmul(layer1,weights2)+biases2 else:#滑动平均 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(biases2) def train(mnist): #样本数据与样本标签 x_=tf.placeholder(tf.float32,[None,INPUT_NODE],name='x_-input') y_=tf.placeholder(tf.float32,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,stddev=0.1)) biases2=tf.Variable(tf.constant(0.1,shape=[OUTPUT_NODE])) global_step=tf.Variable(0,trainable=False) #非滑动平均 y_nohd=inference(x_,None,biases2) #滑动平均 variable_averages=tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step) #滑动平均更新变量的操作 variable_averages_op=variable_averages.apply(tf.trainable_variables()) y_hd=inference(x_,variable_averages,biases2) #交叉嫡损失函数,使用softmax归一化 cross_entropy=tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y_nohd,labels=tf.arg_max(y_,1)) cross_entropy_mean=tf.reduce_mean(cross_entropy) #加入L2正则化损失 regularizer=tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE) 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) #训练与更新参数的滑动平均值 #将2大步操作打包在train_op中,第1大步操作是使用正则化和指数衰减更新参数值 #第2大步操作是使用滑动平均再次更新参数值。 #每次训练都完成这2大步操作。 train_op=tf.group(train_step,variable_averages_op) #检验滑动平均平均模型的神经网络前向传播结果是否正确 correct_predection=tf.equal(tf.argmax(y_hd,1),tf.argmax(y_,1)) accuracy=tf.reduce_mean(tf.cast(correct_predection,tf.float32)) #开始训练过程 with tf.Session() as sess: tf.initialize_all_variables().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(TRANING_STEPS): if i%1000==0: #每1000轮计算当前训练的结果 validate_acc=sess.run(accuracy,Feed_dict=validate_Feed) print("%d次后=>正确率%g"%(i,validate_acc)) #每一轮使用的样本,然后开始训练 xs,ys=mnist.train.next_batch(BATCH_SIZE) sess.run(train_op,Feed_dict={x_:xs,y_:ys}) #TRANING_STEPS次训练结束,对测试数据进行检测,检验神经网络准确度 test_acc=sess.run(accuracy,Feed_dict=test_Feed) print("正确率:%g"%test_acc) def main(argv=None): mnist=input_data.read_data_sets("/tmp/data",one_hot=True) train(mnist) if __name__=='__main__': tf.app.run()使用了非线性激活函数relu,防止梯度消失。