训练模型时出现错误提醒:KeyError: ‘val_acc‘该怎么解决

运行代码时报错的问题是:KeyError: 'val_acc'

在用keras训练深度神经网络模型的时候出现如下问题的原因:

 File "D:\anaconda\lib\site-packages\tensorflow\python\keras\callbacks.py", line 1330, in _get_file_path
    file_path = self.filepath.format(epoch=epoch + 1, **logs)
KeyError: 'val_acc'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/Emotion Recognition/train_emotion_classifier.py", line 70, in 
    model.fit_generator(data_generator.flow(xtrain, ytrain, batch_size),
  File "D:\anaconda\lib\site-packages\tensorflow\python\util\deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "D:\anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1815, in fit_generator
    return self.fit(
  File "D:\anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "D:\anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1137, in fit
    callbacks.on_epoch_end(epoch, epoch_logs)
  File "D:\anaconda\lib\site-packages\tensorflow\python\keras\callbacks.py", line 412, in on_epoch_end
    callback.on_epoch_end(epoch, logs)
  File "D:\anaconda\lib\site-packages\tensorflow\python\keras\callbacks.py", line 1249, in on_epoch_end
    self._save_model(epoch=epoch, logs=logs)
  File "D:\anaconda\lib\site-packages\tensorflow\python\keras\callbacks.py", line 1282, in _save_model
    filepath = self._get_file_path(epoch, logs)
  File "D:\anaconda\lib\site-packages\tensorflow\python\keras\callbacks.py", line 1332, in _get_file_path
    raise KeyError('Failed to format this callback filepath: "{}". '
KeyError: 'Failed to format this callback filepath: "models/_mini_XCEPTION.{epoch:02d}-{val_acc:.2f}.hdf5". Reason: \'val_acc\''

解决方案:修改参数 

base_path = 'models/'  
trained_models_path = base_path + '_mini_XCEPTION'
model_names = trained_models_path + '.{epoch:02d}-{val_acc:.2f}.hdf5'
model_checkpoint = ModelCheckpoint(model_names,               
                                   'val_loss', verbose=1,
                                   save_best_only=True)  

把上面代码中的'.{epoch:02d}-{val_acc:.2f}.hdf5'改成'.{epoch:02d}-accuracy{accuracy:.2f}.hdf5'即可解决问题。

你可能感兴趣的:(keras训练模型,python,神经网络)