AllenNLP 2.0:无法运行 FBetaMultiLabelMeasure

问题描述

我想计算用 allen-nlp 训练的分类器的 f1 分数。我使用了 allen-nlp 指南中的工作代码,它计算的是准确度,而不是 F1,因此我尝试调整代码中的指标。

根据文档,CategoricalAccuracyFBetaMultiLabelMeasure 采用相同的输入。 (预测:torch.Tensor 形状 [batch_size,...,num_classes],gold_labels:torch.Tensor 形状 [batch_size,...]

但由于某种原因,当输入 f1-multi-label 指标时,对于准确度非常有效的输入会导致 RuntimeError。

我将问题浓缩为以下代码片段:

>>> from allennlp.training.metrics import CategoricalAccuracy,FBetaMultiLabelMeasure
>>> import torch
>>> labels = torch.LongTensor([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

为什么会发生这个错误?我在这里错过了什么?

解决方法

您想使用 FBetaMeasure,而不是 FBetaMultiLabelMeasure。 “多标签”意味着您可以指定多个正确答案,但“分类准确度”只允许一个正确答案。这意味着您必须在标签中指定另一个维度。

我怀疑 FBetaMultiLabelMeasure 的文档具有误导性。我会考虑修复它。