欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Keras在訓(xùn)練期間可視化訓(xùn)練誤差和測試誤差實(shí)例

 更新時(shí)間:2020年06月16日 10:46:32   作者:bebr  
這篇文章主要介紹了Keras在訓(xùn)練期間可視化訓(xùn)練誤差和測試誤差實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

詳細(xì)的解釋,讀者自行打開這個(gè)鏈接查看,我這里只把最重要的說下

fit() 方法會返回一個(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ǔ)充知識:訓(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表格合并成總表

    如何用python將文件夾內(nèi)多個(gè)excel表格合并成總表

    前幾天遇見這么一個(gè)問題,手上有很多張表格,這些表格中都只有一個(gè)sheet,需要把這些表匯總到一張表,下面這篇文章主要給大家介紹了關(guān)于如何用python將文件夾內(nèi)多個(gè)excel表格合并成總表的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • python跳出雙層for循環(huán)的解決方法

    python跳出雙層for循環(huán)的解決方法

    今天小編就為大家分享一篇python跳出雙層for循環(huán)的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • pyqt5實(shí)現(xiàn)俄羅斯方塊游戲

    pyqt5實(shí)現(xiàn)俄羅斯方塊游戲

    這篇文章主要介紹了pyqt5實(shí)現(xiàn)俄羅斯方塊游戲,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • 解讀python如何實(shí)現(xiàn)決策樹算法

    解讀python如何實(shí)現(xiàn)決策樹算法

    在本篇文章里我們給讀者們分享了關(guān)于python如何實(shí)現(xiàn)決策樹算法的相關(guān)知識點(diǎn)內(nèi)容,需要的朋友們參考下。
    2018-10-10
  • pytorch中的自定義反向傳播,求導(dǎo)實(shí)例

    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ù)的示例代碼

    這篇文章主要介紹了Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的的相關(guān)示例,文中有詳細(xì)的代碼示例供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-04-04
  • Python中sys.argv用法圖文詳解

    Python中sys.argv用法圖文詳解

    很多剛剛接觸python的人來說,對于python中sys.argv[]往往不是很明白,下面這篇文章主要給大家介紹了關(guān)于Python中sys.argv用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • PyTorch CUDA環(huán)境配置及安裝的步驟(圖文教程)

    PyTorch CUDA環(huán)境配置及安裝的步驟(圖文教程)

    這篇文章主要介紹了PyTorch CUDA環(huán)境配置及安裝的步驟(圖文教程),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Python類型注解必備利器typing模塊全面解讀

    Python類型注解必備利器typing模塊全面解讀

    在Python 3.5版本后引入的typing模塊為Python的靜態(tài)類型注解提供了支持,這個(gè)模塊在增強(qiáng)代碼可讀性和維護(hù)性方面提供了幫助,本文將深入探討typing模塊,介紹其基本概念、常用類型注解以及使用示例,以幫助讀者更全面地了解和應(yīng)用靜態(tài)類型注解
    2024-01-01
  • Python光學(xué)仿真學(xué)習(xí)處理高斯光束分布圖像

    Python光學(xué)仿真學(xué)習(xí)處理高斯光束分布圖像

    這篇文章主要為大家介紹了Python光學(xué)仿真學(xué)習(xí)之如何處理高斯光束的分布圖像,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-10-10

最新評論