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']时,运