1

I am writing a custom CycleGan training loop following TF's documentation. I'd like to use several existing callbacks, and prefer not re-writing their logic.

I've found this SO question, whose answers unfortunately focused on EarlyStopping, rather than on the broad range of callbacks.

I further found this reddit post, which suggested to call them manually. Several callbacks however work with an internal model object (they call self.model, which is the model they are applied on).

How can I simply use the existing callbacks in a custom training loop? I appreciate any code outlines or further recommendations!

emil
  • 194
  • 1
  • 11
  • How about official [documentation](https://www.tensorflow.org/guide/keras/custom_callback)? – mmrbulbul Apr 01 '21 at 09:32
  • If you prefer using built-in `callbacks` in a custom training loop, then you can't. You have to write the logic by yourself. – Innat Apr 01 '21 at 11:04
  • However, I would advise using `.fit` by customizing it - override the `train_step`... In that way, we can use all the convenience functionalities of the fit method. – Innat Apr 01 '21 at 11:12

1 Answers1

0

If you want to use the existing callbacks you have to create the callbacks you want and pass them to callbacks argument while using model.fit().

For example, i want to use the ModelCheckpoint and EarlyStopping

checkpoint = keras.callbacks.ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
es = keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=1)

Create a callbacks_list

callbacks_list = [checkpoint, es]

pass the callbacks_list to callbacks argument while training the model.

model.fit(x_train,y_train,validation_data=(x_test, y_test),epochs=10,callbacks=callbacks_list)

Please refer to this gist for complete code example. Thank You.