我正在尝试将(tp+tn)/total_samples
计算为我的自定义损失函数。我知道如何在列表和列表理解中做到这一点,但是我想我无法将y_true
和y_pred
转换为列表。
到目前为止我写的代码是:
def CustomLossFunction(y_true, y_pred):
y_true_mask_less_zero = K.less(y_true, 0)
y_true_mask_greater_zero = K.greater(y_true, 0)
y_pred_mask_less_zero = K.less(y_pred, 0)
y_pred_mask_greater_zero = K.greater(y_pred, 0)
t_zeros = K.equal(y_pred_mask_less_zero, y_true_mask_less_zero)
t_ones = K.equal(y_pred_mask_greater_zero, y_true_mask_greater_zero)
现在,我需要将t_zeros和t_ones中的TRUE总数相加,并将它们相加并除以总样本
我在此行出现错误:
sum_of_true_negatives = K.sum(t_zeros)
传递给参数'input'的值的DataType bool不在允许的值列表中:float32,float64,int32,uint8,int16
问题:
答案 0 :(得分:1)
在将布尔张量进行计算之前,必须<p>...some text...</p>
使其布尔张量浮动。
但这只是一个警告,所以您不会浪费时间:
此损失功能无法区分,因此无法使用。您不能像这样简单地丢弃
cast
中存在的“连续性”。 (您将收到诸如“不支持任何值”或“操作没有梯度可操作”之类的错误)
使用一些现有的标准函数进行分类,例如y_pred
或binary_crossentropy
。
投射:
categorical_crossentropy