我想计算使用allen-nlp训练的分类器的F1分数。我使用的是allen-nlp指南中的工作代码,它计算的是准确性,而不是F1,所以我尝试调整代码中的度量。
根据文档,CategoricalAccuracy和FBetaMultiLabelMeasure采用相同的输入。(预测: shape [batch_size, ..., num_classes]的torch.Tensor,gold_labels: shape [batch_size, ...]的torch.Tensor )
但由于某些原因,当输入F1多标签度量时,对于准确度工作得很好的输入会导致RuntimeError。
我将问题压缩为以下代码片段:
>>> from allennlp.training.metrics import CategoricalAccuracy, FBetaMultiLabelMeasure
>>> import torch
>>> labels = torch.LongTensor([0, 0, 2, 1, 0])
>>> logits = torch.FloatTensor([[ 0.0063, -0.0118, 0.1857], [ 0.0013, -0.0217, 0.0356], [-0.0028, -0.0512, 0.0253], [-0.0460, -0.0347, 0.0400], [-0.0418, 0.0254, 0.1001]])
>>> labels.shape
torch.Size([5])
>>> logits.shape
torch.Size([5, 3])
>>> ca = CategoricalAccuracy()
>>> f1 = FBetaMultiLabelMeasure()
>>> ca(logits, labels)
>>> f1(logits, labels)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../lib/python3.8/site-packages/allennlp/training/metrics/fbeta_multi_label_measure.py", line 130, in __call__
true_positives = (gold_labels * threshold_predictions).bool() & mask & pred_mask
RuntimeError: The size of tensor a (5) must match the size of tensor b (3) at non-singleton dimension 1为什么会发生这个错误?这里我漏掉了什么?
发布于 2021-02-12 10:01:25
您希望使用FBetaMeasure,而不是FBetaMultiLabelMeasure。"Multilabel“表示您可以指定多个正确答案,但"Categorical Accuracy”只允许指定一个正确答案。这意味着您必须在标签中指定另一个维度。
我怀疑FBetaMultiLabelMeasure的文档有误导性。我会考虑修好它的。
https://stackoverflow.com/questions/65984950
复制相似问题