栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

浅谈keras的深度模型训练过程及结果记录方式

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

浅谈keras的深度模型训练过程及结果记录方式

记录训练过程

history=model.fit(X_train, Y_train, epochs=epochs,batch_size=batch_size,validation_split=0.1)

将训练过程记录在history中

利用时间记录模型

import time
model_id = np.int64(time.strftime('%Y%m%d%H%M', time.localtime(time.time())))
model.save('./VGG16'+str(model_id)+'.h5')

保存模型及结构图

from keras.utils import plot_model
model.save('/opt/Data1/lixiang/letter_recognition/models/VGG16'+str(model_id)+'.h5')
plot_model(model, to_file='/opt/Data1/lixiang/letter_recognition/models/VGG16'+str(model_id)+'.png')

绘制训练过程曲线

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')

文件记录最终训练结果

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()

以字典格式保存训练中间过程

import pickle
file = open('./models/history.pkl', 'wb')
pickle.dump(history.history, file)
file.close()

以上这篇浅谈keras的深度模型训练过程及结果记录方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/21793.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号