12 使用卷積神經網絡識別手寫數字

    看代碼:html

 1 import tensorflow as tf
 2 from tensorflow.examples.tutorials.mnist import input_data
 3 
 4 # 下載訓練和測試數據
 5 mnist = input_data.read_data_sets('MNIST_data/', one_hot = True)
 6 
 7 # 建立session
 8 sess = tf.Session()
 9 
10 # 佔位符
11 x = tf.placeholder(tf.float32, shape=[None, 784]) # 每張圖片28*28,共784個像素
12 y_ = tf.placeholder(tf.float32, shape=[None, 10]) # 輸出爲0-9共10個數字,其實就是把圖片分爲10類
13 
14 # 權重初始化
15 def weight_variable(shape):
16     initial = tf.truncated_normal(shape, stddev=0.1) # 使用截尾正態分佈的隨機數初始化權重,標準誤差是0.1(噪音)
17     return tf.Variable(initial)
18 
19 def bias_variable(shape):
20     initial = tf.constant(0.1, shape = shape) # 使用一個小正數初始化偏置,避免出現偏置總爲0的狀況
21     return tf.Variable(initial)
22 
23 # 卷積和集合
24 def conv2d(x, W): # 計算2d卷積
25     return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
26 
27 def max_pool_2x2(x): # 計算最大集合
28     return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
29 
30 # 第一層卷積
31 W_conv1 = weight_variable([5, 5, 1, 32]) # 爲每一個5*5小塊計算32個特徵
32 b_conv1 = bias_variable([32])
33 
34 x_image = tf.reshape(x, [-1, 28, 28, 1]) # 將圖片像素轉換爲4維tensor,其中二三維是寬高,第四維是像素
35 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
36 h_pool1 = max_pool_2x2(h_conv1)
37 
38 # 第二層卷積
39 W_conv2 = weight_variable([5, 5, 32, 64])
40 b_conv2 = bias_variable([64])
41 
42 h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
43 h_pool2 = max_pool_2x2(h_conv2)
44 
45 # 密集層
46 W_fc1 = weight_variable([7 * 7 * 64, 1024]) # 建立1024個神經元對整個圖片進行處理
47 b_fc1 = bias_variable([1024])
48 
49 h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
50 h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
51 
52 # 退出(爲了減小過分擬合,在讀取層前面加退出層,僅訓練時有效)
53 keep_prob = tf.placeholder(tf.float32)
54 h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
55 
56 # 讀取層(最後咱們加一個像softmax表達式那樣的層)
57 W_fc2 = weight_variable([1024, 10])
58 b_fc2 = bias_variable([10])
59 
60 y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
61 
62 # 預測類和損失函數
63 cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)) # 計算誤差平均值
64 train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) # 每一步訓練
65 
66 # 評估
67 correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
68 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
69 sess.run(tf.global_variables_initializer())
70 
71 for i in range(1000):
72     batch = mnist.train.next_batch(50)
73     if i%10 == 0:
74         train_accuracy = accuracy.eval(feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}, session = sess) # 每10次訓練計算一次精度
75         print("步數 %d, 精度 %g"%(i, train_accuracy))
76     train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}, session = sess)
77 
78 # 關閉
79 sess.close()

執行上面的代碼後輸出:git

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
步數 0, 精度 0.12
步數 10, 精度 0.34
步數 20, 精度 0.52
步數 30, 精度 0.56
步數 40, 精度 0.6
步數 50, 精度 0.74
步數 60, 精度 0.74
步數 70, 精度 0.78
步數 80, 精度 0.82網絡

..........session

步數 900, 精度 0.96
步數 910, 精度 0.98
步數 920, 精度 0.96
步數 930, 精度 0.98
步數 940, 精度 0.98
步數 950, 精度 0.9
步數 960, 精度 0.98
步數 970, 精度 0.9
步數 980, 精度 1
步數 990, 精度 0.9ide

    能夠看到,使用卷積神經網絡訓練1000次能夠讓精度達到95%以上,聽說訓練20000次精度能夠達到99.2%以上。因爲CPU不行,太耗時間,就不訓練那麼多了。你們能夠跟使用softmax訓練識別手寫數字進行對比。《07 訓練Tensorflow識別手寫數字函數

    參考資料測試

    一、Deep MNIST for Experts:https://www.tensorflow.org/get_started/mnist/prosspa

相關文章
相關標籤/搜索