您好,登錄后才能下訂單哦!
這篇文章主要介紹keras中深度模型訓(xùn)練的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
記錄訓(xùn)練過(guò)程
history=model.fit(X_train, Y_train, epochs=epochs,batch_size=batch_size,validation_split=0.1)
將訓(xùn)練過(guò)程記錄在history中
利用時(shí)間記錄模型
import time model_id = np.int64(time.strftime('%Y%m%d%H%M', time.localtime(time.time()))) model.save('./VGG16'+str(model_id)+'.h6')
保存模型及結(jié)構(gòu)圖
from keras.utils import plot_model model.save('/opt/Data1/lixiang/letter_recognition/models/VGG16'+str(model_id)+'.h6') plot_model(model, to_file='/opt/Data1/lixiang/letter_recognition/models/VGG16'+str(model_id)+'.png')
繪制訓(xùn)練過(guò)程曲線
import matplotlib.pyplot as plt fig = plt.figure()#新建一張圖 plt.plot(history.history['acc'],label='training acc') plt.plot(history.history['val_acc'],label='val acc') plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(loc='lower right') fig.savefig('VGG16'+str(model_id)+'acc.png') fig = plt.figure() plt.plot(history.history['loss'],label='training loss') plt.plot(history.history['val_loss'], label='val loss') plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(loc='upper right') fig.savefig('VGG16'+str(model_id)+'loss.png')
文件記錄最終訓(xùn)練結(jié)果
logFilePath = './log.txt' fobj = open(logFilePath, 'a') fobj.write('model id: ' + str(model_id)+'\n') fobj.write('epoch: '+ str(epochs) +'\n') fobj.write('x_train shape: ' + str(X_train.shape) + '\n') fobj.write('x_test shape: ' + str(X_test.shape)+'\n') fobj.write('training accuracy: ' + str(history.history['acc'][-1]) + '\n') fobj.write('model evaluation results: ' + str(score[0]) + ' ' +str(score[-1])+'\n') fobj.write('---------------------------------------------------------------------------\n') fobj.write('\n') fobj.close()
以字典格式保存訓(xùn)練中間過(guò)程
import pickle file = open('./models/history.pkl', 'wb') pickle.dump(history.history, file) file.close()
以上是“keras中深度模型訓(xùn)練的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。