Plotting learning curve in keras gives KeyError: 'val_acc'

前端 未结 8 2018
情话喂你
情话喂你 2020-12-09 09:54

I was trying to plot train and test learning curve in keras, however, the following code produces KeyError: \'val_acc error.

The official document

相关标签:
8条回答
  • 2020-12-09 10:29

    You may need to enable the validation split of your trainset. Usually, the validation happens in 1/3 of the trainset. In your code, make the change as given below:

    history=model.fit(X[train], dummy_y[train],validation_split=0.33,nb_epoch=200, batch_size=5, verbose=0) 
    

    It works!

    0 讨论(0)
  • 2020-12-09 10:30

    The main point everyone misses to mention is that this Key Error is related to the naming of metrics during model.compile(...). You need to be consistent with the way you name your accuracy metric inside model.compile(....,metrics=['<metric name>']). Your history callback object will receive the dictionary containing key-value pairs as defined in metrics.

    So, if your metric is metrics=['acc'], you can access them in history object with history.history['acc'] but if you define metric as metrics=['accuracy'], you need to change to history.history['accuracy'] to access the value, in order to avoid Key Error. I hope it helps.

    N.B. Here's a link to the metrics you can use in Keras.

    0 讨论(0)
  • 2020-12-09 10:36

    This error also happens when you specify the validation_data=(X_test, Y_test) and your X_test and/or Y_test are empty. To check this, print the shape of X_test and Y_test respectively. In this case, the model.fit(validation_data=(X_test, Y_test), ...) method ran but because the validation set was empty, it didn't create a dictionary key for val_loss in the history.history dictionary.

    0 讨论(0)
  • 2020-12-09 10:43

    If you upgrade keras older version (e.g. 2.2.5) to 2.3.0 (or newer) which is compatible with Tensorflow 2.0, you might have such error (e.g. KeyError: 'acc'). Both acc and val_acc has been renamed to accuracy and val_accuracy respectively. Renaming them in script will solve the issue.

    0 讨论(0)
  • 2020-12-09 10:47

    Looks like in Keras + Tensorflow 2.0 val_acc was renamed to val_accuracy

    0 讨论(0)
  • 2020-12-09 10:51
    history_dict = history.history
    print(history_dict.keys())
    

    if u print keys of history_dict, you will get like this dict_keys(['loss', 'acc', 'val_loss', 'val_acc']).

    and edit a code like this

    acc = history_dict['acc']
    val_acc = history_dict['val_acc']
    loss = history_dict['loss']
    val_loss = history_dict['val_loss']
    

    Keys and error

    0 讨论(0)
提交回复
热议问题