Keras在訓(xùn)練期間可視化訓(xùn)練誤差和測試誤差實(shí)例
詳細(xì)的解釋,讀者自行打開這個(gè)鏈接查看,我這里只把最重要的說下
fit() 方法會(huì)返回一個(gè)訓(xùn)練期間歷史數(shù)據(jù)記錄對象,包含 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)確率圖
首先定義畫圖函數(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
# 開始訓(xùn)練
lists = []
step = 0
for epochs in range(EPOCH_NUM):
# 開始訓(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)行一次測試
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)練誤差和測試誤差實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何用python將文件夾內(nèi)多個(gè)excel表格合并成總表
前幾天遇見這么一個(gè)問題,手上有很多張表格,這些表格中都只有一個(gè)sheet,需要把這些表匯總到一張表,下面這篇文章主要給大家介紹了關(guān)于如何用python將文件夾內(nèi)多個(gè)excel表格合并成總表的相關(guān)資料,需要的朋友可以參考下2023-06-06
pytorch中的自定義反向傳播,求導(dǎo)實(shí)例
今天小編就為大家分享一篇pytorch中的自定義反向傳播,求導(dǎo)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的示例代碼
這篇文章主要介紹了Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的的相關(guān)示例,文中有詳細(xì)的代碼示例供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04
PyTorch CUDA環(huán)境配置及安裝的步驟(圖文教程)
這篇文章主要介紹了PyTorch CUDA環(huán)境配置及安裝的步驟(圖文教程),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Python光學(xué)仿真學(xué)習(xí)處理高斯光束分布圖像
這篇文章主要為大家介紹了Python光學(xué)仿真學(xué)習(xí)之如何處理高斯光束的分布圖像,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10

