簡(jiǎn)述
- 使用卷積神經(jīng)網(wǎng)絡(luò)做數(shù)字識(shí)別谜悟;
- 訓(xùn)練方法饥瓷;
前三部分是卷積和神經(jīng)網(wǎng)絡(luò)的構(gòu)造舷丹,最后一部分是tensorflow的會(huì)話(huà)部分,會(huì)話(huà)部分要將數(shù)據(jù)集分為大概100個(gè)一份的數(shù)據(jù)集翘魄,一份一份的喂進(jìn)卷積入口鼎天,原數(shù)據(jù)集有5萬(wàn)個(gè)數(shù)據(jù),這樣就能進(jìn)行500次的反向傳播暑竟,以更新參數(shù)斋射,再做20次循環(huán),可以更新一萬(wàn)次但荤,這比一次喂進(jìn)5萬(wàn)個(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ì)話(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
識(shí)別準(zhǔn)確率達(dá)到了98.99%