mindformers.core.EntityScore

View Source On Gitee
class mindformers.core.EntityScore[source]

Evaluates the precision, recall, and F1 score of predicted entities against the ground truth.

Mathematically, these metrics are defined as follows:

Precision: Measures the fraction of correctly predicted entities out of all predicted entities.

\[\text{Precision} = \frac{\text{Number of correct entities}}{\text{Number of predicted entities}}\]

Recall: Measures the fraction of correctly predicted entities out of all actual entities.

\[\text{Recall} = \frac{\text{Number of correct entities}}{\text{Number of actual entities}}\]

F1 Score: The harmonic mean of precision and recall, providing a balance between them.

\[\text{F1} = \frac{2 \times \text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}\]

Examples

>>> import numpy as np
>>> from mindspore import Tensor
>>> from mindformers.core.metric.metric import EntityScore
>>> x = Tensor(np.array([[np.arange(0, 22)]]))
>>> y = Tensor(np.array([[21]]))
>>> metric = EntityScore()
>>> metric.clear()
>>> metric.update(x, y)
>>> result = metric.eval()
>>> print(result)
({'precision': 1.0, 'recall': 1.0, 'f1': 1.0}, {'address': {'precision': 1.0, 'recall': 1.0, 'f1': 1.0}})
clear()[source]

Clearing the internal evaluation result.

eval()[source]

Computing the evaluation result.

Returns

A dict of evaluation results with precision, recall, and F1 scores of entities relative to their true labels.

update(*inputs)[source]

Updating the internal evaluation result.

Parameters

*inputs (List) – Logits and labels. The logits are tensors of shape \([N,C]\) with data type Float16 or Float32, and the labels are tensors of shape \([N,]\) with data type Int32 or Int64, where \(N\) is batch size, and \(C\) is the total number of entity types.