用tensorflow 做mnist數(shù)字識別(卷積神經(jīng)網(wǎng)絡(luò))

簡述

  1. 使用卷積神經(jīng)網(wǎng)絡(luò)做數(shù)字識別;
  2. 訓(xùn)練方法;
    前三部分是卷積和神經(jīng)網(wǎng)絡(luò)的構(gòu)造,最后一部分是tensorflow的會(huì)話部分,會(huì)話部分要將數(shù)據(jù)集分為大概100個(gè)一份的數(shù)據(jù)集,一份一份的喂進(jìn)卷積入口,原數(shù)據(jù)集有5萬個(gè)數(shù)據(jù),這樣就能進(jìn)行500次的反向傳播,以更新參數(shù),再做20次循環(huán),可以更新一萬次,這比一次喂進(jìn)5萬個(gè)數(shù)據(jù)只更新20次要好的多!務(wù)必牢記這個(gè)訓(xùn)練方法。

代碼

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])
#神經(jīng)網(wǎng)絡(luò)層---------------------------------------------------------------------------#
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))
#會(huì)話---------------------------------------------------------------------------#
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

識別準(zhǔn)確率達(dá)到了98.99%

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容