美文网首页我爱编程
用tensorflow 做mnist数字识别(卷积神经网络)

用tensorflow 做mnist数字识别(卷积神经网络)

作者: 圣_狒司机 | 来源:发表于2018-08-09 16:05 被阅读105次

    简述

    1. 使用卷积神经网络做数字识别;
    2. 训练方法;
      前三部分是卷积和神经网络的构造,最后一部分是tensorflow的会话部分,会话部分要将数据集分为大概100个一份的数据集,一份一份的喂进卷积入口,原数据集有5万个数据,这样就能进行500次的反向传播,以更新参数,再做20次循环,可以更新一万次,这比一次喂进5万个数据只更新20次要好的多!务必牢记这个训练方法。

    代码

    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    mnist=input_data.read_data_sets("./",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])
    x_image = tf.reshape(x,[-1,28,28,1])
    #卷积层---------------------------------------------------------------------------#
    W_conv1 = tf.Variable(tf.truncated_normal([5,5,1,32],stddev=0.1))
    b_conv1 = tf.Variable(tf.constant(0.1,shape=[32]))
    h_conv1 = tf.nn.relu(tf.nn.conv2d(x_image,W_conv1,strides=[1,1,1,1],padding='SAME')+b_conv1)
    h_pool1 = tf.nn.max_pool(h_conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
    
    W_conv2 = tf.Variable(tf.truncated_normal([5,5,32,64],stddev=0.1))
    b_conv2 = tf.Variable(tf.constant(0.1,shape=[64]))
    h_conv2 = tf.nn.relu(tf.nn.conv2d(h_pool1,W_conv2,strides=[1,1,1,1],padding='SAME')+b_conv2)
    h_pool2 = tf.nn.max_pool(h_conv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
    h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
    #神经网络层---------------------------------------------------------------------------#
    W_fc1 = tf.Variable(tf.truncated_normal([7*7*64,1024],stddev=0.1))
    b_fc1 = tf.Variable(tf.constant(0.1,shape=[1024]))
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1) + b_fc1)
    keep_prob = tf.placeholder(tf.float32)
    h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
    
    W_fc2 = tf.Variable(tf.truncated_normal([1024,10],stddev=0.1))
    b_fc2 = tf.Variable(tf.constant(0.1,shape=[10]))
    prediction = tf.nn.relu(tf.matmul(h_fc1_drop,W_fc2) + b_fc2)
    
    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    #会话---------------------------------------------------------------------------#
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range(10):
            for batch in range(n_batch):
                batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
                sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.7})
            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(acc)) 
    

    输出

    Iter 0, Testing Accuracy= 0.9578
    Iter 1, Testing Accuracy= 0.9728
    Iter 2, Testing Accuracy= 0.9788
    Iter 3, Testing Accuracy= 0.984
    Iter 4, Testing Accuracy= 0.9856
    Iter 5, Testing Accuracy= 0.9871
    Iter 6, Testing Accuracy= 0.9864
    Iter 7, Testing Accuracy= 0.9893
    Iter 8, Testing Accuracy= 0.9896
    Iter 9, Testing Accuracy= 0.9899
    

    识别准确率达到了98.99%

    相关文章

      网友评论

        本文标题:用tensorflow 做mnist数字识别(卷积神经网络)

        本文链接:https://www.haomeiwen.com/subject/fhkcbftx.html