随笔 - 128  文章 - 0  评论 - 75  阅读 - 11万

使用评价指标工具

评估一个训练好的模型需要评估指标,比如正确率、查准率、查全率、F1值等。当然不同的任务类型有着不同的评估指标,而HuggingFace提供了统一的评价指标工具。

1.列出可用的评价指标
通过list_metrics()函数列出可用的评价指标:

def list_metric_test():
    # 第4章/列出可用的评价指标
    from datasets import list_metrics
    metrics_list = list_metrics()
    print(len(metrics_list), metrics_list[:5])

输出结果如下所示:

157 ['accuracy''bertscore''bleu''bleurt''brier_score']

可见目前包含157个评价指标,并且输出了前5个评价指标。

2.加载一个评价指标
通过load_metric()加载评价指标,需要说明的是有的评价指标和对应的数据集配套使用,这里以glue数据集的mrpc子集为例:

def load_metric_test():
    # 第4章/加载评价指标
    from datasets import load_metric
    metric = load_metric(path="accuracy"#加载accuracy指标
    print(metric)

    # 第4章/加载一个评价指标
    from datasets import load_metric
    metric = load_metric(path='glue', config_name='mrpc'#加载glue数据集中的mrpc子集
    print(metric)

3.获取评价指标的使用说明
评价指标的inputs_description属性描述了评价指标的使用方法,以及评价指标的使用方法如下所示:

def load_metric_description_test():
    # 第4章/加载一个评价指标
    from datasets import load_metric
    glue_metric = load_metric('glue''mrpc')  # 加载glue数据集中的mrpc子集
    print(glue_metric.inputs_description)

    references = [01]
    predictions = [01]
    results = glue_metric.compute(predictions=predictions, references=references)
    print(results)  # {'accuracy': 1.0, 'f1': 1.0}

输出结果如下所示:

Compute GLUE evaluation metric associated to each GLUE dataset.
Args:
    predictions: list of predictions to score.
        Each translation should be tokenized into a list of tokens.
    references: list of lists of references for each translation.
        Each reference should be tokenized into a list of tokens.
Returns: depending on the GLUE subset, one or several of:
    "accuracy": Accuracy
    "f1": F1 score
    "pearson": Pearson Correlation
    "spearmanr": Spearman Correlation
    "matthews_correlation": Matthew Correlation
Examples:

    >>> glue_metric = datasets.load_metric('glue''sst2')  # 'sst2' or any of ["mnli""mnli_mismatched""mnli_matched""qnli""rte""wnli""hans"]
    >>> references = [01]
    >>> predictions = [01]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy'1.0}

    >>> glue_metric = datasets.load_metric('glue''mrpc')  # 'mrpc' or 'qqp'
    >>> references = [01]
    >>> predictions = [01]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy'1.0'f1'1.0}

    >>> glue_metric = datasets.load_metric('glue''stsb')
    >>> references = [0.1.2.3.4.5.]
    >>> predictions = [0.1.2.3.4.5.]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print({"pearson": round(results["pearson"], 2), "spearmanr": round(results["spearmanr"], 2)})
    {'pearson'1.0'spearmanr'1.0}

    >>> glue_metric = datasets.load_metric('glue''cola')
    >>> references = [01]
    >>> predictions = [01]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'matthews_correlation'1.0}

{'accuracy'1.0'f1'1.0}

首先描述了评价指标的使用方法,然后计算评价指标accuracy和f1。

posted on   扫地升  阅读(95)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示