Python allennlp.training.metrics.F1Measure() Examples
The following are 16
code examples of allennlp.training.metrics.F1Measure().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
allennlp.training.metrics
, or try the search function
.
Example #1
Source File: bert_text_classifier.py From scibert with Apache License 2.0 | 6 votes |
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)
Example #2
Source File: text_classifier.py From scibert with Apache License 2.0 | 5 votes |
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)
Example #3
Source File: pico_crf_tagger.py From scibert with Apache License 2.0 | 5 votes |
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)
Example #4
Source File: f1_measure_test.py From allennlp with Apache License 2.0 | 5 votes |
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)
Example #5
Source File: f1_measure_test.py From allennlp with Apache License 2.0 | 5 votes |
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)
Example #6
Source File: f1_measure_test.py From allennlp with Apache License 2.0 | 5 votes |
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
Example #7
Source File: f1_measure_test.py From allennlp with Apache License 2.0 | 5 votes |
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)
Example #8
Source File: f1_measure_test.py From magnitude with MIT License | 5 votes |
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)
Example #9
Source File: f1_measure_test.py From magnitude with MIT License | 5 votes |
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)
Example #10
Source File: f1_measure_test.py From magnitude with MIT License | 5 votes |
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
Example #11
Source File: f1_measure_test.py From magnitude with MIT License | 5 votes |
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)
Example #12
Source File: prolocal_model.py From propara with Apache License 2.0 | 5 votes |
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)
Example #13
Source File: multiple_correct_mcq_multee_esim.py From multee with Apache License 2.0 | 5 votes |
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"])
Example #14
Source File: f1_measure_test.py From allennlp with Apache License 2.0 | 4 votes |
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)
Example #15
Source File: classification_model.py From kb with Apache License 2.0 | 4 votes |
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
Example #16
Source File: scaffold_bilstm_attention_classifier.py From scicite with Apache License 2.0 | 4 votes |
def __init__(self, vocab: Vocabulary, text_field_embedder: TextFieldEmbedder, citation_text_encoder: Seq2SeqEncoder, classifier_feedforward: FeedForward, classifier_feedforward_2: FeedForward, classifier_feedforward_3: FeedForward, initializer: InitializerApplicator = InitializerApplicator(), regularizer: Optional[RegularizerApplicator] = None, report_auxiliary_metrics: bool = False, predict_mode: bool = False, ) -> None: """ Additional Args: lexicon_embedder_params: parameters for the lexicon attention model use_sparse_lexicon_features: whether to use sparse (onehot) lexicon features multilabel: whether the classification is multi-label data_format: s2 or jurgens report_auxiliary_metrics: report metrics for aux tasks predict_mode: predict unlabeled examples """ super(ScaffoldBilstmAttentionClassifier, self).__init__(vocab, regularizer) self.text_field_embedder = text_field_embedder self.num_classes = self.vocab.get_vocab_size("labels") self.num_classes_sections = self.vocab.get_vocab_size("section_labels") self.num_classes_cite_worthiness = self.vocab.get_vocab_size("cite_worthiness_labels") self.citation_text_encoder = citation_text_encoder self.classifier_feedforward = classifier_feedforward self.classifier_feedforward_2 = classifier_feedforward_2 self.classifier_feedforward_3 = classifier_feedforward_3 self.label_accuracy = CategoricalAccuracy() self.label_f1_metrics = {} self.label_f1_metrics_sections = {} self.label_f1_metrics_cite_worthiness = {} # for i in range(self.num_classes): # self.label_f1_metrics[vocab.get_token_from_index(index=i, namespace="labels")] =\ # F1Measure(positive_label=i) for i in range(self.num_classes): self.label_f1_metrics[vocab.get_token_from_index(index=i, namespace="labels")] =\ F1Measure(positive_label=i) for i in range(self.num_classes_sections): self.label_f1_metrics_sections[vocab.get_token_from_index(index=i, namespace="section_labels")] =\ F1Measure(positive_label=i) for i in range(self.num_classes_cite_worthiness): self.label_f1_metrics_cite_worthiness[vocab.get_token_from_index(index=i, namespace="cite_worthiness_labels")] =\ F1Measure(positive_label=i) self.loss = torch.nn.CrossEntropyLoss() self.attention_seq2seq = Attention(citation_text_encoder.get_output_dim()) self.report_auxiliary_metrics = report_auxiliary_metrics self.predict_mode = predict_mode initializer(self)