|
| 1 | +"""Callbacks: utilities called at certain points during model training. |
| 2 | +""" |
1 | 3 | from __future__ import absolute_import |
2 | 4 | from __future__ import division |
3 | 5 | from __future__ import print_function |
@@ -815,11 +817,12 @@ class ReduceLROnPlateau(Callback): |
815 | 817 | of epochs, the learning rate is reduced. |
816 | 818 |
|
817 | 819 | # Example |
818 | | - ```python |
819 | | - reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, |
820 | | - patience=5, min_lr=0.001) |
821 | | - model.fit(X_train, Y_train, callbacks=[reduce_lr]) |
822 | | - ``` |
| 820 | +
|
| 821 | + ```python |
| 822 | + reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, |
| 823 | + patience=5, min_lr=0.001) |
| 824 | + model.fit(X_train, Y_train, callbacks=[reduce_lr]) |
| 825 | + ``` |
823 | 826 |
|
824 | 827 | # Arguments |
825 | 828 | monitor: quantity to be monitored. |
@@ -928,10 +931,11 @@ class CSVLogger(Callback): |
928 | 931 | including 1D iterables such as np.ndarray. |
929 | 932 |
|
930 | 933 | # Example |
931 | | - ```python |
932 | | - csv_logger = CSVLogger('training.log') |
933 | | - model.fit(X_train, Y_train, callbacks=[csv_logger]) |
934 | | - ``` |
| 934 | +
|
| 935 | + ```python |
| 936 | + csv_logger = CSVLogger('training.log') |
| 937 | + model.fit(X_train, Y_train, callbacks=[csv_logger]) |
| 938 | + ``` |
935 | 939 |
|
936 | 940 | # Arguments |
937 | 941 | filename: filename of the csv file, e.g. 'run/log.csv'. |
@@ -1020,32 +1024,33 @@ class LambdaCallback(Callback): |
1020 | 1024 | on_train_end: called at the end of model training. |
1021 | 1025 |
|
1022 | 1026 | # Example |
1023 | | - ```python |
1024 | | - # Print the batch number at the beginning of every batch. |
1025 | | - batch_print_callback = LambdaCallback( |
1026 | | - on_batch_begin=lambda batch,logs: print(batch)) |
1027 | | -
|
1028 | | - # Stream the epoch loss to a file in JSON format. The file content |
1029 | | - # is not well-formed JSON but rather has a JSON object per line. |
1030 | | - import json |
1031 | | - json_log = open('loss_log.json', mode='wt', buffering=1) |
1032 | | - json_logging_callback = LambdaCallback( |
1033 | | - on_epoch_end=lambda epoch, logs: json_log.write( |
1034 | | - json.dumps({'epoch': epoch, 'loss': logs['loss']}) + '\n'), |
1035 | | - on_train_end=lambda logs: json_log.close() |
1036 | | - ) |
1037 | | -
|
1038 | | - # Terminate some processes after having finished model training. |
1039 | | - processes = ... |
1040 | | - cleanup_callback = LambdaCallback( |
1041 | | - on_train_end=lambda logs: [ |
1042 | | - p.terminate() for p in processes if p.is_alive()]) |
1043 | | -
|
1044 | | - model.fit(..., |
1045 | | - callbacks=[batch_print_callback, |
1046 | | - json_logging_callback, |
1047 | | - cleanup_callback]) |
1048 | | - ``` |
| 1027 | +
|
| 1028 | + ```python |
| 1029 | + # Print the batch number at the beginning of every batch. |
| 1030 | + batch_print_callback = LambdaCallback( |
| 1031 | + on_batch_begin=lambda batch,logs: print(batch)) |
| 1032 | +
|
| 1033 | + # Stream the epoch loss to a file in JSON format. The file content |
| 1034 | + # is not well-formed JSON but rather has a JSON object per line. |
| 1035 | + import json |
| 1036 | + json_log = open('loss_log.json', mode='wt', buffering=1) |
| 1037 | + json_logging_callback = LambdaCallback( |
| 1038 | + on_epoch_end=lambda epoch, logs: json_log.write( |
| 1039 | + json.dumps({'epoch': epoch, 'loss': logs['loss']}) + '\n'), |
| 1040 | + on_train_end=lambda logs: json_log.close() |
| 1041 | + ) |
| 1042 | +
|
| 1043 | + # Terminate some processes after having finished model training. |
| 1044 | + processes = ... |
| 1045 | + cleanup_callback = LambdaCallback( |
| 1046 | + on_train_end=lambda logs: [ |
| 1047 | + p.terminate() for p in processes if p.is_alive()]) |
| 1048 | +
|
| 1049 | + model.fit(..., |
| 1050 | + callbacks=[batch_print_callback, |
| 1051 | + json_logging_callback, |
| 1052 | + cleanup_callback]) |
| 1053 | + ``` |
1049 | 1054 | """ |
1050 | 1055 |
|
1051 | 1056 | def __init__(self, |
|
0 commit comments