如何使用keras找到损失值? [英] How to find loss values using keras?
本文介绍了如何使用keras找到损失值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用keras中定义的各种损失函数来手动计算损失值.例如:
I want to use the various loss function defined in keras for calculating the loss value manually. For example:
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 列表/数组.
Similar way I want to use other keras loss function. I have my y_pred and y_true lists/arrays.
推荐答案
您可以使用 K.variable()
包装输入并使用 K.eval()
获取价值.
You can use K.variable()
to wrap the inputs and use K.eval()
to get the value.
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]
这篇关于如何使用keras找到损失值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文