keras中的History对象用法

所属分类: 脚本专栏 / python 阅读数: 1242
收藏 0 赞 0 分享

keras中的fit_generator和fit函数均返回History对象,那么History怎么用呢?事实上History对象已经记录了运行输出。在了解之前,我们甚至自己定义回调函数记录损失和准确率等。

相关keras源码位于网址

class History(Callback):
 """Callback that records events into a `History` object.
 This callback is automatically applied to
 every Keras model. The `History` object
 gets returned by the `fit` method of models.
 """

 def on_train_begin(self, logs=None):
  self.epoch = []
  self.history = {}

 def on_epoch_end(self, epoch, logs=None):
  logs = logs or {}
  self.epoch.append(epoch)
  for k, v in logs.items():
   self.history.setdefault(k, []).append(v)

可以看出History类对象包含两个属性,分别为epoch和history,epoch为训练轮数。

根据compile参数metrics,history包含不同的内容。比如,当某一次metrics=['accuracy']时,运行如下部分代码我们可以看出,history字典类型,包含val_loss,val_acc,loss,acc四个key值。

####省略若干
history = model.fit_generator(
  mp.train_flow,
  steps_per_epoch=32,
  epochs=3,
  validation_data=mp.test_flow,
  validation_steps=32)
print(history.history)
print(history.epoch)

print(history.history['val_loss'])

{‘val_loss': [0.4231100323200226, 0.3713115310668945, 0.3836631367206573], ‘val_acc': [0.815, 0.84, 0.83], ‘loss': [0.8348453622311354, 0.5010451343324449, 0.4296100065112114], ‘acc': [0.630859375, 0.7509920634920635, 0.783203125]}
[0, 1, 2]
[0.4231100323200226, 0.3713115310668945, 0.3836631367206573]

补充知识:在ipython中使用%history快速查找历史命令

1、输出所有历史记录,且带有序号

 %history -n

 150: %cpaste
 151: %cpaste
 152: print(r">>>>>>>>>")
 153: print(r'>>>>>>>>>')
 154: print(r'>>>>>>>>><')
 155: print(r'>')
 156: print(r'>>')
 157: print(r'>>>')
 ...

2、按序号,查找某些序号区间的历史纪录

 %history -n 168-170 178 185-190
 
 168: planets
 169:
for method, group in planets.groupby('method'):
 print(f'{method:30s} method={group}')
 170:
for method, group in planets.groupby('method'):
 print(f'{method:30s} method={group.shape}')
 178: %history?
 185: %history -u
 186: %history -n -u
 187: ?%history
 188: %history -g method
 189: %history -g method print
 190: %history -g for method,

3、模糊查找

 %history -g print*metho*

 120:
for method, group in planets.groupby('method'):
 print(f"{method:30s} shape={groupe.shape}")
 121:
for method, group in planets.groupby('method'):
 print(f"{method:30s} shape={group.shape}")
 169:
for method, group in planets.groupby('method'):
 print(f'{method:30s} method={group}')
 170:
for method, group in planets.groupby('method'):
 print(f'{method:30s} method={group.shape}')
 182:
for method, group in planets.groupby('method'):
  print(f"{method:30s shape=group.shape}")
 198: %history -g print*metho*

以上这篇keras中的History对象用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

python打印直角三角形与等腰三角形实例代码

这篇文章主要给大家介绍了关于python打印直角三角形与等腰三角形的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

基于python解线性矩阵方程(numpy中的matrix类)

这篇文章主要介绍了基于python解线性矩阵方程(numpy中的matrix类),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

vim自动补全插件YouCompleteMe(YCM)安装过程解析

这篇文章主要介绍了vim自动补全插件YouCompleteMe(YCM)安装过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python中@property和property函数常见使用方法示例

这篇文章主要介绍了python中@property和property函数常见使用方法,结合实例形式分析了Python @property和property函数功能、使用方法及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

python中bytes和str类型的区别

这篇文章主要介绍了python中bytes和str类型的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解python中*号的用法

这篇文章主要介绍了python中*号的用法,文中通过代码给大家介绍了双星号(**)的用法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python超时重新请求解决方案

这篇文章主要介绍了python超时重新请求解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python多继承(钻石继承)问题和解决方法简单示例

这篇文章主要介绍了python多继承(钻石继承)问题和解决方法,结合实例形式分析了Python多继承调用父类初始化方法相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Python解密仿射密码

这篇文章主要介绍了基于Python解密仿射密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Python实现船舶的MMSI的获取(推荐)

工作中遇到一个需求,需要通过网站查询船舶名称得到MMSI码,网站来自船讯网。这篇文章主要介绍了基于Python实现船舶的MMSI的获取,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多