ner.utils.metrics module#
- ner.utils.metrics.compute_entity_f1(y_true: ndarray | Dict[str, List[Tuple[int]]], y_pred: ndarray | Dict[str, List[Tuple[int]]], average: str = 'weighted', token_idxs: List | ndarray | None = None) float #
Compute an average of entity-level F1 scores.
- Parameters:
- y_true
Union
[np.ndarray
,Dict
[str
,List
[Tuple
[int
]]]] The true label IDs, or a dictionary of true named-entities mapped to a list of their respective spans (e.g.,
{"LOC": [(1, 1), (2, 5)]}
).- y_pred
Union
[np.ndarray
,Dict
[str
,List
[Tuple
[int
]]]] The predicted label IDs, or a dictionary of predicted named-entities mapped to a list of their respective spans (e.g.,
{"LOC": [(1, 1), (2, 5)]}
).- average{“weighted”, “macro”}, default: “weighted”
The averaging method for aggregating entity-level F1 scores.
- token_idxs
Optional
[Union
[List
,np.ndarray
]], default:None
The token indices associated with
y_true
andy_test
, when they arenp.ndarray
.
- y_true
- Returns:
float
An average of entity-level F1 scores.
- ner.utils.metrics.compute_loss(loss_fn: Callable, preds: Tensor, labels: Tensor) Tensor #
Compute loss given a loss function, logits, and labels. Note: the logits and labels must be on the same device.
- Parameters:
- loss_fn
Callable
The loss function (criterion).
- preds
torch.Tensor
The prediction IDs output by the model.
- labels
torch.Tensor
The target label IDs.
- loss_fn
- Returns:
torch.Tensor
The computed loss, returned as a tensor on the same device as logits and labels.
- ner.utils.metrics.compute_metrics(preds: Tensor, labels: Tensor, padding_mask: Tensor | None = None, labels_ignore_idx: int | None = None, other_ner_tag_idx: int | None = None, average: str = 'weighted')#
Compute metrics given predictions and labels.
- Parameters:
- preds
torch.Tensor
The prediction IDs output by the model.
- labels
torch.Tensor
The target label IDs.
- padding_mask
Optional
[torch.Tensor
], default:None
The padding mask.
- labels_ignore_idx
Optional
[int
], default:None
If
padding_mask
is not provided, thepadding_mask
can be auto-inferred fromlabels_ignore_idx
, iflabels_ignore_idx
is provided. Corresponds toPAD_NER_TAG
inNER_ENCODING_MAP
.- other_ner_tag_idx
Optional
[int
] The index of the “O” tag; if provided, the performance computation ignores the “O” tags. Corresponds to
O
inNER_ENCODING_MAP
.- average
str
The averaging method for aggregating entity-level F1 scores.
- preds
- Returns: