# -*- coding: utf-8 -*-
"""
**使用CNN實作手寫數字識別代碼練習
"""
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import numpy as np
"""
一 匯入資料
"""
from tensorflow.examples.tutorials.mnist import input_data
#mnist是一個輕量級的類,它以numpy陣列的形式儲存著訓練,校驗,測驗資料集 one_hot表示輸出二值后的10維
mnist = input_data.read_data_sets('MNIST-data',one_hot=True)
print(type(mnist))
print('Training data shape:', mnist.train.images.shape)
print('Test data shape:', mnist.test.images.shape)
print('Validation data shape:', mnist.validation.images.shape)
print('Training label shape:', mnist.train.labels.shape)
#設定tennsorflow對GPU使用按需分配
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.compat.v1.InteractiveSession(config=config)
"""
二 構建網路
"""
"""
初始化權值和偏重
為了創建這個模型,我們需要創建大量的權重的話偏置項。這個模型中的權重在初始化時應該加入少量的噪聲來打破對稱性以及避免0梯度。
由于我們使用的是ReLU神經元,因此比較好的做法是用一個較小的正數來初始化偏置項,以避免神經元節點輸出恒為0的問題(dead neurons).
為了不再建立模型時反復做初始化操作,我們定義兩個函式用于初始化。
"""
def weight_variable(shape):
#使用正態分布初始化權值
initial = tf.truncated_normal(shape, stddev=0.1) #標準差為0.1
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
"""
卷積層和池化層
TensorFlow在卷積和池化上有很強的靈活性。我們怎樣處理邊界?步長應該設定多大?在這個實體里,我們會一直使用vanilla版本。我們
的卷積使用1步長(stride size),0邊距(padding size)的模板,保證輸出和輸入是同一個大小。我們的池化用簡單傳統的2x2大小的模板做
max_pooling。為了代碼更簡潔,我們把這部分抽象成一個函式。
"""
#定義卷積層
def conv2d(x, W):
"""
默認 strides[0] = strides[3] = 1, strides[1]為x方向步長,strides[2]為y方向步長,
"""
return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')
#pooling層
def max_pooling(x):
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
#我們通過輸入影像和目標輸出類別創建節點,來開始構建計算題 None表示數值不固定,用來指定batch的大小
x_ = tf.placeholder(tf.float32, [None,784])
y_ = tf.placeholder(tf.float32, [None, 10])
#把x轉換為卷積所需要的的形式 batch_size張手寫數字,每張維度為 1x28x28
"""
為了用這一層,我們把 x 變成了一個4d向量,其第2、第3維對應圖片的高、寬,最后一維代表圖片的顏色通道數(因為是灰度圖,所以這里
的通道數為1,如果是rgb彩色圖,則為3)
"""
X = tf.reshape(x_, shape=[-1, 28, 28, 1]) # -1 表示reshape的行數在前不知道,直接reshape將 n*784 reshape為 n個28x28
"""
現在我們可以開始第一層。它由一個卷積接一個max pooling完成。卷積在每個 5x5 的patch中算出32個特征。卷積的權重張量形狀是
[5,5,1,32],前兩維是patch的大小,接著是輸入的通道數,最后是輸出的通道數。而對于每一個輸出通道都有個對應的偏置量。
"""
#第一層卷積,32個過濾器,共享權重矩陣為 1*5*5 h_conv1.shape=[-1,28,28,32]
w_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(X, w_conv1) + b_conv1)
#第一個pooling層 最大池化層2x2 [-1,28,28,1]->[-1,14,14,32]
h_pool1 = max_pooling(h_conv1)
#第二層卷積,64個過濾器,共享權重矩陣為32*5*5 h_conv2.shape=[-1,14,14,64]
w_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
#第二個pooling層 最大值池化2x2 [-1,14,14,64]->[-1,7,7,64]
h_pool2 = max_pooling(h_conv2)
"""
全連接層
現在,圖片尺寸減小到7*7,我們加入一個有1024個神經元的全連接層,用于處理整個圖片。我們把池化層輸出的張量reshape成一些向量,
乘上權重矩陣,加上偏置,然后對其使用ReLU
"""
h_pool2_falt = tf.reshape(h_pool2, [-1, 7*7*64])
#隱藏層
w_h = weight_variable([7*7*64, 1024])
b_h = bias_variable([1024])
hidden = tf.nn.relu(tf.matmul(h_pool2_falt, w_h) + b_h)
"""
加入棄權,把部分神經元輸出置為0
為了減少過擬合,我們在輸出層之間加入dropout。我們用一個placeholder來代表一個神經元的輸出在dropout中保持不變的概率。這樣
我們可以在訓練程序中啟用dropout,在測驗程序中關閉dropout。TensorFlow的tf.nn.dropout操作除了可以屏蔽神經元的輸出外,還會
自動處理神經元輸出值的scale。所以用dropout的時候可以不用考慮scale。
"""
keep_prob = tf.placeholder(tf.float32) #棄權概率 0.0-1.0 1.0表示不使用棄權
hidden_drop = tf.nn.dropout(hidden, rate = 1-keep_prob)
"""
輸出層
最后,我們添加一個softmax層,就像前面的單層softmax regression一樣
"""
w_o = weight_variable([1024,10])
b_o = bias_variable([10])
output = tf.nn.softmax(tf.matmul(hidden_drop, w_o) + b_o)
"""
三 設定對數似然損失函式
"""
#代價函式 J =-(∑y.logaL)/n .表示逐元素乘
cost = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(output), axis=1))
"""
四 求解
"""
train = tf.train.AdamOptimizer(0.0001).minimize(cost)
#預測結果評估
#tf.argmax(output,1) 按行統計最大的值的索引
correct = tf.equal(tf.argmax(output,1), tf.argmax(y_,1)) #回傳一個陣列 表示統計預測正確或者錯誤
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) #tf.cast(correct, tf.float32)表示將correct的bool型別轉化成0、1陣列
#創建list保存每一迭代的結果
training_accuracy_list = []
test_accuracy_list = []
training_cost_list = []
test_cost_list = []
#使用會話執行圖
sess.run(tf.global_variables_initializer()) #初始化變數
#開始迭代 使用Adam優化的隨機梯度下降法
for i in range(5000): #一個epoch需要迭代次數計算公式:測驗集長度/batch_size
x_batch, y_batch = mnist.train.next_batch(batch_size=64)
#開始訓練
train.run(feed_dict={x_:x_batch,y_:y_batch,keep_prob:1.0})
if (i+1)%200 ==0:
#輸出訓練集準確率
#training_accuracy = accuracy.eval(feed_dict={x_:mnist.train.images, y_:mnist.trainn.labels})
training_accuracy, training_cost = sess.run([accuracy,cost], feed_dict={x_:x_batch, y_:y_batch, keep_prob:1.0})
training_accuracy_list.append(training_accuracy)
training_cost_list.append(training_cost)
print('Step {0}:Training set accuracy {1},cost {2}'.format(i+1, training_accuracy, training_cost))
#全部測驗完成做測驗 分為200次,一次測驗50個樣本
#輸出測驗集準確率 如果一次性全部做測驗,內容不夠會出現OOM錯誤。所以測驗時選取比較小的mini_batch來測驗
#test_accuracy = accuracy.eval(feed_dict={x_:mnist.test.images, y_:mnist.test.labels})
for i in range(200):
x_batch, y_batch = mnist.test.next_batch(batch_size=50)
test_accuracy, test_cost = sess.run([accuracy,cost], feed_dict={x_:x_batch, y_:y_batch, keep_prob:1.0})
test_accuracy_list.append(test_accuracy)
test_cost_list.append(test_cost)
if (i+1)%20 ==0:
print('Step {0}: Test set accuracy {1}, cost {2}'.format(i + 1, test_accuracy, test_cost))
print('Test accuracy: ', np.mean(test_cost_list))
"""
影像操作
"""
import matplotlib.pyplot as plt
#隨便取一張影像
img = mnist.train.images[2]
label = mnist.train.labels[2]
#print('影像像素值: {0},對應的標簽{1}'.format(img.reshape(28,28), np.argmax(label)))
print('影像對應的標簽{0}'.format(np.argmax(label)))
plt.figure()
#子圖1
plt.subplot(1,2,1)
plt.imshow(img.reshape(28,28)) #顯示的是熱度圖片
plt.axis('off')
#子圖2
plt.subplot(1,2,2)
plt.imshow(img.reshape(28,28), cmap='gray')
plt.axis('off')
plt.show()
###############################
## 顯示卷積和池化層結果 ##
###############################
plt.figure(figsize=(1.0*8,1.6*4))
plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)
#顯示第一個卷積層之后的結果 (1,28,28,32)
conv1 = h_conv1.eval(feed_dict={x_:img.reshape([-1,784]), y_:label.reshape([-1,10]), keep_prob:1.0})
print('conv1 shape:', conv1.shape)
for i in range(32):
show_image = conv1[:,:,:,1]
show_image.shape = [28,28]
plt.subplot(4,8,i+1)
plt.imshow(show_image, cmap='gray')
plt.axis('off')
plt.show()
plt.figure(figsize=(1.2*8, 2.0*4))
plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)
#顯示第一個池化層以后的結果
pool1 = h_pool1.eval(feed_dict={x_:img.reshape([-1,784]), y_:label.reshape([-1,10]), keep_prob:1.0})
print('pool1 shape:', pool1.shape)
for i in range(32):
show_image = pool1[:,:,:,1]
show_image.shape = [14,14]
plt.subplot(4,8,i+1)
plt.imshow(show_image, cmap='gray')
plt.axis('off')
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/274909.html
上一篇:一道python基礎題
