1. 程式人生 > >記錄MNIST採用卷積方式實現與理解

記錄MNIST採用卷積方式實現與理解

從時間上來說,這篇文章寫的完了,因為這個實驗早就做完了;但從能力上來說,這篇文章出現的早了,因為很多地方我都還沒有理解。如果不現在寫,不知道什麼時候會有時間是其一,另外一個原因是怕自己過段時間忘記。

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 # @Author  : mario
 5 # @File    : mnist_faltung.py
 6 # @Project : base
 7 # @Time    : 2018-12-19 14:11:38
 8 # @Desc    : File is ...
 9 
10
import tensorflow as tf 11 from tensorflow.examples.tutorials.mnist import input_data 12 13 mnist = input_data.read_data_sets("data/", one_hot=True) 14 15 16 def init_weight_variable(shape): 17 initial = tf.truncated_normal(shape, stddev=0.1) 18 return tf.Variable(initial) 19 20 21 def init_bias_variable(shape):
22 initial = tf.constant(0.1, shape=shape) 23 return tf.Variable(initial) 24 25 26 def conv2d(x, W): 27 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME") 28 29 30 def max_pool_2x2(x): 31 return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME"
) 32 33 34 x = tf.placeholder(tf.float32, [None, 784]) 35 y_ = tf.placeholder(tf.float32, [None, 10]) 36 37 W_conv_1 = init_weight_variable([5, 5, 1, 32]) 38 b_conv_1 = init_bias_variable([32]) 39 40 x_image = tf.reshape(x, [-1, 28, 28, 1]) 41 42 h_conv_1 = tf.nn.relu(conv2d(x_image, W_conv_1) + b_conv_1) 43 h_pool_1 = max_pool_2x2(h_conv_1) 44 45 W_conv_2 = init_weight_variable([5, 5, 32, 64]) 46 b_conv_2 = init_bias_variable([64]) 47 48 h_conv_2 = tf.nn.relu(conv2d(h_pool_1, W_conv_2) + b_conv_2) 49 h_pool_2 = max_pool_2x2(h_conv_2) 50 51 W_fc_1 = init_weight_variable([7 * 7 * 64, 1024]) 52 b_fc_1 = init_bias_variable([1024]) 53 54 h_pool_flat = tf.reshape(h_pool_2, [-1, 7 * 7 * 64]) 55 h_fc_1 = tf.nn.relu(tf.matmul(h_pool_flat, W_fc_1) + b_fc_1) 56 57 keep_prob = tf.placeholder(tf.float32) 58 h_fc1_drop = tf.nn.dropout(h_fc_1, keep_prob) 59 60 W_fc_2 = init_weight_variable([1024, 10]) 61 b_fc_2 = init_bias_variable([10]) 62 63 y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc_2) + b_fc_2) 64 65 cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) 66 train_step = tf.train.AdagradOptimizer(1e-4).minimize(cross_entropy) 67 68 sess = tf.InteractiveSession() 69 init = tf.global_variables_initializer() 70 sess.run(init) 71 72 for _ in range(20000): 73 batch = mnist.train.next_batch(50) 74 train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) 75 76 correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) 77 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 78 79 print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

python版本:Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31) ;tensor flow版本:1.12.0

10、11行:匯入必要模組

13行:載入本地的資料

16~18行:定義初始化權重變數函式

21~23行:定義初始化偏置變數函式

26~27行:定義一個步長為1,邊距為0的2x2的卷積函式

30~31行:定義一個2x2的池化函式

34、35行:定義佔位符x和y_,其中x是為了接收原始資料,y_是為了接受原始資料標籤

37、38行:初始化第一層卷積權重和偏置量

40行:重塑原始資料結構,我們要把[n,784]這樣的資料結構轉換成卷積需要的[n,28,28,1],這裡的n是指資料量,原始資料是將28x28畫素的圖片展開為784,卷積相當於我們先將資料還原為28x28,最後的1是指通道數

42、43行:進行卷積操作,並將卷積結果池化

45~49行:進行第二次卷積操作,同樣也是將卷積結果池化

51~61行:使用ReLU,其中57行和58行是為了防止過擬合

63行:使用softmax演算法確定其分類

65行:計算交叉熵

66行:利用交叉熵,呼叫AdagradOptimizer演算法,訓練模型

68~70行:啟用session,初始化變數

72~74行:每次50個訓練20000次

76~79行:評估模型識別率

也是遇到了很多的問題,但幾乎都是因為不理解程式碼造成的,雖然現在程式碼是改對了,但是不理解的地方還是有很多,而且很多概念也是不理解,並且不知道實際上是做了什麼操作,比如說卷積、池化等,倒是做了什麼?感覺這個還是需要後續瞭解的。“路漫漫其修遠兮,吾將上下而求索”用在這裡再合適不過了。