Skip to content

Commit

Permalink
fix: issues 1262, 1522 (#1655)
Browse files Browse the repository at this point in the history
Co-authored-by: vasily <[email protected]>
  • Loading branch information
Kolpnick and vaskonov authored Aug 15, 2023
1 parent 375534b commit 2254e30
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
3 changes: 3 additions & 0 deletions deeppavlov/metrics/accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ def sets_accuracy(y_true: [list, np.ndarray], y_predicted: [list, np.ndarray]) -
Returns:
portion of samples with absolutely coincidental sets of predicted values
Alias:
sets_accuracy
"""
examples_len = len(y_true)
correct = sum([set(y1) == set(y2) for y1, y2 in zip(y_true, y_predicted)])
Expand Down
40 changes: 38 additions & 2 deletions deeppavlov/metrics/fmeasure.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@

@register_metric('ner_f1')
def ner_f1(y_true, y_predicted):
"""
Calculates F1 measure for Named Entity Recognition task.
Args:
y_true: list of true values
y_predicted: list of predicted values
Returns:
F1 score
Alias:
ner_f1
"""
y_true = list(chain(*y_true))
y_predicted = list(chain(*y_predicted))
results = precision_recall_f1(y_true,
Expand All @@ -37,9 +50,23 @@ def ner_f1(y_true, y_predicted):


@register_metric('ner_token_f1')
def ner_token_f1(y_true, y_pred, print_results=False):
def ner_token_f1(y_true, y_predicted, print_results=False):
"""
Calculates F1 measure for Named Entity Recognition task without taking into account BIO or BIOES markup.
Args:
y_true: list of true values
y_predicted: list of predicted values
print_results: if True, then F1 score for each entity type is printed
Returns:
F1 score
Alias:
ner_f1
"""
y_true = list(chain(*y_true))
y_pred = list(chain(*y_pred))
y_pred = list(chain(*y_predicted))

# Drop BIO or BIOES markup
assert all(len(tag.split('-')) <= 2 for tag in y_true)
Expand Down Expand Up @@ -190,6 +217,9 @@ def round_f1(y_true, y_predicted):
Returns:
F1 score
Alias:
f1
"""
try:
predictions = [np.round(x) for x in y_predicted]
Expand All @@ -214,6 +244,9 @@ def round_f1_macro(y_true, y_predicted):
Returns:
F1 score
Alias:
f1_macro
"""
try:
predictions = [np.round(x) for x in y_predicted]
Expand All @@ -234,6 +267,9 @@ def round_f1_weighted(y_true, y_predicted):
Returns:
F1 score
Alias:
f1_weighted
"""
try:
predictions = [np.round(x) for x in y_predicted]
Expand Down
3 changes: 3 additions & 0 deletions deeppavlov/metrics/log_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ def sk_log_loss(y_true: Union[List[List[float]], List[List[int]], np.ndarray],
Returns:
Log loss
Alias:
log_loss
"""
return log_loss(y_true, y_predicted)
3 changes: 3 additions & 0 deletions deeppavlov/metrics/roc_auc_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def roc_auc_score(y_true: Union[List[List[float]], List[List[int]], np.ndarray],
Returns:
Area Under the Curve (AUC) from prediction scores
Alias:
roc_auc
"""
try:
return sklearn.metrics.roc_auc_score(np.squeeze(np.array(y_true)),
Expand Down
4 changes: 4 additions & 0 deletions docs/apiref/metrics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Different Metric functions.

.. autofunction:: deeppavlov.metrics.fmeasure.round_f1_weighted

.. autofunction:: deeppavlov.metrics.fmeasure.ner_f1

.. autofunction:: deeppavlov.metrics.fmeasure.ner_token_f1

.. autofunction:: deeppavlov.metrics.log_loss.sk_log_loss

.. autofunction:: deeppavlov.metrics.roc_auc_score.roc_auc_score

0 comments on commit 2254e30

Please sign in to comment.