Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix ' KeyError: 'accuracy' ' when running flowers_tf_lite.ipynb?

When I run the flowers_tf_lite.ipynb script.

Link to flowers_tf_lite.ipynb script

The getting KeyError: 'accuracy' error stopped the program.

How can I solve this type of error?

acc = history.history['accuracy']

val_acc = history.history['val_accuracy']

loss = history.history['loss']

val_loss = history.history['val_loss']

plt.figure(figsize=(8, 8))

plt.subplot(2, 1, 1)

plt.plot(acc, label='Training Accuracy')

plt.plot(val_acc, label='Validation Accuracy')

plt.legend(loc='lower right')

plt.ylabel('Accuracy')

plt.ylim([min(plt.ylim()),1])

plt.title('Training and Validation Accuracy')


plt.subplot(2, 1, 2)

plt.plot(loss, label='Training Loss')

plt.plot(val_loss, label='Validation Loss')

plt.legend(loc='upper right')

plt.ylabel('Cross Entropy')

plt.ylim([0,1.0])

plt.title('Training and Validation Loss')

plt.xlabel('epoch')

plt.show()

Img of KeyError: 'accuracy' error

like image 804
mavericks Avatar asked Sep 04 '19 06:09

mavericks


1 Answers

The accuracy metric is named as 'acc' and 'val_acc' after model compile, you can check that using

model.metrics_names

which will give you

['loss', 'acc', 'val_acc']

So you just need to change first 2 lines is above code

acc = history.history['acc']
val_acc = history.history['val_acc']
like image 121
Nishant Patel Avatar answered Sep 22 '22 14:09

Nishant Patel