运行flowers_tf_lite.ipynb时如何修复'KeyError:'accuracy''?

时间:2019-09-04 06:53:10

标签: python tensorflow keyerror

当我运行flowers_tf_lite.ipynb脚本时。

Link to flowers_tf_lite.ipynb script

遇到KeyError: 'accuracy'错误时,程序停止运行。

如何解决此类错误?

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

1 个答案:

答案 0 :(得分:1)

模型编译后,精度指标分别命名为'acc'和'val_acc',您可以使用

进行确认
model.metrics_names

这将为您提供

  

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

所以您只需要在代码上方更改前两行

acc = history.history['acc']
val_acc = history.history['val_acc']