Callback in Tensorflow

Viewed 959

In Keras we can add callback simply as below:

self.model.fit(X_train,y_train,callbacks=[Custom_callback])

The callback is defined in doc, but I cannot find any example to use them. Could anyone tell me how to add custom callbacks into TensorFlow?

1 Answers

Here is an example of a loss logging callback I like to use instead of tensorboard. Note that it requires post-processing, which I actually prefer so I can calculate average validation loss for cross-validation:

class lossesLogger(tf.keras.callbacks.Callback):
    def __init__(self, fileName):
        self.fileName = fileName
        self.json_log = open(
            self.fileName +'.json',
            mode='w+',
            buffering=1
        )     
    def on_epoch_end(self, epoch, logs=None):
        self.json_log.write(
            json.dumps(
                'epoch {}: '.format(epoch) +
                str(logs)
            ) +
            '\n'
        )        
    def on_train_end(self, logs=None):
        self.json_log.close()

To use this, add it to your list of callbacks like in this example, where I have it at the end of a list of three:

callbacks = [ #allows analyzing output of replicates
        EarlyStopping(
            patience=epochsWithoutValidationLossDecrease,
            verbose=1
        ),
        ModelCheckpoint(
            os.path.join(
                os.getcwd(),
                'Latest_saved_model_{}.h5'.format(uniqueID)
            ), 
            verbose=1,
            save_best_only=True,
            save_weights_only=False
        ),
        lossesLogger(
            os.path.join(
                os.getcwd(),
                ('val_log_per_epoch_' + str(uniqueID))
            )
        )
    ]

Related