温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

keras中深度模型训练的示例分析

发布时间:2021-08-19 15:02:41 来源:亿速云 阅读:189 作者:小新 栏目:开发技术

这篇文章主要介绍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)+'.h6')

保存模型及结构图

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

绘制训练过程曲线

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中深度模型训练的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI