KeyError:“ val_acc”

时间:2019-09-27 17:17:44

标签: python keras image-recognition

下面是model.fit_generator()函数的一些参数。这些对象保存在标有回调的列表中。

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]

在创建回调对象和对象的参数之后,我实现了图层并进行了编译(未显示,因为它与我遇到的问题无关)。然后,我运行model.fit_generator函数(使用上面的回调参数):

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)

我得到的错误是:

KeyError: 'val_acc'

据我了解,这意味着val_acc不在列表中。但这是..因此需要帮助来了解为什么我会收到此错误。

编辑:

错误显示前的结果图片。[https://i.stack.imgur.com/5lheg.png]

1 个答案:

答案 0 :(得分:1)

您需要将monitor= 'val_acc'更改为monitor='val_loss'

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