Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

NER example: fix divisions by zero #15068

Merged
merged 1 commit into from
May 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions example/named_entity_recognition/src/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,20 @@ def classifer_metrics(label, pred):
correct_entitites = np.sum(corr_pred[pred_is_entity])

#precision: when we predict entity, how often are we right?
precision = correct_entitites/entity_preds
if entity_preds == 0:
precision = np.nan
else:
precision = correct_entitites/entity_preds

#recall: of the things that were an entity, how many did we catch?
recall = correct_entitites / num_entities
if num_entities == 0:
recall = np.nan
f1 = 2 * precision * recall / (precision + recall)
# To prevent dozens of warning: RuntimeWarning: divide by zero encountered in long_scalars
if precision + recall == 0:
f1 = 0
else:
f1 = 2 * precision * recall / (precision + recall)
return precision, recall, f1

def entity_precision(label, pred):
Expand Down