import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#参数设置
input_nodes = 784 #输入节点数
output_nodes = 10 #输出节点数
layer1_nodes = 500 #隐层节点数
bitch_size = 100 #每次训练包含的数据个数
learning_rate = 0.8 #初始学习率
learning_rate_deacy = 0.99 #学习率衰减率
l2_regulation = 0.0001 #l2正则化系数
moving_rate_deacy = 0.99 #滑动模型那个衰减率
train_num = 10000
#前向传播,variable_average平均滑动模型参数
def inference(x,variable_average,w1,b1,w2,b2):
if variable_average == None:
layer1 = tf.nn.relu(tf.matmul(x,w1)+b1)
return tf.matmul(layer1,w2)+b2
else:
layer1 = tf.nn.relu(tf.matmul(x,variable_average.average(w1))+variable_average.average(b1))
return tf.matmul(layer1,variable_average.average(w2))+variable_average.average(b2)
def train(mnist):
#features and labels
x = tf.placeholder(tf.float32,[None,784])
y_ = tf.placeholder(tf.float32,[None,10])
#参数初始化
w1 = tf.Variable(tf.truncated_normal([input_nodes,layer1_nodes],stddev=0.1))
b1 = tf.Variable(tf.constant(0.1,shape=[layer1_nodes]))
w2 = tf.Variable(tf.truncated_normal([layer1_nodes,output_nodes],stddev=0.1))
b2 = tf.Variable(tf.constant(0.1,shape=[output_nodes]))
#不使用平均化滑动模型的前向传播结果
y = inference(x,None,w1,b1,w2,b2)
#平均滑动模型
global_step = tf.Variable(0,trainable=False)
#定义一个平均滑动模型的类
variable_average = tf.train.ExponentialMovingAverage(0.99,global_step)
#定义一个平均华东模型操作,应用给所有可训练变量
variable_average_op = variable_average.apply(tf.trainable_variables())
#使用平均化滑动模型的前向传播结果
average_y = inference(x,variable_average,w1,b1,w2,b2)
#交叉熵损失函数
cost_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,labels=tf.arg_max(y_,1))
cost_entropy_mean = tf.reduce_mean(cost_entropy)
#l2正则化
regulations = tf.contrib.layers.l2_regularizer(0.0001)
l2_regulation = regulations(w1)+regulations(w2)
#带有正则化的损失函数作为最终的损失函数
loss = cost_entropy_mean+l2_regulation
#学习率衰减
learning_rate_deacy = tf.train.exponential_decay(learning_rate=0.8,global_step=global_step,decay_steps=100,
decay_rate=0.99)
#训练
train_step = tf.train.GradientDescentOptimizer(learning_rate_deacy).minimize(loss,global_step=global_step)
#tf.group函数保证再一次迭代中,参数的train和参数的平均滑动都被执行
train_op = tf.group(train_step,variable_average_op)
#准确率
correct_predict = tf.equal(tf.argmax(average_y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_predict,tf.float32))
#定义一个初始化的操作
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
init_op.run()
validation_feed = {x:mnist.validation.images,y_:mnist.validation.labels} #验证数据
test_feed = {x:mnist.test.images,y_:mnist.test.labels} #测试数据
for i in range(train_num):
if i%1000 == 0:
validation_acc = sess.run(accuracy,feed_dict=validation_feed)
print('训练%d次验证集准确率是%g'%(i+1,validation_acc))
#训练数据
x_data,y_data = mnist.train.next_batch(bitch_size)
train_feed = {x:x_data,y_:y_data}
sess.run(train_op,feed_dict=train_feed)
#测试精度
test_acc = sess.run(accuracy,feed_dict=test_feed)
print('测试精度是%g'%test_acc)
mnist = input_data.read_data_sets('data',one_hot=True)
train(mnist)