准备使用evaluate进行模型效果的统计,按照相关参考资料
pip install evaluate
这一步没有什么问题,于是运行简单示例测试:
import evaluate
metric_name = 'accuracy'
accuracy = evaluate.load(metric_name)
result = accuracy.compute(references=[0,1,0,1], predictions=[1,0,0,1])
print(result)
发现一直卡在 load 迟迟没有结果,控制台也没有显示任何错误,经过逐步 debug 找到问题所在。
首先根据官方的说明
'''
path (str): Path or name of the metric script.
- if ``path`` is a local metric script or a directory containing a local metric script (if the script has the same name as the directory):
-> load the module from the metric script
e.g. ``'./metrics/accuracy'`` or ``'./metrics/accuracy/accuracy.py'``.
- if ``path`` is a metric on the Hugging Face Hub (ex: `glue`, `squad`)
-> load the module from the metric script in the github repository at huggingface/datasets
e.g. ``'accuracy'`` or ``'rouge'``.
'''
如果直接使用指标名称“accuracy”等,程序将会从 huggingface 上下载相应模块到缓存中使用,实际上我的问题就是无法顺利下载【HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /spaces/evaluate-metric/accuracy/resolve/v0.4.0/accuracy.py (Caused by ProxyError('Cannot connect to proxy.', timeout('_ssl.c:1059: The handshake operation timed out')))】(这个错误不会显示ヽ(`Д´)ノ)
因此只能将相关文件下载到本地,采用 local metric script 方法:
打开官方Github GitHub - huggingface/evaluate: 🤗 Evaluate: A library for easily evaluating machine learning models and datasets. 下载 metrics 文件夹,放在测试脚本的目录下,将'accuracy' 改为 './metrics/accuracy',再次运行文件即可得到正确结果
{'accuracy': 0.5}