library.custom_objects
1def f1(y_true, y_pred): 2 """Compute the F1 score (harmonic mean of precision and recall) for binary classification. 3 This function operates on Keras backend tensors and computes precision and recall 4 from rounded/clipped binary values before combining them into the F1 score. 5 True positives, predicted positives and actual positives are computed by 6 rounding clipped values to {0, 1}, and a small epsilon (K.epsilon()) is added 7 to denominators to avoid division by zero. 8 Args: 9 y_true: Tensor. Ground-truth labels. Expected shape (batch_size, ...) and 10 values are treated as binary after clipping/rounding. 11 y_pred: Tensor. Predicted values (e.g. probabilities or logits). Same shape 12 as y_true; values are clipped and rounded to produce binary predictions. 13 Returns: 14 Tensor. Scalar tensor representing the F1 score in the range [0, 1]. 15 Notes: 16 - Implemented using Keras backend (K) operations; intended for use as a 17 Keras metric or loss component. 18 - The implementation computes: 19 TP = sum(round(clip(y_true * y_pred, 0, 1))) 20 Predicted = sum(round(clip(y_pred, 0, 1))) 21 Actual = sum(round(clip(y_true, 0, 1))) 22 then precision = TP / (Predicted + eps), recall = TP / (Actual + eps), 23 and F1 = 2 * (precision * recall) / (precision + recall + eps). 24 """ 25 26 def recall_m(y_true, y_pred): 27 TP = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) 28 Positives = K.sum(K.round(K.clip(y_true, 0, 1))) 29 recall = TP / (Positives+K.epsilon()) 30 return recall 31 32 def precision_m(y_true, y_pred): 33 TP = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) 34 Pred_Positives = K.sum(K.round(K.clip(y_pred, 0, 1))) 35 precision = TP / (Pred_Positives+K.epsilon()) 36 return precision 37 38 precision, recall = precision_m(y_true, y_pred), recall_m(y_true, y_pred) 39 40 return 2*((precision*recall)/(precision+recall+K.epsilon()))
def
f1(y_true, y_pred):
2def f1(y_true, y_pred): 3 """Compute the F1 score (harmonic mean of precision and recall) for binary classification. 4 This function operates on Keras backend tensors and computes precision and recall 5 from rounded/clipped binary values before combining them into the F1 score. 6 True positives, predicted positives and actual positives are computed by 7 rounding clipped values to {0, 1}, and a small epsilon (K.epsilon()) is added 8 to denominators to avoid division by zero. 9 Args: 10 y_true: Tensor. Ground-truth labels. Expected shape (batch_size, ...) and 11 values are treated as binary after clipping/rounding. 12 y_pred: Tensor. Predicted values (e.g. probabilities or logits). Same shape 13 as y_true; values are clipped and rounded to produce binary predictions. 14 Returns: 15 Tensor. Scalar tensor representing the F1 score in the range [0, 1]. 16 Notes: 17 - Implemented using Keras backend (K) operations; intended for use as a 18 Keras metric or loss component. 19 - The implementation computes: 20 TP = sum(round(clip(y_true * y_pred, 0, 1))) 21 Predicted = sum(round(clip(y_pred, 0, 1))) 22 Actual = sum(round(clip(y_true, 0, 1))) 23 then precision = TP / (Predicted + eps), recall = TP / (Actual + eps), 24 and F1 = 2 * (precision * recall) / (precision + recall + eps). 25 """ 26 27 def recall_m(y_true, y_pred): 28 TP = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) 29 Positives = K.sum(K.round(K.clip(y_true, 0, 1))) 30 recall = TP / (Positives+K.epsilon()) 31 return recall 32 33 def precision_m(y_true, y_pred): 34 TP = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) 35 Pred_Positives = K.sum(K.round(K.clip(y_pred, 0, 1))) 36 precision = TP / (Pred_Positives+K.epsilon()) 37 return precision 38 39 precision, recall = precision_m(y_true, y_pred), recall_m(y_true, y_pred) 40 41 return 2*((precision*recall)/(precision+recall+K.epsilon()))
Compute the F1 score (harmonic mean of precision and recall) for binary classification. This function operates on Keras backend tensors and computes precision and recall from rounded/clipped binary values before combining them into the F1 score. True positives, predicted positives and actual positives are computed by rounding clipped values to {0, 1}, and a small epsilon (K.epsilon()) is added to denominators to avoid division by zero.
Arguments:
- y_true: Tensor. Ground-truth labels. Expected shape (batch_size, ...) and values are treated as binary after clipping/rounding.
- y_pred: Tensor. Predicted values (e.g. probabilities or logits). Same shape as y_true; values are clipped and rounded to produce binary predictions.
Returns:
Tensor. Scalar tensor representing the F1 score in the range [0, 1].
Notes:
- Implemented using Keras backend (K) operations; intended for use as a Keras metric or loss component.
- The implementation computes: TP = sum(round(clip(y_true * y_pred, 0, 1))) Predicted = sum(round(clip(y_pred, 0, 1))) Actual = sum(round(clip(y_true, 0, 1))) then precision = TP / (Predicted + eps), recall = TP / (Actual + eps), and F1 = 2 * (precision * recall) / (precision + recall + eps).