SoFunction
Updated on 2024-11-13

History object usage in keras

Both the fit_generator and fit functions in keras return History objects, so how does History work? In fact the History object already records the run output. Before we know it, we even define our own callback functions to record loss, accuracy, etc.

The relevant keras source code is located atweb address

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):
   = []
   = {}

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

it can be seenThe History class object contains two properties, epoch and history, with epoch being the number of training rounds.

Depending on the compile parameter metrics, history contains different content. For example, when the metrics=['accuracy'] at a certain time, running the following part of the code we can see that the history dictionary type, containing val_loss,val_acc,loss,acc four key values.

#### omit several
history = model.fit_generator(
  mp.train_flow,
  steps_per_epoch=32,
  epochs=3,
  validation_data=mp.test_flow,
  validation_steps=32)
print()
print()

print(['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]

Additional knowledge:Quick find history command in ipython using %history

1、Output all history records with serial numbers

 %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, by serial number, find some serial number interval of the history of records

 %history -n 168-170 178 185-190
 
 168: planets
 169:
for method, group in ('method'):
 print(f'{method:30s} method={group}')
 170:
for method, group in ('method'):
 print(f'{method:30s} method={}')
 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、Fuzzy search

 %history -g print*metho*

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

This above History object usage in keras is all I have to share with you, I hope it will give you a reference, and I hope you will support me more.