Issue with Custom Callback Not Triggering EarlyStopping in TensorFlow 2.12
I'm building a feature where I'm using TensorFlow 2.12 to train a model and have implemented a custom callback to monitor validation accuracy... Despite setting it up, it seems like the EarlyStopping callback is not triggering, even when the validation accuracy plateaus. Hereโs how Iโve configured my callbacks: ```python from tensorflow.keras.callbacks import EarlyStopping, Callback class CustomCallback(Callback): def on_epoch_end(self, epoch, logs=None): print(f'Epoch {epoch + 1}: val_accuracy = {logs.get('val_accuracy')}') # Define EarlyStopping callback early_stopping = EarlyStopping(monitor='val_accuracy', patience=5, restore_best_weights=True) custom_callback = CustomCallback() # Compile and fit the model model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit( train_dataset, validation_data=validation_dataset, epochs=50, callbacks=[early_stopping, custom_callback] ) ``` I'm expecting the EarlyStopping callback to halt training after 5 epochs of no improvement, but it seems to continue training even when the validation accuracy has not increased for several epochs. I've checked the logs and can confirm that the 'val_accuracy' is being printed correctly by my custom callback. However, thereโs no indication that the EarlyStopping condition is being evaluated correctly. Is there something I might be missing in the setup? I've also ensured that my validation data is correctly being passed to the fit method. Additionally, I've tried using the `monitor` parameter with different metrics like `val_loss` and `accuracy`, but the issue persists. Any insights would be appreciated! What's the correct way to implement this? Thanks for taking the time to read this! I'm using Python 3.9 in this project.