Keras adds a number after metric key

Viewed 20

I use tensoflow keras version 2.4.0 on Windows 11 and I want to add a ModelCheckpoint callback monitoring auc:

import tensorflow as tf

try: # Atttempt to get rid of any model, history from previous runs
    del model
    del history
except:
    print('No model to delete')

checkpoint_filepath = "checkpoint"
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_filepath,
    save_weights_only=True,
    monitor='val_auc',
    mode='max',
    save_best_only=True)

model.compile(optimizer=Adam(learning_rate=0.001),
    loss= 'binary_crossentropy',
    metrics=['accuracy', tf.keras.metrics.AUC()])

history = model.fit(..., callbacks=[model_checkpoint_callback])

Everything is fine since val_auc exists in the history.history.keys(). However, if I run the code again (in a Jupyter notebook) the next time the key becomes: val_auc_1. And of course ModelCheckpoint does not work. I have to restart the kernel to get rid of this annoying _1 at the end of the key.

One solution suggested by keras docs is to run model.fit just for 1 epoch to get the history.history.keys() and then use it in the ModelCheckpoint. But this is really clumsy. Is there a way to get the metric keys after model.compile without running model.fit? Or else is it possible to avoid this _1 somehow?

Thanks for your help!

0 Answers
Related