1. 程式人生 > >第二節,TensorFlow 使用前饋神經網絡實現手寫數字識別

第二節,TensorFlow 使用前饋神經網絡實現手寫數字識別

com net config return pyplot dataset 運行 算法 但是

一 感知器

感知器學習筆記:https://blog.csdn.net/liyuanbhu/article/details/51622695

感知器(Perceptron)是二分類的線性分類模型,其輸入為實例的特征向量,輸出為實例的類別,取+1和-1。這種算法的局限性很大:

  1. 只能將數據分為 2 類
  2. 數據必須是線性可分的

雖然有這些局限,但是感知器是 ANN 和 SVM 的基礎,理解了感知器的原理,對學習ANN 和 SVM 會有幫助,所以還是值得花些時間的。

感知器可以表示為 f:Rn -> {-1,+1}的映射函數,其中f的形式如下:

f(x) = sign(w.x+b)

其中w,b都是n維列向量,w表示權重,b表示偏置,w.x表示w和x的內積。感知器的訓練過程其實就是求解w和b的過程,正確的w和b所構成的超平面

w.x + b=0恰好將兩類數據點分割在這個平面的兩側。

二 神經網絡

今天,使用的神經網絡就是由一個個類似感知器的神經元模型疊加拼接成的。目前常用的神經元包括S型神經元,ReLU神經元,tanh神經元,Softmax神

經元等等。

手寫數字識別是目前在學習神經網絡中普遍使用的案例。在這個案例中將的是簡單的全連接網絡實現手寫數字識別,這個例子主要包括三個部分。

1.模型搭建

2.確定目標函數,設置損失和梯度值

3.選擇算法,設置優化器選擇合適的學習率更新權重和偏置。

手寫數字識別代碼如下:

# -*- coding: utf-8 -*-
"""
Created on Sun Apr  1 19:16:15 2018

@author: Administrator
"""

‘‘‘
使用TnsorFlow實現手寫數字識別
‘‘‘

import numpy as np
import matplotlib.pyplot as plt

#繪制訓練集準確率,以及測試卷準確率曲線
def plot_overlay_accuracy(training_accuracy,test_accuaracy):
    ‘‘‘
    test_accuracy,training_accuracy:訓練集測試集準確率
    
‘‘‘ #叠代次數 num_epochs = len(test_accuaracy) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(np.arange(0, num_epochs), [accuracy*100.0 for accuracy in test_accuaracy], color=#2A6EA6, label="Accuracy on the test data") ax.plot(np.arange(0, num_epochs), [accuracy*100.0 for accuracy in training_accuracy], color=#FFA933, label="Accuracy on the training data") ax.grid(True) ax.set_xlim([0, num_epochs]) ax.set_xlabel(Epoch) ax.set_ylim([90, 100]) plt.legend(loc="lower right") #右小角 plt.show() #繪制訓練集代價和測試卷代價函數曲線 def plot_overlay_cost(training_cost,test_cost): ‘‘‘ test,reaining:訓練集測試集代價 list類型 ‘‘‘ #叠代次數 num_epochs = len(test_cost) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(np.arange(0, num_epochs), [cost for cost in test_cost], color=#2A6EA6, label="Cost on the test data") ax.plot(np.arange(0, num_epochs), [cost for cost in training_cost], color=#FFA933, label="Cost on the training data") ax.grid(True) ax.set_xlim([0, num_epochs]) ax.set_xlabel(Epoch) #ax.set_ylim([0, 0.75]) plt.legend(loc="upper right") plt.show() from sklearn.metrics import classification_report from sklearn.metrics import confusion_matrix ‘‘‘ 打印圖片 images:list或者tuple,每一個元素對應一張圖片 title:list或者tuple,每一個元素對應一張圖片的標題 h:高度的像素數 w:寬度像素數 n_row:輸出行數 n_col:輸出列數 ‘‘‘ def plot_gallery(images,title,h,w,n_row=3,n_col=4): #指定整個繪圖對象的寬度和高度 plt.figure(figsize=(1.8*n_col,2.4*n_row)) plt.subplots_adjust(bottom=0,left=.01,right=.99,top=.90,hspace=.35) #繪制每個子圖 for i in range(n_row*n_col): #第i+1個子窗口 默認從1開始編號 plt.subplot(n_row,n_col,i+1) #顯示圖片 傳入height*width矩陣 https://blog.csdn.net/Eastmount/article/details/73392106?locationNum=5&fps=1 plt.imshow(images[i].reshape((h,w)),cmap=plt.cm.gray) #cmap Colormap 灰度 #設置標題 plt.title(title[i],size=12) plt.xticks(()) plt.yticks(()) ‘‘‘ 打印第i個測試樣本對應的標題 Y_pred:測試集預測結果集合 (分類標簽集合) Y_test: 測試集真實結果集合 (分類標簽集合) target_names:分類每個標簽對應的名稱 i:第i個樣本 ‘‘‘ def title(Y_pred,Y_test,target_names,i): pred_name = target_names[Y_pred[i]].rsplit( ,1)[-1] true_name = target_names[Y_test[i]].rsplit( ,1)[-1] return predicted:%s\ntrue: %s %(pred_name,true_name) import tensorflow as tf #設置tensorflow對GPU使用按需分配 config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.InteractiveSession(config=config) ‘‘‘ 一 導入數據 ‘‘‘ from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets(MNIST-data,one_hot=True) print(type(mnist)) #<class ‘tensorflow.contrib.learn.python.learn.datasets.base.Datasets‘> print(Training data shape:,mnist.train.images.shape) #Training data shape: (55000, 784) print(Test data shape:,mnist.test.images.shape) #Test data shape: (10000, 784) print(Validation data shape:,mnist.validation.images.shape) #Validation data shape: (5000, 784) print(Training label shape:,mnist.train.labels.shape) #Training label shape: (55000, 10) ‘‘‘ 二 搭建前饋神經網絡模型 搭建一個包含輸入層分別為 784,1024,10個神經元的神經網絡 ‘‘‘ #初始化權值和偏重 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) #input layer x_ = tf.placeholder(tf.float32,shape=[None,784]) y_ = tf.placeholder(tf.float32,shape=[None,10]) #隱藏層 w_h = weight_variable([784,1024]) b_h = bias_variable([1024]) hidden = tf.nn.relu(tf.matmul(x_,w_h) + b_h) #輸出層 w_o = weight_variable([1024,10]) b_o = bias_variable([10]) output = tf.nn.softmax(tf.matmul(hidden,w_o) + b_o) ‘‘‘ 三 設置對數似然損失函數 ‘‘‘ #代價函數 J =-(Σy.logaL)/n .表示逐元素乘 cost = -tf.reduce_sum(y_*tf.log(output)) ‘‘‘ 四 求解 ‘‘‘ train = tf.train.AdamOptimizer(0.001).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)) #求準確率 #創建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): x_batch,y_batch = mnist.train.next_batch(batch_size = 64) #開始訓練 train.run(feed_dict={x_:x_batch,y_:y_batch}) if (i+1)%200 == 0: #輸出訓練集準確率 #training_accuracy = accuracy.eval(feed_dict={x_:mnist.train.images,y_:mnist.train.labels}) training_accuracy,training_cost = sess.run([accuracy,cost],feed_dict={x_:mnist.train.images,y_:mnist.train.labels}) training_accuracy_list.append(training_accuracy) training_cost_list.append(training_cost) print({0}:Training set accuracy {1},cost {2}..format(i+1,training_accuracy,training_cost)) #輸出測試機準確率 #test_accuracy = accuracy.eval(feed_dict={x_:mnist.test.images,y_:mnist.test.labels}) test_accuracy,test_cost = sess.run([accuracy,cost],feed_dict={x_:mnist.test.images,y_:mnist.test.labels}) test_accuracy_list.append(test_accuracy) test_cost_list.append(test_cost) print({0}:Test set accuracy {1},cost {2}..format(i+1,test_accuracy,test_cost)) #繪制曲線圖 plot_overlay_cost(training_cost_list,test_cost_list) plot_overlay_accuracy(training_accuracy_list,test_accuracy_list) #取24個樣本,可視化顯示預測效果 x_batch,y_batch = mnist.test.next_batch(batch_size = 24) #獲取x_batch圖像對象的數字標簽 y_test = np.argmax(y_batch,1) #獲取預測結果 y_pred = np.argmax(output.eval(feed_dict={x_:x_batch,y_:y_batch}),1) #顯示與分類標簽0-9對應的名詞 target_names = [number 0,number 1,number 2,number 3,number 4,number 5,number 6,number 7,number 8,number 9] #需要測試的真實的標簽和預測作為比較 顯示主要的分類指標,返回每個類標簽的精確、召回率及F1值 print(classification_report(y_test,y_pred,target_names = target_names)) #建立一個n*n 分別對應每一組真實的和預測的值 用於呈現一種可視化效果 print(confusion_matrix(y_test,y_pred,labels = range(len(target_names)))) #標題 prediction_titles = [title(y_pred,y_test,target_names,i) for i in range(y_pred.shape[0])] #打印圖片 plot_gallery(x_batch,prediction_titles,28,28,6,4) plt.show()

運行結果如下:

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

f:RN{?1,

第二節,TensorFlow 使用前饋神經網絡實現手寫數字識別