0

Currently, this is the code I have. I know the problem is in the checkpoint callback because I've tested running my program without it and it works fine.

model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

class_weight = {0: 70.,
                1: 110.,
                2: 82.,
                3: 17.,
                4: 33.,
                5: 1.,
                6: 1.,
                7: 9.}

lr_schedule = tf.keras.callbacks.LearningRateScheduler(lr_schedule)

checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath='Checkpoint/model.{epoch:02d}-{val_loss:.4f}.h5', 
                                               save_weights_only=True,
                                                save_best_only=True,monitor='loss',
                                                mode='min',verbose=1) 

model.fit(train_generator, epochs = 5, steps_per_epoch=10, callbacks=[lr_schedule, checkpoint], class_weight=(class_weight))  

In a folder named tests I have the pyton file and another folder called Checkpoint. I want to save my model inside the folder named Checkpoint but I've tried different name and all give me the error below

Error: 'Failed to format this callback filepath: "Checkpoint/model.{epoch:02d}-{val_loss:.4f}.h5". Reason: \'val_loss\'

How can I make the ModelCheckpoint work correctly?

EDIT: I am not using validation data because my monitor is based on the loss function. I have updated my version of tensorflow by doing activate <name_of_env> followed by pip install tensorflow --upgrade. After this I ran it again and I still get the same error.

I was wondering, do I have to have something inside the Checkpoint folder for it to work?

Bia
  • 305
  • 3
  • 10
  • What's your `tf` version. I couldn't reproduce your issue in `tf 2.4`. It works fine for me. – Innat Mar 18 '21 at 19:59
  • Does this answer your question? [KeyError: Failed to format this callback filepath](https://stackoverflow.com/questions/61049830/keyerror-failed-to-format-this-callback-filepath) – Innat Mar 18 '21 at 20:01
  • My version is 2.3. I'll try installing the 2.4 – Bia Mar 18 '21 at 20:02
  • @M.Innat it doesn't because the problem is not on the monitor variable but in the filepath variable – Bia Mar 18 '21 at 20:05

1 Answers1

0

I solved it. I just changed the filepath from 'Checkpoint/model.{epoch:02d}-{val_loss:.4f}.h5' to 'Checkpoint/random.h5' and it worked.

Bia
  • 305
  • 3
  • 10