AllenNLP 2.0:无法将 FBetaMultiLabelMeasure 设为 运行

AllenNLP 2.0: Can't get FBetaMultiLabelMeasure to run

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

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

但出于某种原因,在将输入提供给 f1 多标签指标时,对于准确度来说非常有效的输入会导致运行时错误。

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

>>> 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

为什么会出现这个错误?我在这里错过了什么?

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

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