1. 程式人生 > 實用技巧 >Tensorflow框架學習(1)--CNN的實現

Tensorflow框架學習(1)--CNN的實現

 1 # CNN神經網路的程式碼實現
 2 import tensorflow as tf
 3 # 匯入資料集
 4 from tensorflow.examples.tutorials.mnist import input_data
 5 # number 1 to 10 data:讀取資料
 6 mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
 7 
 8 sess = tf.Session()
 9 
10 #計算準確率
11 def compute_accuracy(v_xs, v_ys):
12     global
prediction 13 y_pre = sess.run(prediction, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 0.5}) # keep_prob的設定是為了防止發生過擬合的現象 14 correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1)) 15 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 16 result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 0.5})
17 return result 18 19 # 定義並初始化weight_variable 20 def weight_variable(shape): 21 initial = tf.truncated_normal(shape, stddev=0.1) 22 return tf.Variable(initial) 23 24 # 定義並初始化bias_variable 25 def bias_variable(shape): 26 initial = tf.constant(0.1, shape=shape) 27 return tf.Variable(initial)
28 29 #定義卷積層 30 def conv2d(x,W): 31 # strides=[1,x_movement,y_movement,1] 32 # strides[0] = strides[3] = 0 33 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME") 34 35 #定義池化層 36 def max_pool_2x2(x): 37 # strides=[1,x_movement,y_movement,1] 38 # strides[0] = strides[3] = 0 39 # ksize=[1,x,x,1]:其中表示池化的大小 40 # 在池化得我過程中,不能有重疊,否則會出現報錯 --栽溝裡了!!! 41 return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") 42 43 # 定義placeholder for inputs to network,None表示輸入的樣本數待定 44 xs = tf.placeholder(tf.float32, [None, 784]) # None表示輸入的樣本數待定(待定) 45 ys = tf.placeholder(tf.float32, [None, 10]) 46 keep_prob = tf.placeholder(tf.float32) 47 48 # 將輸入的資料變換成矩陣的形式 49 # 此時x_image表示的是[n_samples,28,28,1],其中1表示channel 50 x_image = tf.reshape(xs, [-1, 28, 28, 1]) # -1表示輸入的樣本數待定(待定) 51 52 # 定義conv1 layer--卷積層1 53 # [5,5,1,32] 54 # 5*5:patch size 1:channel 32:filter的數量 55 W_conv1 = weight_variable([5, 5, 1, 32]) 56 b_conv1 = bias_variable([32]) 57 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) #output_size:28*28*32 58 h_pool1 = max_pool_2x2(h_conv1) #output_size:14*14*32 59 60 # 定義conv2 layer 61 # 32:第二層卷積中卷積核的深度 64:卷積核的個數 62 W_conv2 = weight_variable([5, 5, 32, 64]) 63 b_conv2 = bias_variable([64]) 64 h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) #output_size:14*14*64 65 h_pool2 = max_pool_2x2(h_conv2) #output_size:7*7*64 66 67 68 # 定義func1 layer:全卷積層1 69 W_fc1 = weight_variable([7*7*64, 1024]) #全連線層的in_size:7*7*64 out_size:1024 70 b_fc1 = bias_variable([1024]) 71 # 將con2 layer的輸出拉平:[n_sample,7,7,64] ->> [n_sample,7*7*64] 72 h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) # -1表示輸入的樣本數(待定) 73 h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 74 # 為了防止出現過擬合,使用tf.nn.dropout() 75 h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 76 77 # 定義func2 layer 78 W_fc2 = weight_variable([1024, 10]) #全連線層的in_size:1024 out_size:10(分類對應10個數據) 79 b_fc2 = bias_variable([10]) 80 # 將con2 layer的輸出拉平:[n_sample,7,7,64] ->> [n_sample,7*7*64] 81 prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2)+b_fc2) 82 83 # the error between prediction and real data 84 cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1])) 85 # 優化器的選擇 86 train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) 87 88 # 定義變數需要初始化所有的引數 -- very important 89 sess.run(tf.initialize_all_variables()) 90 91 # 資料的訓練 92 for i in range(1000): 93 batch_xs, batch_ys = mnist.train.next_batch(100) 94 sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5}) 95 if i % 50 == 0: 96 # 每訓練50次,計算一下準確略:函式需要自定義 97 print(compute_accuracy( 98 mnist.test.images, mnist.test.labels))

說明:

  在實現CNN過程中,定義池化層的時候,由於strides設定錯誤([1, 1, 1, 1] ->>[1,2, 2,1]),導致在進行MaxPooling時出現重疊進而導致後續的運算出現矩陣維度不匹配的錯誤