我在这里找到了这个功能 How to calculate F1 Macro in Keras?,但我不确定如何以相同的方式写出特异性?我正在为keras使用tensorflow后端。
def recall(y_true, y_pred):
"""Recall metric.
Only computes a batch-wise average of recall.
Computes the recall, a metric for multi-label classification of
how many relevant items are selected.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
我尝试了此解决方案,但它给出了错误,
def compute_binary_specificity(y_pred, y_true):
"""Compute the confusion matrix for a set of predictions.
Returns
-------
out : the specificity
"""
TN = np.logical_and(K.eval(y_true) == 0, K.eval(y_pred) == 0)
FP = np.logical_and(K.eval(y_true) == 0, K.eval(y_pred) == 1)
# as Keras Tensors
TN = K.sum(K.variable(TN))
FP = K.sum(K.variable(FP))
specificity = TN / (TN + FP + K.epsilon())
return specificity
错误:InvalidArgumentError:您必须使用dtype float和shape [?,140]输入占位符张量'dense_95_input'的值 [[[Node:density_95_input = Placeholderdtype = DT_FLOAT,shape = [?, 140],_device =“ / job:localhost /副本:0 / task:0 / device:CPU:0”]]
并指向此处
---> TN = np.logical_and(K.eval(y_true)== 0,K.eval(y_pred)== 0)