Вы можете передать объект callback методу model.fit()
и затем выполнять действия на разных этапах во время подгонки.
https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/Callback
class MyCustomCallback(tf.keras.callbacks.Callback):
def on_train_batch_begin(self, batch, logs=None):
print('Training: batch {} begins at {}'.format(batch, datetime.datetime.now().time()))
def on_train_batch_end(self, batch, logs=None):
print('Training: batch {} ends at {}'.format(batch, datetime.datetime.now().time()))
def on_test_batch_begin(self, batch, logs=None):
print('Evaluating: batch {} begins at {}'.format(batch, datetime.datetime.now().time()))
def on_test_batch_end(self, batch, logs=None):
print('Evaluating: batch {} ends at {}'.format(batch, datetime.datetime.now().time()))
model = get_model()
model.fit(x_train, y_train, callbacks=[MyCustomCallback()])
https://www.tensorflow.org/guide/keras/custom_callback