Skip to content

创建评估函数, 二分类模型, 计算f1_metric 错误 #17

Description

02-NLP Tasks ---> 08-transformers_slolution ---> classificaion_demo.ipynb

问题定位
f1 = f1_metric.compute(predictions=predictions, references=labels)

报错信息
return {"f1": float(score) if score.size == 1 else score}
AttributeError: 'float' object has no attribute 'size'

原因分析
def _compute(self, predictions, references, labels=None, pos_label=1, average="binary", sample_weight=None):
score = f1_score(
references, predictions, labels=labels, pos_label=pos_label, average=average, sample_weight=sample_weight
)

return {"f1": float(score) if score.size == 1 else score}

'float' object has no attribute 'size', 在二分类任务中,当 average="binary" 时,f1_score 返回一个单一的 float 值,

而不是 NumPy 数组,因此 score.size 会触发错误。

修改返回值
return {"f1": float(score)}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions