【发布时间】:2021-02-12 10:01:25
【问题描述】:
我想计算一个使用 allen-nlp 训练的分类器的 f1 分数。我使用了 allen-nlp 指南中的工作代码,它计算的是准确度,而不是 F1,所以我尝试调整代码中的指标。
根据文档,CategoricalAccuracy 和 FBetaMultiLabelMeasure 采用相同的输入。 (预测:torch.Tensor 形状 [batch_size, ..., num_classes],gold_labels:torch.Tensor 形状 [batch_size, ...])
但由于某种原因,当将输入用于 f1-multi-label 指标时,对于准确性非常有效的输入会导致 RuntimeError。
我把问题浓缩成如下代码sn-p:
>>> 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
为什么会发生这个错误?我在这里错过了什么?
【问题讨论】: