问题描述:

程序运行出现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_historyplt.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_historyplt.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 accuracyplt.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 lossplt.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 accuracyplt.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 lossplt.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

解决KeyError: ‘acc‘ 和KeyError: ‘val_acc‘错误相关推荐

  1. KeyError: ‘acc’的解决方法

    会出现KeyError: 'acc'.KeyError: 'val_acc'等报错信息,是因为keras库的版本不同所造成的. 在Keras 2.3.0需要将acc替换为 accuracy,同样的va ...

  2. Keyerror ‘acc‘ KeyError: ‘val_acc‘解决方法

    在使用keras时候报错Keyerror 'acc',这是一个keras版本问题,acc和accuracy本意是一样的,但是不同keras版本使用不同命名,因此需要更换.val_acc也是如此. 把a ...

  3. 【Python】可视化神经网络训练过程时处理报错 train_acc=hist.history[‘acc’] KeyError: ‘acc’

    train_acc=hist.history['acc'] KeyError: 'acc' 回看编译神经网络的时候,是这样写的: network.compile(loss="binary_c ...

  4. python3 日志检索异常抛出异常 raise KeyError(key),KeyError: ‘formatters‘

    python3 日志检索异常抛出异常 raise KeyError(key),KeyError: 'formatters' 参考文章: (1)python3 日志检索异常抛出异常 raise KeyE ...

  5. post json 提示远程服务器500_解决WinServer2012R2服务器远程提示“参数错误”

    最近,小编在远程服务器进行日常的运维的时候,发现服务器无法远程了,提示如下: "参数错误",看到这个提示,小编一脸懵逼,记忆中好像没有对服务器做什么调整,只是前两天对它进行了修改管 ...

  6. PHP编译安装时常见错误解决办法,php编译常见错误

    PHP编译安装时常见错误解决办法,php编译常见错误 1.configure: error: xslt-config not found. Please reinstall the libxslt & ...

  7. 成功解决启动SQLServer失败,根据错误信息判断错误故障

    成功解决启动SQLServer失败,根据错误信息判断错误故障 目录 解决问题 解决思路及解决方法 解决问题 启动SQLServer失败,根据错误信息判断错误故障 解决思路及解决方法 (1).错误109 ...

  8. 如何让apache支持.htaccess 解决Internal Server Error The server …错误

    如何让apache支持.htaccess 解决Internal Server Error The server -错误 文章来源:小灰博客| 时间:2013-12-25 12:17:08| 作者:Le ...

  9. Ubuntu解决sudo source command not found错误

    Ubuntu解决sudo: source: command not found错误 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 在Ubuntu Ser ...

最新文章

  1. 《Node.js区块链开发》一3.5 亿书对DPoS机制的改进
  2. 乱谈Qt事件循环嵌套
  3. BZOJ1257 [CQOI2007]余数之和
  4. html中透明度怎么写,css中控制透明度
  5. gradle 上传jar包_gradle 打包jar上传到nexus 同时上传源码jar
  6. 五分钟入门 Dingo API
  7. 【渝粤教育】 国家开放大学2020年春季 1303护理伦理学 参考试题
  8. dns服务器v6解析 windows_04:缓存DNS、Split分离解析、电子邮件通信、Web服务器项目实战...
  9. P4570 [BJWC2011]元素(线性基+贪心)
  10. html lineheight div,html – Chrome上的文本输入:line-height似乎有最小值
  11. 表面招助理实为“拉皮条”?招聘平台也有情色陷阱,BOSS直聘回应...
  12. ROS笔记——Create a cropped bagfile(创建一个已裁剪的包文件)
  13. 谷歌发布2012年搜索上升最快关键词 江南style上榜
  14. MySQL的sql_mode解析设置
  15. python深拷贝和浅拷贝学习
  16. 大数据使用的5种主要数据挖掘技术
  17. Elasticsearch 7.X RESTful 风格 高级查询
  18. qt 移动文件夹到另一目录下
  19. 华为mate40pro和p40pro参数对比 华为mate40pro和p40pro哪个好
  20. Dbus启动问题 Failed to get D-Bus connection: Operation not permitted

热门文章

  1. java菱形有几种状态_java程序,打出一个菱形,有什么规律吗
  2. Journal of Genetics and Genomics科学编辑招聘启事
  3. 单细胞转录组数据整合分析专题研讨会(2019.11)
  4. Fertility of Soils:根系C/P计量比影响水稻残根周际酶活的时空动态分布特征
  5. ISME|宏转录组揭示参与深海碳氮循环的微生物
  6. R,Git和Github(下)
  7. R语言数据包自带数据集之ISwR包的melanom数据集字段解释、数据导入实战
  8. python使用matplotlib可视化包含倒影的柱状图(bar plot with shadow)、配置rcParams坐标轴正确显示负号(-)
  9. pandas筛选dataframe列名称中包含特定字符串的数据列(select columns contains specifiec substring in dataframe)
  10. R语言使用lm构建线性回归模型、并将目标变量对数化(log10)实战:可视化模型预测输出与实际值对比图、可视化模型的残差、模型预测中系统误差的一个例子 、自定义函数计算R方指标和均方根误差RMSE