使用Keras畫神經(jīng)網(wǎng)絡準確性圖教程
1.在搭建網(wǎng)絡開始時,會調用到 keras.models的Sequential()方法,返回一個model參數(shù)表示模型
2.model參數(shù)里面有個fit()方法,用于把訓練集傳進網(wǎng)絡。fit()返回一個參數(shù),該參數(shù)包含訓練集和驗證集的準確性acc和錯誤值loss,用這些數(shù)據(jù)畫成圖表即可。
如:
history=model.fit(x_train, y_train, batch_size=32, epochs=5, validation_split=0.25) #獲取數(shù)據(jù) #########畫圖 acc = history.history['acc'] #獲取訓練集準確性數(shù)據(jù) val_acc = history.history['val_acc'] #獲取驗證集準確性數(shù)據(jù) loss = history.history['loss'] #獲取訓練集錯誤值數(shù)據(jù) val_loss = history.history['val_loss'] #獲取驗證集錯誤值數(shù)據(jù) epochs = range(1,len(acc)+1) plt.plot(epochs,acc,'bo',label='Trainning acc') #以epochs為橫坐標,以訓練集準確性為縱坐標 plt.plot(epochs,val_acc,'b',label='Vaildation acc') #以epochs為橫坐標,以驗證集準確性為縱坐標 plt.legend() #繪制圖例,即標明圖中的線段代表何種含義 plt.figure() #創(chuàng)建一個新的圖表 plt.plot(epochs,loss,'bo',label='Trainning loss') plt.plot(epochs,val_loss,'b',label='Vaildation loss') plt.legend() ##繪制圖例,即標明圖中的線段代表何種含義 plt.show() #顯示所有圖表
得到效果:

完整代碼:
import keras
from keras.datasets import mnist
from keras.layers import Conv2D, MaxPool2D, Dense, Flatten,Dropout
from keras.models import Sequential
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)
x_train = x_train / 255.
x_test = x_test / 255.
y_train = keras.utils.to_categorical(y_train)
y_test = keras.utils.to_categorical(y_test)
model = Sequential()
model.add(Conv2D(20,(5,5),strides=(1,1),input_shape=(28,28,1),padding='valid',activation='relu',kernel_initializer='uniform'))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(64,(5,5),strides=(1,1),padding='valid',activation='relu',kernel_initializer='uniform'))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
model.add(Flatten())
model.add(Dense(500,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10,activation='softmax'))
model.compile('sgd', loss='categorical_crossentropy', metrics=['accuracy']) #隨機梯度下降
history=model.fit(x_train, y_train, batch_size=32, epochs=5, validation_split=0.25) #獲取數(shù)據(jù)
#########畫圖
acc = history.history['acc'] #獲取訓練集準確性數(shù)據(jù)
val_acc = history.history['val_acc'] #獲取驗證集準確性數(shù)據(jù)
loss = history.history['loss'] #獲取訓練集錯誤值數(shù)據(jù)
val_loss = history.history['val_loss'] #獲取驗證集錯誤值數(shù)據(jù)
epochs = range(1,len(acc)+1)
plt.plot(epochs,acc,'bo',label='Trainning acc') #以epochs為橫坐標,以訓練集準確性為縱坐標
plt.plot(epochs,val_acc,'b',label='Vaildation acc') #以epochs為橫坐標,以驗證集準確性為縱坐標
plt.legend() #繪制圖例,即標明圖中的線段代表何種含義
plt.figure() #創(chuàng)建一個新的圖表
plt.plot(epochs,loss,'bo',label='Trainning loss')
plt.plot(epochs,val_loss,'b',label='Vaildation loss')
plt.legend() ##繪制圖例,即標明圖中的線段代表何種含義
以上這篇使用Keras畫神經(jīng)網(wǎng)絡準確性圖教程就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
升級keras解決load_weights()中的未定義skip_mismatch關鍵字問題
這篇文章主要介紹了升級keras解決load_weights()中的未定義skip_mismatch關鍵字問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
pytorch下的unsqueeze和squeeze的用法說明
這篇文章主要介紹了pytorch下的unsqueeze和squeeze的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Pymysql實現(xiàn)往表中插入數(shù)據(jù)過程解析
這篇文章主要介紹了Pymysql實現(xiàn)往表中插入數(shù)據(jù)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06
Python?一篇文章看懂Python集合與字典數(shù)據(jù)類型
集合并不是一種數(shù)據(jù)處理類型,而是一種中間類型。集合(set)是一個無序、不重復的元素序列,經(jīng)常被用來處理兩個列表進行交并差的處理性。本文將詳細講解集合的一些常用方法,感興趣的可以了解一下2022-03-03
Python實現(xiàn)基于KNN算法的筆跡識別功能詳解
這篇文章主要介紹了Python實現(xiàn)基于KNN算法的筆跡識別功能,結合實例形式詳細分析了使用KNN算法進行筆跡識別的相關庫引入、操作步驟與相關注意事項,需要的朋友可以參考下2018-07-07

