Skip to content

Commit

Permalink
Avoid double key lookup on callback.py (#3018)
Browse files Browse the repository at this point in the history
On method on_epoch_end, to add new keys to the history dict, first it is
verified if a key is not on the history dict and if that is the case, a new key
is created on the history dict with an empty list as value.

However, this operation search for a key twice in the dict. This same behavior
can be achieved in a single step using dict setdefault method.
  • Loading branch information
lucasmoura authored and fchollet committed Jun 19, 2016
1 parent b235f91 commit b6ca3ef
Showing 1 changed file with 1 addition and 4 deletions.
5 changes: 1 addition & 4 deletions keras/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ def on_train_begin(self, logs={}):
def on_epoch_end(self, epoch, logs={}):
self.epoch.append(epoch)
for k, v in logs.items():
if k not in self.history:
self.history[k] = []
self.history[k].append(v)

self.history.setdefault(k, []).append(v)

class ModelCheckpoint(Callback):
'''Save the model after every epoch.
Expand Down

0 comments on commit b6ca3ef

Please sign in to comment.