【问题标题】:How to find loss values using keras?如何使用 keras 查找损失值?
【发布时间】:2018-04-02 09:13:54
【问题描述】:

我想使用 keras 中定义的各种损失函数来手动计算损失值。例如:

from keras.losses import binary_crossentropy
error=binary_crossentropy([1,2,3,4],[6,7,8,9])

给我错误

AttributeError: 'list' object has no attribute 'dtype'.

我想使用其他 keras 损失函数的类似方式。我有我的 y_pred 和 y_true 列表/数组。

【问题讨论】:

  • 使用 numpy 数组而不是列表
  • 完成了,将我的列表转换为 numpy 数组,但我得到了这个AttributeError: 'numpy.dtype' object has no attribute 'base_dtype'。 ​

标签: python deep-learning keras


【解决方案1】:

您可以使用K.variable() 包装输入并使用K.eval() 获取值。

from keras.losses import binary_crossentropy
from keras import backend as K
y_true = K.variable(np.array([[1], [0], [1], [1]]))
y_pred = K.variable(np.array([[0.5], [0.6], [0.7], [0.8]]))
error = K.eval(binary_crossentropy(y_true, y_pred))

print(error)
[ 0.69314718  0.91629082  0.35667494  0.22314353]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 2022-10-19
    • 2019-12-28
    • 2018-07-28
    • 2020-11-26
    相关资源
    最近更新 更多