简体   繁体   中英

KeyError: 'val_acc'

Below are some parameters for the model.fit_generator() function. Theses objects are saved in a list labeled callbacks.

checkpoint = ModelCheckpoint(
        model_file, 
        monitor= 'val_acc', 
        save_best_only=True)

early_stopping = EarlyStopping(
    monitor='val_loss',
    patience=5,
    verbose=1,
    restore_best_weights=True)


tensorboard = TensorBoard(
    log_dir=log_dir,
    batch_size=batch_size,
    update_freq = 'batch')


reduce_lr = ReduceLROnPlateau(
    monitor='val_loss',
    patience=5,
    cooldown=2,
    min_lr=0.0000000001,
    verbose=1)


#-----------------------------------------------------------------------------------------------------------------#
callbacks = [checkpoint, reduce_lr, early_stopping, tensorboard]

After creating the callback objects and parameters for the objects, I implement the layers and compile(which is not shown because it is irrelevant to the problem I am having). Then I run the model.fit_generator function (which uses the callback arguments above):

history = model.fit_generator(
    train_generator,
    steps_per_epoch = steps_per_epoch,
    epochs=epochs,
    verbose=2,
    callbacks=callbacks,
    validation_data=validation_generator,
    validation_steps=validation_steps,
    class_weight=class_weight)

The error that I am getting is:

KeyError: 'val_acc'

From my understanding this means that val_acc is not in the list. But it is.. so need help to understanding why I am getting this error.

Edit:

Picture of the result before the error shows..[ https://i.stack.imgur.com/5lheg.png]

You need to change monitor= 'val_acc' to monitor='val_loss'

checkpoint = ModelCheckpoint(
        model_file, 
        monitor='val_loss', 
        save_best_only=True)

Make sure you have used model.compile(metrics=['accuracy']) , not Accuracy or acc . Also in filepath use val_accuracy . I have recently faced this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM