本文整理汇总了Python中allennlp.training.metrics.F1Measure方法的典型用法代码示例。如果您正苦于以下问题:Python metrics.F1Measure方法的具体用法?Python metrics.F1Measure怎么用?Python metrics.F1Measure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类allennlp.training.metrics
的用法示例。
在下文中一共展示了metrics.F1Measure方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def __init__(self, vocab: Vocabulary,
text_field_embedder: TextFieldEmbedder,
verbose_metrics: bool = False,
dropout: float = 0.2,
initializer: InitializerApplicator = InitializerApplicator(),
regularizer: Optional[RegularizerApplicator] = None,
) -> None:
super(TextClassifier, self).__init__(vocab, regularizer)
self.text_field_embedder = text_field_embedder
self.dropout = torch.nn.Dropout(dropout)
self.num_classes = self.vocab.get_vocab_size("labels")
self.classifier_feedforward = torch.nn.Linear(self.text_field_embedder.get_output_dim() , self.num_classes)
self.label_accuracy = CategoricalAccuracy()
self.label_f1_metrics = {}
self.verbose_metrics = verbose_metrics
for i in range(self.num_classes):
self.label_f1_metrics[vocab.get_token_from_index(index=i, namespace="labels")] = F1Measure(positive_label=i)
self.loss = torch.nn.CrossEntropyLoss()
initializer(self)
示例2: __init__
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def __init__(self, vocab: Vocabulary,
text_field_embedder: TextFieldEmbedder,
text_encoder: Seq2SeqEncoder,
classifier_feedforward: FeedForward,
verbose_metrics: False,
initializer: InitializerApplicator = InitializerApplicator(),
regularizer: Optional[RegularizerApplicator] = None,
) -> None:
super(TextClassifier, self).__init__(vocab, regularizer)
self.text_field_embedder = text_field_embedder
self.num_classes = self.vocab.get_vocab_size("labels")
self.text_encoder = text_encoder
self.classifier_feedforward = classifier_feedforward
self.prediction_layer = torch.nn.Linear(self.classifier_feedforward.get_output_dim() , self.num_classes)
self.label_accuracy = CategoricalAccuracy()
self.label_f1_metrics = {}
self.verbose_metrics = verbose_metrics
for i in range(self.num_classes):
self.label_f1_metrics[vocab.get_token_from_index(index=i, namespace="labels")] = F1Measure(positive_label=i)
self.loss = torch.nn.CrossEntropyLoss()
self.pool = lambda text, mask: util.get_final_encoder_states(text, mask, bidirectional=True)
initializer(self)
示例3: __init__
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def __init__(self, vocab: Vocabulary,
text_field_embedder: TextFieldEmbedder,
encoder: Seq2SeqEncoder,
include_start_end_transitions: bool = True,
dropout: Optional[float] = None,
initializer: InitializerApplicator = InitializerApplicator(),
regularizer: Optional[RegularizerApplicator] = None) -> None:
super().__init__(vocab, regularizer)
self.label_namespace = 'labels'
self.num_tags = self.vocab.get_vocab_size(self.label_namespace)
# encode text
self.text_field_embedder = text_field_embedder
self.encoder = encoder
self.dropout = torch.nn.Dropout(dropout) if dropout else None
# crf
output_dim = self.encoder.get_output_dim()
self.tag_projection_layer = TimeDistributed(Linear(output_dim, self.num_tags))
self.crf = ConditionalRandomField(self.num_tags, constraints=None, include_start_end_transitions=include_start_end_transitions)
self.metrics = {
"accuracy": CategoricalAccuracy(),
"accuracy3": CategoricalAccuracy(top_k=3)
}
for index, label in self.vocab.get_index_to_token_vocabulary(self.label_namespace).items():
self.metrics['F1_' + label] = F1Measure(positive_label=index)
initializer(self)
示例4: test_f1_measure_catches_exceptions
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def test_f1_measure_catches_exceptions(self, device: str):
f1_measure = F1Measure(0)
predictions = torch.rand([5, 7], device=device)
out_of_range_labels = torch.tensor([10, 3, 4, 0, 1], device=device)
with pytest.raises(ConfigurationError):
f1_measure(predictions, out_of_range_labels)
示例5: test_f1_measure_other_positive_label
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def test_f1_measure_other_positive_label(self, device: str):
f1_measure = F1Measure(positive_label=1)
predictions = torch.tensor(
[
[0.35, 0.25, 0.1, 0.1, 0.2],
[0.1, 0.6, 0.1, 0.2, 0.0],
[0.1, 0.6, 0.1, 0.2, 0.0],
[0.1, 0.5, 0.1, 0.2, 0.0],
[0.1, 0.2, 0.1, 0.7, 0.0],
[0.1, 0.6, 0.1, 0.2, 0.0],
],
device=device,
)
# [True Negative, False Positive, True Positive,
# False Positive, True Negative, False Positive]
targets = torch.tensor([0, 4, 1, 0, 3, 0], device=device)
f1_measure(predictions, targets)
precision, recall, f1 = f1_measure.get_metric()
assert f1_measure._true_positives == 1.0
assert f1_measure._true_negatives == 2.0
assert f1_measure._false_positives == 3.0
assert f1_measure._false_negatives == 0.0
f1_measure.reset()
# check value
assert_allclose(precision, 0.25)
assert_allclose(recall, 1.0)
assert_allclose(f1, 0.4)
# check type
assert isinstance(precision, float)
assert isinstance(recall, float)
assert isinstance(f1, float)
示例6: test_f1_measure_accumulates_and_resets_correctly
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def test_f1_measure_accumulates_and_resets_correctly(self, device: str):
f1_measure = F1Measure(positive_label=0)
predictions = torch.tensor(
[
[0.35, 0.25, 0.1, 0.1, 0.2],
[0.1, 0.6, 0.1, 0.2, 0.0],
[0.1, 0.6, 0.1, 0.2, 0.0],
[0.1, 0.5, 0.1, 0.2, 0.0],
[0.1, 0.2, 0.1, 0.7, 0.0],
[0.1, 0.6, 0.1, 0.2, 0.0],
],
device=device,
)
# [True Positive, True Negative, True Negative,
# False Negative, True Negative, False Negative]
targets = torch.tensor([0, 4, 1, 0, 3, 0], device=device)
f1_measure(predictions, targets)
f1_measure(predictions, targets)
precision, recall, f1 = f1_measure.get_metric()
assert f1_measure._true_positives == 2.0
assert f1_measure._true_negatives == 6.0
assert f1_measure._false_positives == 0.0
assert f1_measure._false_negatives == 4.0
f1_measure.reset()
assert_allclose(precision, 1.0)
assert_allclose(recall, 0.333333333)
assert_allclose(f1, 0.499999999)
assert f1_measure._true_positives == 0.0
assert f1_measure._true_negatives == 0.0
assert f1_measure._false_positives == 0.0
assert f1_measure._false_negatives == 0.0
示例7: test_f1_measure_works_for_sequences
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def test_f1_measure_works_for_sequences(self, device: str):
f1_measure = F1Measure(positive_label=0)
predictions = torch.tensor(
[
[[0.35, 0.25, 0.1, 0.1, 0.2], [0.1, 0.6, 0.1, 0.2, 0.0], [0.1, 0.6, 0.1, 0.2, 0.0]],
[[0.35, 0.25, 0.1, 0.1, 0.2], [0.1, 0.6, 0.1, 0.2, 0.0], [0.1, 0.6, 0.1, 0.2, 0.0]],
],
device=device,
)
# [[True Positive, True Negative, True Negative],
# [True Positive, True Negative, False Negative]]
targets = torch.tensor([[0, 3, 4], [0, 1, 0]], device=device)
f1_measure(predictions, targets)
precision, recall, f1 = f1_measure.get_metric()
assert f1_measure._true_positives == 2.0
assert f1_measure._true_negatives == 3.0
assert f1_measure._false_positives == 0.0
assert f1_measure._false_negatives == 1.0
f1_measure.reset()
assert_allclose(precision, 1.0)
assert_allclose(recall, 0.666666666)
assert_allclose(f1, 0.8)
# Test the same thing with a mask:
mask = torch.tensor([[False, True, False], [True, True, True]], device=device)
f1_measure(predictions, targets, mask)
precision, recall, f1 = f1_measure.get_metric()
assert f1_measure._true_positives == 1.0
assert f1_measure._true_negatives == 2.0
assert f1_measure._false_positives == 0.0
assert f1_measure._false_negatives == 1.0
assert_allclose(precision, 1.0)
assert_allclose(recall, 0.5)
assert_allclose(f1, 0.66666666666)
示例8: test_f1_measure_catches_exceptions
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def test_f1_measure_catches_exceptions(self):
f1_measure = F1Measure(0)
predictions = torch.rand([5, 7])
out_of_range_labels = torch.Tensor([10, 3, 4, 0, 1])
with pytest.raises(ConfigurationError):
f1_measure(predictions, out_of_range_labels)
示例9: test_f1_measure
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def test_f1_measure(self):
f1_measure = F1Measure(positive_label=0)
predictions = torch.Tensor([[0.35, 0.25, 0.1, 0.1, 0.2],
[0.1, 0.6, 0.1, 0.2, 0.0],
[0.1, 0.6, 0.1, 0.2, 0.0],
[0.1, 0.5, 0.1, 0.2, 0.0],
[0.1, 0.2, 0.1, 0.7, 0.0],
[0.1, 0.6, 0.1, 0.2, 0.0]])
# [True Positive, True Negative, True Negative,
# False Negative, True Negative, False Negative]
targets = torch.Tensor([0, 4, 1, 0, 3, 0])
f1_measure(predictions, targets)
precision, recall, f1 = f1_measure.get_metric()
assert f1_measure._true_positives == 1.0
assert f1_measure._true_negatives == 3.
assert f1_measure._false_positives == 0.0
assert f1_measure._false_negatives == 2.0
f1_measure.reset()
numpy.testing.assert_almost_equal(precision, 1.0)
numpy.testing.assert_almost_equal(recall, 0.333333333)
numpy.testing.assert_almost_equal(f1, 0.499999999)
# Test the same thing with a mask:
mask = torch.Tensor([1, 0, 1, 1, 1, 0])
f1_measure(predictions, targets, mask)
precision, recall, f1 = f1_measure.get_metric()
assert f1_measure._true_positives == 1.0
assert f1_measure._true_negatives == 2.0
assert f1_measure._false_positives == 0.0
assert f1_measure._false_negatives == 1.0
f1_measure.reset()
numpy.testing.assert_almost_equal(precision, 1.0)
numpy.testing.assert_almost_equal(recall, 0.5)
numpy.testing.assert_almost_equal(f1, 0.6666666666)
示例10: test_f1_measure_accumulates_and_resets_correctly
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def test_f1_measure_accumulates_and_resets_correctly(self):
f1_measure = F1Measure(positive_label=0)
predictions = torch.Tensor([[0.35, 0.25, 0.1, 0.1, 0.2],
[0.1, 0.6, 0.1, 0.2, 0.0],
[0.1, 0.6, 0.1, 0.2, 0.0],
[0.1, 0.5, 0.1, 0.2, 0.0],
[0.1, 0.2, 0.1, 0.7, 0.0],
[0.1, 0.6, 0.1, 0.2, 0.0]])
# [True Positive, True Negative, True Negative,
# False Negative, True Negative, False Negative]
targets = torch.Tensor([0, 4, 1, 0, 3, 0])
f1_measure(predictions, targets)
f1_measure(predictions, targets)
precision, recall, f1 = f1_measure.get_metric()
assert f1_measure._true_positives == 2.0
assert f1_measure._true_negatives == 6.0
assert f1_measure._false_positives == 0.0
assert f1_measure._false_negatives == 4.0
f1_measure.reset()
numpy.testing.assert_almost_equal(precision, 1.0)
numpy.testing.assert_almost_equal(recall, 0.333333333)
numpy.testing.assert_almost_equal(f1, 0.499999999)
assert f1_measure._true_positives == 0.0
assert f1_measure._true_negatives == 0.0
assert f1_measure._false_positives == 0.0
assert f1_measure._false_negatives == 0.0
示例11: test_f1_measure_works_for_sequences
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def test_f1_measure_works_for_sequences(self):
f1_measure = F1Measure(positive_label=0)
predictions = torch.Tensor([[[0.35, 0.25, 0.1, 0.1, 0.2],
[0.1, 0.6, 0.1, 0.2, 0.0],
[0.1, 0.6, 0.1, 0.2, 0.0]],
[[0.35, 0.25, 0.1, 0.1, 0.2],
[0.1, 0.6, 0.1, 0.2, 0.0],
[0.1, 0.6, 0.1, 0.2, 0.0]]])
# [[True Positive, True Negative, True Negative],
# [True Positive, True Negative, False Negative]]
targets = torch.Tensor([[0, 3, 4],
[0, 1, 0]])
f1_measure(predictions, targets)
precision, recall, f1 = f1_measure.get_metric()
assert f1_measure._true_positives == 2.0
assert f1_measure._true_negatives == 3.0
assert f1_measure._false_positives == 0.0
assert f1_measure._false_negatives == 1.0
f1_measure.reset()
numpy.testing.assert_almost_equal(precision, 1.0)
numpy.testing.assert_almost_equal(recall, 0.666666666)
numpy.testing.assert_almost_equal(f1, 0.8)
# Test the same thing with a mask:
mask = torch.Tensor([[0, 1, 0],
[1, 1, 1]])
f1_measure(predictions, targets, mask)
precision, recall, f1 = f1_measure.get_metric()
assert f1_measure._true_positives == 1.0
assert f1_measure._true_negatives == 2.0
assert f1_measure._false_positives == 0.0
assert f1_measure._false_negatives == 1.0
numpy.testing.assert_almost_equal(precision, 1.0)
numpy.testing.assert_almost_equal(recall, 0.5)
numpy.testing.assert_almost_equal(f1, 0.66666666666)
示例12: __init__
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def __init__(self, vocab: Vocabulary,
text_field_embedder: TextFieldEmbedder,
seq2seq_encoder: Seq2SeqEncoder,
initializer: InitializerApplicator) -> None:
super(ProLocalModel, self).__init__(vocab)
self.text_field_embedder = text_field_embedder
self.seq2seq_encoder = seq2seq_encoder
self.attention_layer = \
Attention(similarity_function=BilinearSimilarity(2 * seq2seq_encoder.get_output_dim(),
seq2seq_encoder.get_output_dim()), normalize=True)
self.num_types = self.vocab.get_vocab_size("state_change_type_labels")
self.aggregate_feedforward = Linear(seq2seq_encoder.get_output_dim(),
self.num_types)
self.span_metric = SpanBasedF1Measure(vocab,
tag_namespace="state_change_tags") # by default "O" is ignored in metric computation
self.num_tags = self.vocab.get_vocab_size("state_change_tags")
self.tag_projection_layer = TimeDistributed(Linear(self.seq2seq_encoder.get_output_dim() + 2
, self.num_tags))
self._type_accuracy = CategoricalAccuracy()
self.type_f1_metrics = {}
self.type_labels_vocab = self.vocab.get_index_to_token_vocabulary("state_change_type_labels")
for type_label in self.type_labels_vocab.values():
self.type_f1_metrics["type_" + type_label] = F1Measure(self.vocab.get_token_index(type_label, "state_change_type_labels"))
self._loss = torch.nn.CrossEntropyLoss()
initializer(self)
示例13: __init__
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def __init__(self, vocab: Vocabulary,
text_field_embedder: TextFieldEmbedder,
encoder: Seq2SeqEncoder,
projection_feedforward: FeedForward,
inference_encoder: Seq2SeqEncoder,
output_feedforward: FeedForward,
output_logit: FeedForward,
final_feedforward: FeedForward,
coverage_loss: CoverageLoss,
similarity_function: SimilarityFunction = DotProductSimilarity(),
dropout: float = 0.5,
contextualize_pair_comparators: bool = False,
pair_context_encoder: Seq2SeqEncoder = None,
pair_feedforward: FeedForward = None,
initializer: InitializerApplicator = InitializerApplicator(),
regularizer: Optional[RegularizerApplicator] = None) -> None:
super().__init__(vocab=vocab,
text_field_embedder=text_field_embedder,
encoder=encoder,
similarity_function=similarity_function,
projection_feedforward=projection_feedforward,
inference_encoder=inference_encoder,
output_feedforward=output_feedforward,
output_logit=output_logit,
final_feedforward=final_feedforward,
coverage_loss=coverage_loss,
contextualize_pair_comparators=contextualize_pair_comparators,
pair_context_encoder=pair_context_encoder,
pair_feedforward=pair_feedforward,
dropout=dropout,
initializer=initializer,
regularizer=regularizer)
self._ignore_index = -1
self._answer_loss = torch.nn.CrossEntropyLoss(ignore_index=self._ignore_index)
self._coverage_loss = coverage_loss
self._accuracy = CategoricalAccuracy()
self._entailment_f1 = F1Measure(self._label2idx["entailment"])
示例14: test_f1_measure
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def test_f1_measure(self, device: str):
f1_measure = F1Measure(positive_label=0)
predictions = torch.tensor(
[
[0.35, 0.25, 0.1, 0.1, 0.2],
[0.1, 0.6, 0.1, 0.2, 0.0],
[0.1, 0.6, 0.1, 0.2, 0.0],
[0.1, 0.5, 0.1, 0.2, 0.0],
[0.1, 0.2, 0.1, 0.7, 0.0],
[0.1, 0.6, 0.1, 0.2, 0.0],
],
device=device,
)
# [True Positive, True Negative, True Negative,
# False Negative, True Negative, False Negative]
targets = torch.tensor([0, 4, 1, 0, 3, 0], device=device)
f1_measure(predictions, targets)
precision, recall, f1 = f1_measure.get_metric()
assert f1_measure._true_positives == 1.0
assert f1_measure._true_negatives == 3.0
assert f1_measure._false_positives == 0.0
assert f1_measure._false_negatives == 2.0
f1_measure.reset()
# check value
assert_allclose(precision, 1.0)
assert_allclose(recall, 0.333333333)
assert_allclose(f1, 0.499999999)
# check type
assert isinstance(precision, float)
assert isinstance(recall, float)
assert isinstance(f1, float)
# Test the same thing with a mask:
mask = torch.tensor([True, False, True, True, True, False], device=device)
f1_measure(predictions, targets, mask)
precision, recall, f1 = f1_measure.get_metric()
assert f1_measure._true_positives == 1.0
assert f1_measure._true_negatives == 2.0
assert f1_measure._false_positives == 0.0
assert f1_measure._false_negatives == 1.0
f1_measure.reset()
assert_allclose(precision, 1.0)
assert_allclose(recall, 0.5)
assert_allclose(f1, 0.6666666666)
示例15: get_metrics
# 需要导入模块: from allennlp.training import metrics [as 别名]
# 或者: from allennlp.training.metrics import F1Measure [as 别名]
def get_metrics(self, reset: bool = False):
metrics = {}
avg_metric = None
for metric in self.metrics:
if isinstance(metric, CategoricalAccuracy):
metrics['accuracy'] = metric.get_metric(reset)
m = metrics['accuracy']
elif isinstance(metric, F1Measure):
metrics['f1'] = metric.get_metric(reset)[-1]
m = metrics['f1']
elif isinstance(metric, FBetaMeasure):
metrics['f1'] = metric.get_metric(reset)['fscore']
m = metrics['f1']
elif isinstance(metric, FastMatthews) or isinstance(metric, Correlation):
metrics[f'correlation_{metric.corr_type}'] = metric.get_metric(reset)
m = metrics[f'correlation_{metric.corr_type}']
elif isinstance(metric, SemEval2010Task8Metric):
metrics['f1'] = metric.get_metric(reset)
m = metrics['f1']
elif isinstance(metric, MicroF1):
precision, recall, f1 = metric.get_metric(reset)
metrics['micro_prec'] = precision
metrics['micro_rec'] = recall
metrics['micro_f1'] = f1
m = metrics['micro_f1']
elif isinstance(metric, F1Metric):
precision, recall, f1 = metric.get_metric(reset)
metrics['precision'] = precision
metrics['recall'] = recall
metrics['f1'] = f1
m = metrics['f1']
else:
raise ValueError
if avg_metric is None:
avg_metric = [m, 1.0]
else:
avg_metric[0] += m
avg_metric[1] += 1
metrics = {k: float(v) for k, v in metrics.items()}
if avg_metric is not None:
metrics["avg_metric"] = avg_metric[0] / avg_metric[1]
return metrics