解决KeyError: ‘acc‘ 和KeyError: ‘val_acc‘错误

问题描述:

程序运行出现KeyError: ‘acc’ 和KeyError: 'val_acc’的错误。

解决方法:

在Keras 2.3.0中,矩阵的报告方式已更改为与指定的确切名称相匹配。 如果您使用的是较旧的代码或较旧的代码示例,则可能会遇到错误。 下面是解决方法。

您是否一直在使用Keras的fit()函数返回的“历史记录”对象来绘制或可视化模型的训练历史? 自最近Keras升级以来,您是否一直收到诸如以下的“ KeyError”类型错误,并想知道为什么?

Traceback (most recent call last):
  File "lenet_mnist_keras.py", line 163, in <module>
    graph_training_history(history)
  File "lenet_mnist_keras.py", line 87, in graph_training_history
    plt.plot(history.history['acc'])
KeyError: 'acc'
Traceback (most recent call last):
  File "lenet_mnist_keras.py", line 163, in <module>
    graph_training_history(history)
  File "lenet_mnist_keras.py", line 88, in graph_training_history
    plt.plot(history.history['val_acc'])
KeyError: 'val_acc'

这是由于Keras 2.3.0版中引入的重大更改所致。
根据2.3.0发行说明:

“Metrics and losses are now reported under the exact name specified by the user (e.g. if you pass metrics=[‘acc’], your metric will be reported under the string “acc”, not “accuracy”, and inversely metrics=[‘accuracy’] will be reported under the string “accuracy”.”

您可以在此处阅读官方发行说明:https://github.com/keras-team/keras/releases/tag/2.3.0

这意味着如果在model.compile()中指定metrics = [“ accuracy”],则历史记录对象将具有键“ accuracy”和“ val_accuracy”。 如果您将其指定为metrics = [“ acc”],则将使用键“ acc”和“ val_acc”来报告它们。

因此,要纠正该错误,您应该在整个代码中使用一种标准。
您可以使用“ acc”,

...

model.compile(loss="categorical_crossentropy",
              optimizer=opt, metrics=["acc"])
 
...
 
plt.figure(1)

# summarize history for accuracy

plt.subplot(211)
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Training', 'Validation'], loc='lower right')

# summarize history for loss

plt.subplot(212)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Training', 'Validation'], loc='upper right')

plt.tight_layout()

plt.show()

或者使用"accuracy"

...

model.compile(loss="categorical_crossentropy",
              optimizer=opt, metrics=["accuracy"])
 
...
 
plt.figure(1)

# summarize history for accuracy

plt.subplot(211)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Training', 'Validation'], loc='lower right')

# summarize history for loss

plt.subplot(212)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Training', 'Validation'], loc='upper right')

plt.tight_layout()

plt.show()

只需记住在metrics = […]和您从历史对象访问密钥的地方都使用相同的密钥。

相关链接:https://www.codesofinterest.com/2017/03/graph-model-training-history-keras.html

原文链接:https://towardsdatascience.com/fixing-the-keyerror-acc-and-keyerror-val-acc-errors-in-keras-2-3-x-or-newer-b29b52609af9

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

毕加猪plus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值