python - 如何使用 TensorFlow 2.0 计算分类交叉熵?
问题描述
我试图了解CategoricalCrossentropy()
TensorFlow 2.0 中的损失函数。当我使用
tf.keras.metrics.CategoricalCrossentropy(actual, pred)
我收到以下错误:
ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()
谁能解释如何纠正?
解决方案
从docs计算交叉熵,使用,
cce = tf.keras.metrics.CategoricalCrossentropy()
# cce.update_state(target, prediction)
cce.update_state([[0, 1, 0], [0, 0, 1]], [[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
cce.result().numpy()
# 1.1769392
OTOH,如果您尝试计算交叉熵损失,请tf.keras.losses.CategoricalCrossentropy
改用:
cce = tf.keras.metrics.CategoricalCrossentropy()
cce([[0, 1, 0], [0, 0, 1]], [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]).numpy()
# 1.1769392
推荐阅读
- ios - 无法使用 Swift 5.0 在 Xcode Simulator 中放大和缩小 HIChart 的折线图/条形图
- c# - Selenium Chrome 驱动程序(75 起)使用 GetLog() 方法访问日志时出现问题
- c - 为什么它一直说我的动态数组的容量是 2,即使我一直在改变它,所以它不是 2?
- android - NavigationView onClick 内的菜单项仅在双击时触发
- php - PHP ODBC - 连接无法正常工作
- python - 如何获得累积匹配特征 (CMC) 曲线的正确结果
- angular - 错误:TypeError:无法读取未定义的属性“用户名”
- css - 为什么 gnome 应用程序会忽略部分 gtk 3 主题?
- loops - 气流任务仅在外部触发时运行一次,而不是在默认争论中提到的时间间隔中指定
- angular - Angular 8 测试 - Devextreme Grid getDataSource() 不使用状态存储