keras/keras_core/callbacks/history.py

43 lines
1.3 KiB
Python
Raw Normal View History

2023-04-17 21:55:41 +00:00
from keras_core.api_export import keras_core_export
from keras_core.callbacks.callback import Callback
@keras_core_export("keras_core.callbacks.History")
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.
Example:
>>> model = Sequential([layers.Dense(10)])
>>> model.compile(SGD(), loss='mse')
>>> history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5),
... epochs=10, verbose=1)
>>> print(history.params)
{'verbose': 1, 'epochs': 10, 'steps': 1}
>>> # check the keys of history object
>>> print(history.history.keys())
dict_keys(['loss'])
"""
def __init__(self):
super().__init__()
self.history = {}
def on_train_begin(self, logs=None):
self.epoch = []
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)
# Set the history attribute on the model after the epoch ends. This will
# make sure that the state which is set is the latest one.
self.model.history = self