1.加入了两个隐层
2.学习率衰减
3.加入反向传播
4.dropout防止过拟合
准确率0.98
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)#载入数据集
batch_size = 100#每个批次的大小
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)#dropout参数
learning_rate = tf.Variable(0.001, dtype=tf.float32)#优化器的学习率
#创建一个神经网络
w1 = tf.Variable(tf.truncated_normal([784, 500], stddev=0.1))#权值矩阵
b1 = tf.Variable(tf.zeros([500]) + 0.1)#偏置值
l1 = tf.nn.tanh(tf.matmul(x, w1) + b1)
l1_dropout = tf.nn.dropout(l1, keep_prob)#设置dropout,防止过拟合
w2 = tf.Variable(tf.truncated_normal([500, 300], stddev=0.1))#权值矩阵
b2 = tf.Variable(tf.zeros([300]) + 0.1)#偏置值
l2 = tf.nn.tanh(tf.matmul(l1_dropout, w2) + b2)
l2_dropout = tf.nn.dropout(l2, keep_prob)#设置dropout,防止过拟合
w3 = tf.Variable(tf.truncated_normal([300, 10], stddev=0.1))#权值矩阵
b3 = tf.Variable(tf.zeros([10]) + 0.1)#偏置值
prediction = tf.nn.softmax(tf.matmul(l2_dropout, w3) + b3)#使用softmax进行预测
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))#损失函数用交叉熵
train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss)#优化器
train_op = tf.group(train_step)#反向传播神经网络
init = tf.initialize_all_variables()#初始化变量
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1))#返回布尔类型的列表
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))#计算准确率
with tf.Session() as sess:
sess.run(init)
for epoch in range(50):#训练50个周期
sess.run(tf.assign(learning_rate, 0.001 * (0.95 ** epoch)))#学习率递减
for batch in range(n_batch):
batch_x, batch_y = mnist.train.next_batch(batch_size)
sess.run(train_op, feed_dict={x:batch_x, y:batch_y, keep_prob:0.8})#设置dropout,进行训练
test_acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels, keep_prob:1.0})#使用测试集进行评测准确率
print('Iter',epoch,'test_Accuracy:',test_acc)