我想计算用 allen-nlp 训练的分类器的 f1 分数。我使用了 allen-nlp 指南中的工作代码,它计算了准确度,而不是 F1,所以我尝试调整代码中的指标。
根据文档,CategoricalAccuracy和FBetaMultiLabelMeasure采用相同的输入。(预测: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, 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
为什么会发生此错误?我在这里想念什么?