问题
I was trying to plot train and test learning curve in keras, however, the following code produces KeyError: 'val_acc error
.
The official document <https://keras.io/callbacks/>
states that in order to use 'val_acc'
I need to enable validation and accuracy monitoring which I dont understand and dont know how to use in my code.
Any help would be much appreciated. Thanks.
seed = 7
np.random.seed(seed)
dataframe = pandas.read_csv("iris.csv", header=None)
dataset = dataframe.values
X = dataset[:,0:4].astype(float)
Y = dataset[:,4]
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
dummy_y = np_utils.to_categorical(encoded_Y)
kfold = StratifiedKFold(y=Y, n_folds=10, shuffle=True, random_state=seed)
cvscores = []
for i, (train, test) in enumerate(kfold):
model = Sequential()
model.add(Dense(12, input_dim=4, init='uniform', activation='relu'))
model.add(Dense(3, init='uniform', activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history=model.fit(X[train], dummy_y[train], nb_epoch=200, batch_size=5, verbose=0)
scores = model.evaluate(X[test], dummy_y[test], verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
cvscores.append(scores[1] * 100)
print( "%.2f%% (+/- %.2f%%)" % (np.mean(cvscores), np.std(cvscores)))
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
回答1:
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!
回答2:
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
回答3:
to get any val_* data (val_acc
, val_loss
, ...), you need to first set the validation.
first method (will validate from what you give it):
model.fit(validation_data=(X_test, Y_test))
second method (will validate from a part of the training data):
model.fit(validation_split=0.5)
回答4:
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.
回答5:
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.
回答6:
Looks like in Keras + Tensorflow 2.0 val_acc
was renamed to val_accuracy
来源:https://stackoverflow.com/questions/39883331/plotting-learning-curve-in-keras-gives-keyerror-val-acc