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

  介绍

这篇文章主要介绍keras中深度模型训练的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

<强>记录训练过程

=model.fit历史(X_train, Y_train,,时代=时代,batch_size=batch_size validation_split=0.1)

将训练过程记录在历史中

<>强利用时间记录模型

import 时间   时间=model_id  np.int64 (time.strftime (& # 39; % Y % m % d % H %猴# 39;,,time.localtime (time.time ())))   model.save (& # 39;。/VGG16& # 39; + str (model_id) + & # 39; .h6& # 39;)

<强>保存模型及结构图

得到keras.utils  import  plot_model   model.save (& # 39;/opt/Data1/李翔/letter_recognition/模型/VGG16& # 39; + str (model_id) + & # 39; .h6& # 39;)   plot_model(模型,to_file=& # 39;/opt/Data1/李翔/letter_recognition/模型/VGG16& # 39; + str (model_id) + & # 39; . png # 39;)

<强>绘制训练过程曲线

import  matplotlib.pyplot  as  plt   时间=fig  plt.figure() #新建一张图   plt.plot (history.history [& # 39; acc # 39;],标签=& # 39;training  acc # 39;)   plt.plot (history.history [& # 39; val_acc& # 39;],标签=& # 39;val  acc # 39;)   plt.title (& # 39; model 准确性# 39;)   plt.ylabel(& # 39;准确性# 39;)   plt.xlabel(& # 39;时代# 39;)   plt.legend (loc=& # 39; lower 对# 39;)   fig.savefig (& # 39; VGG16& # 39; + str (model_id) + & # 39; acc.png& # 39;)   时间=fig  plt.figure ()   plt.plot (history.history[& # 39;损失# 39;],标签=& # 39;training 损失# 39;)   plt.plot (history.history [& # 39; val_loss& # 39;],,标签=& # 39;val 损失# 39;)   plt.title (& # 39; model 损失# 39;)   plt.ylabel(& # 39;损失# 39;)   plt.xlabel(& # 39;时代# 39;)   plt.legend (loc=& # 39; upper 对# 39;)   fig.savefig (& # 39; VGG16& # 39; + str (model_id) + & # 39; loss.png& # 39;)

<强>文件记录最终训练结果

logFilePath =, & # 39;。/log.txt& # 39;=fobj 开放(logFilePath, & # 39;一个# 39;)   fobj.write (& # 39; model  id:, & # 39;, +, str (model_id) + & # 39; \ n # 39;)   fobj.write(& # 39;时代:& # 39;+,str(时代),+ & # 39;\ n # 39;)   fobj.write (& # 39; x_train 形状:,& # 39;,+,str (X_train.shape), +, & # 39; \ n # 39;)   fobj.write (& # 39; x_test 形状:,& # 39;,+,str (X_test.shape) + & # 39; \ n # 39;)   fobj.write (& # 39; training 准确性:,& # 39;,+,str (history.history [& # 39; acc # 39;] [1]), +, & # 39; \ n # 39;)   fobj.write (& # 39; model  evaluation 结果:,& # 39;,+,str(得分[0]),+,& # 39;,& # 39;,+ str(得分[1])+ & # 39;\ n # 39;)   fobj.write (& # 39; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n # 39;)   fobj.write (& # 39; \ n # 39;)   fobj.close ()

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

import 困境   时间=file 开放(& # 39;。/模型/history.pkl& # 39;,, & # 39; wb # 39;)   pickle.dump (history.history,文件)   file.close ()

以上是“keras中深度模型训练的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

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