Skip to content

Commit

Permalink
Replace plot_loss_callback example with json_logging_callback (keras-…
Browse files Browse the repository at this point in the history
…team#4116) (keras-team#6941)

The original plotting example doesn't work and a working version is somewhat too involved for a lambda.
  • Loading branch information
melentye authored and fchollet committed Jun 18, 2017
1 parent ff45159 commit d852c2d
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions keras/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ def on_train_end(self, logs=None):


class LambdaCallback(Callback):
"""Callback for creating simple, custom callbacks on-the-fly.
r"""Callback for creating simple, custom callbacks on-the-fly.
This callback is constructed with anonymous functions that will be called
at the appropriate time. Note that the callbacks expects positional
Expand All @@ -1003,12 +1003,15 @@ class LambdaCallback(Callback):
batch_print_callback = LambdaCallback(
on_batch_begin=lambda batch,logs: print(batch))
# Plot the loss after every epoch.
import numpy as np
import matplotlib.pyplot as plt
plot_loss_callback = LambdaCallback(
on_epoch_end=lambda epoch, logs: plt.plot(np.arange(epoch),
logs['loss']))
# Stream the epoch loss to a file in JSON format. The file content
# is not well-formed JSON but rather has a JSON object per line.
import json
json_log = open('loss_log.json', mode='wt', buffering=1)
json_logging_callback = LambdaCallback(
on_epoch_end=lambda epoch, logs: json_log.write(
json.dumps({'epoch': epoch, 'loss': logs['loss']}) + '\n'),
on_train_end=lambda logs: json_log.close()
)
# Terminate some processes after having finished model training.
processes = ...
Expand All @@ -1018,7 +1021,7 @@ class LambdaCallback(Callback):
model.fit(...,
callbacks=[batch_print_callback,
plot_loss_callback,
json_logging_callback,
cleanup_callback])
```
"""
Expand Down

0 comments on commit d852c2d

Please sign in to comment.