Tensorflow——MNIST手写数字数据集识别分类,准确率达到98%以上的方法实验

1、网络设计 

  • 首先我们设计一个具有两层隐藏层的神经网络作为训练MNIST数据集的网络;
  • 将学习率设置为变量(每迭代一次,按公式减小学习率,目的为了使得收敛速度更快);
  • 将Dropout算法引入,但是并不使用它(keep_prob设置为1.0),只为了说明此项也是可以更改的,对准确率都一定的影响;
  • 使用交叉熵(cross-entropy)代价函数计算loss
  • 使用Adam优化器,对loss进行操作,使得loss最小

2、网络的实现

     网络如下:

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

#定义两个占位符(placeholder)
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#再定义一个placeholder,后面设置Dropout参数
keep_prob = tf.placeholder(tf.float32)
#定义学习率变量
lr = tf.Variable(0.001,dtype= tf.float32)

#创建一个简单的神经网络
#第一个隐藏层
#权重
W1 = tf.Variable(tf.truncated_normal([784,500],stddev = 0.1))  #截断的正态分布中输出随机值,标准差:stddev
#偏置值
b1 = tf.Variable(tf.zeros([500]) + 0.1)
#使用激活函数,获得信号输出,即此层神经元的输出
L1 = tf.nn.tanh(tf.matmul(x, W1) + b1)
#调用tensorflow封装好的dropout函数,keep_prob参数是设置有多少的神经元是工作的,在训练时,通过feed操作将确切值传入
L1_drop = tf.nn.dropout(L1, keep_prob)

#第二个隐藏层:2000个神经元
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_drop, W2) + b2)
L2_drop = tf.nn.dropout(L2, keep_prob)

#最后一层输出层:10个神经元
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_drop,W3) + b3)


#交叉熵代价函数(cross-entropy)的使用
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
#使用Adam优化器方法
train_step = tf.train.AdamOptimizer(lr).minimize(loss)
#初始化变量
init = tf.global_variables_initializer()


#结果存放在一个布尔型列表中
#equal中的两个值,若是一样,则返回True,否则返回False。argmax函数:返回最大值所在的索引值,即位置
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#求准确率
#将上一步的布尔类型转化为32位浮点型,即True转换为1.0,False转换为0.0,然后计算这些值的平均值作为准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

#定义会话
with tf.Session() as sess:
    #初始化变量
    sess.run(init)
    #迭代21个周期
    for epoch in range(51):
        #每迭代一个周期,重新给学习率赋值,目的:在后期收敛时,防止学习率过大,因此降低学习率,使得loss达到最小
        sess.run(tf.assign(lr,0.001*(0.95**epoch)))
        #n_batch:之前定义的批次
        for batch in range(n_batch):
            #获得100张图片,图片的数据保存在batch_xs中,图片的标签保存在batch_ys中
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            #使用Feed操作,此步执行训练操作的op,将数据喂给他,keep_prob设置为1.0就相当于Dropout没有起作用
            sess.run(train_step,feed_dict = {x:batch_xs,y:batch_ys,keep_prob:1.0})
            
        learning_rate = sess.run(lr)
        #训练一个周期后就可以看下准确率,使用Feed方法,此步执行计算准确度的op操作,将其对应的参数喂给它
        test_acc = sess.run(accuracy,feed_dict = {x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
        print("Iter " + str(epoch) + ",Testing Accuracy " + str(test_acc) + ", Learning Rate= " + str(learning_rate))

 

3、测试结果

      输出结果:

Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
Iter 0,Testing Accuracy 0.9499, Learning Rate= 0.001
Iter 1,Testing Accuracy 0.9645, Learning Rate= 0.00095
Iter 2,Testing Accuracy 0.968, Learning Rate= 0.0009025
Iter 3,Testing Accuracy 0.9708, Learning Rate= 0.000857375
Iter 4,Testing Accuracy 0.9747, Learning Rate= 0.00081450626
Iter 5,Testing Accuracy 0.9761, Learning Rate= 0.0007737809
Iter 6,Testing Accuracy 0.9746, Learning Rate= 0.0007350919
Iter 7,Testing Accuracy 0.9791, Learning Rate= 0.0006983373
Iter 8,Testing Accuracy 0.9757, Learning Rate= 0.0006634204
Iter 9,Testing Accuracy 0.9797, Learning Rate= 0.0006302494
Iter 10,Testing Accuracy 0.9773, Learning Rate= 0.0005987369
Iter 11,Testing Accuracy 0.9795, Learning Rate= 0.0005688001
Iter 12,Testing Accuracy 0.9786, Learning Rate= 0.0005403601
Iter 13,Testing Accuracy 0.9801, Learning Rate= 0.0005133421
Iter 14,Testing Accuracy 0.9807, Learning Rate= 0.000487675
Iter 15,Testing Accuracy 0.9806, Learning Rate= 0.00046329122
Iter 16,Testing Accuracy 0.9814, Learning Rate= 0.00044012666
Iter 17,Testing Accuracy 0.9807, Learning Rate= 0.00041812033
Iter 18,Testing Accuracy 0.9802, Learning Rate= 0.00039721432
Iter 19,Testing Accuracy 0.9817, Learning Rate= 0.0003773536
Iter 20,Testing Accuracy 0.9807, Learning Rate= 0.00035848594
Iter 21,Testing Accuracy 0.9804, Learning Rate= 0.00034056162
Iter 22,Testing Accuracy 0.9801, Learning Rate= 0.00032353355
Iter 23,Testing Accuracy 0.9814, Learning Rate= 0.00030735688
Iter 24,Testing Accuracy 0.9818, Learning Rate= 0.000291989
Iter 25,Testing Accuracy 0.9821, Learning Rate= 0.00027738957
Iter 26,Testing Accuracy 0.981, Learning Rate= 0.0002635201
Iter 27,Testing Accuracy 0.9818, Learning Rate= 0.00025034408
Iter 28,Testing Accuracy 0.9826, Learning Rate= 0.00023782688
Iter 29,Testing Accuracy 0.9822, Learning Rate= 0.00022593554
Iter 30,Testing Accuracy 0.9824, Learning Rate= 0.00021463877
Iter 31,Testing Accuracy 0.9811, Learning Rate= 0.00020390682
Iter 32,Testing Accuracy 0.9818, Learning Rate= 0.00019371149
Iter 33,Testing Accuracy 0.9815, Learning Rate= 0.0001840259
Iter 34,Testing Accuracy 0.9814, Learning Rate= 0.00017482461
Iter 35,Testing Accuracy 0.9818, Learning Rate= 0.00016608338
Iter 36,Testing Accuracy 0.9823, Learning Rate= 0.00015777921
Iter 37,Testing Accuracy 0.9828, Learning Rate= 0.00014989026
Iter 38,Testing Accuracy 0.9818, Learning Rate= 0.00014239574
Iter 39,Testing Accuracy 0.9813, Learning Rate= 0.00013527596
Iter 40,Testing Accuracy 0.9815, Learning Rate= 0.00012851215
Iter 41,Testing Accuracy 0.9815, Learning Rate= 0.00012208655
Iter 42,Testing Accuracy 0.9813, Learning Rate= 0.00011598222
Iter 43,Testing Accuracy 0.9811, Learning Rate= 0.00011018311
Iter 44,Testing Accuracy 0.9815, Learning Rate= 0.000104673956
Iter 45,Testing Accuracy 0.9813, Learning Rate= 9.944026e-05
Iter 46,Testing Accuracy 0.9822, Learning Rate= 9.446825e-05
Iter 47,Testing Accuracy 0.9813, Learning Rate= 8.974483e-05
Iter 48,Testing Accuracy 0.9817, Learning Rate= 8.525759e-05
Iter 49,Testing Accuracy 0.9823, Learning Rate= 8.099471e-05
Iter 50,Testing Accuracy 0.9816, Learning Rate= 7.6944976e-05

你可能感兴趣的:(tensorflow)