Keras在訓(xùn)練期間可視化訓(xùn)練誤差和測(cè)試誤差實(shí)例
詳細(xì)的解釋?zhuān)x者自行打開(kāi)這個(gè)鏈接查看,我這里只把最重要的說(shuō)下
fit() 方法會(huì)返回一個(gè)訓(xùn)練期間歷史數(shù)據(jù)記錄對(duì)象,包含 training error, training accuracy, validation error, validation accuracy 字段,如下打印
# list all data in history
print(history.history.keys())
完整代碼
# Visualize training history
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt
import numpy
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
history = model.fit(X, Y, validation_split=0.33, epochs=150, batch_size=10, verbose=0)
# list all data in history
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

補(bǔ)充知識(shí):訓(xùn)練時(shí)同時(shí)輸出實(shí)時(shí)cost、準(zhǔn)確率圖
首先定義畫(huà)圖函數(shù):
train_prompt = "Train cost" cost_ploter = Ploter(train_prompt) def event_handler_plot(ploter_title, step, cost): cost_ploter.append(ploter_title, step, cost) cost_ploter.plot()
在訓(xùn)練時(shí)如下方式使用:
EPOCH_NUM = 8
# 開(kāi)始訓(xùn)練
lists = []
step = 0
for epochs in range(EPOCH_NUM):
# 開(kāi)始訓(xùn)練
for batch_id, train_data in enumerate(train_reader()): #遍歷train_reader的迭代器,并為數(shù)據(jù)加上索引batch_id
train_cost,sult,lab,vgg = exe.run(program=main_program, #運(yùn)行主程序
feed=feeder.feed(train_data), #喂入一個(gè)batch的數(shù)據(jù)
fetch_list=[avg_cost,predict,label,VGG]) #fetch均方誤差和準(zhǔn)確率
if step % 10 == 0:
event_handler_plot(train_prompt,step,train_cost[0])
# print(batch_id)
if batch_id % 10 == 0: #每100次batch打印一次訓(xùn)練、進(jìn)行一次測(cè)試
p = [np.sum(pre) for pre in sult]
l = [np.sum(pre) for pre in lab]
print(p,l,np.sum(sult),np.sum(lab))
print('Pass:%d, Batch:%d, Cost:%0.5f' % (epochs, batch_id, train_cost[0]))
step += 1
# 保存模型
if model_save_dir is not None:
fluid.io.save_inference_model(model_save_dir, ['images'], [predict], exe)
print('訓(xùn)練模型保存完成!')
end = time.time()
print(time.strftime('V100訓(xùn)練用時(shí):%M分%S秒',time.localtime(end-start)))
實(shí)時(shí)顯示準(zhǔn)確率用同樣的方法
以上這篇Keras在訓(xùn)練期間可視化訓(xùn)練誤差和測(cè)試誤差實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何用python將文件夾內(nèi)多個(gè)excel表格合并成總表
前幾天遇見(jiàn)這么一個(gè)問(wèn)題,手上有很多張表格,這些表格中都只有一個(gè)sheet,需要把這些表匯總到一張表,下面這篇文章主要給大家介紹了關(guān)于如何用python將文件夾內(nèi)多個(gè)excel表格合并成總表的相關(guān)資料,需要的朋友可以參考下2023-06-06
解讀python如何實(shí)現(xiàn)決策樹(shù)算法
在本篇文章里我們給讀者們分享了關(guān)于python如何實(shí)現(xiàn)決策樹(shù)算法的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考下。2018-10-10
pytorch中的自定義反向傳播,求導(dǎo)實(shí)例
今天小編就為大家分享一篇pytorch中的自定義反向傳播,求導(dǎo)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的示例代碼
這篇文章主要介紹了Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的的相關(guān)示例,文中有詳細(xì)的代碼示例供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04
PyTorch CUDA環(huán)境配置及安裝的步驟(圖文教程)
這篇文章主要介紹了PyTorch CUDA環(huán)境配置及安裝的步驟(圖文教程),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Python類(lèi)型注解必備利器typing模塊全面解讀
在Python 3.5版本后引入的typing模塊為Python的靜態(tài)類(lèi)型注解提供了支持,這個(gè)模塊在增強(qiáng)代碼可讀性和維護(hù)性方面提供了幫助,本文將深入探討typing模塊,介紹其基本概念、常用類(lèi)型注解以及使用示例,以幫助讀者更全面地了解和應(yīng)用靜態(tài)類(lèi)型注解2024-01-01
Python光學(xué)仿真學(xué)習(xí)處理高斯光束分布圖像
這篇文章主要為大家介紹了Python光學(xué)仿真學(xué)習(xí)之如何處理高斯光束的分布圖像,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10

