Я нашел эту функцию здесь
Как рассчитать F1 макрос в Керасе? но я не уверен, как я могу написать так же для специфичности? Я использую бэкэнд тензорного потока для кератов.
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: Вы должны передать значение для тензора-заполнителя 'density_95_input' с типом плавания dtype и формой [?, 140]
[[Узел: density_95_input = Placeholderdtype = DT_FLOAT, shape = [?, 140], _device = "/ job: localhost / replica: 0 / task: 0 / device: CPU: 0"]]
и указывает здесь
---> TN = np.logical_and (K.eval (y_true) == 0, K.eval (y_pred) == 0)