在执行 RecBole 的 run_hyper.py 时,遇到 AttributeError: module ‘ray.tune’ has no attribute ‘report’,记录解决方法。
目录
1. Ray
1.1 介绍
引用 Ray 官方介绍。
Ray is an open-source unified framework for scaling AI and Python applications like machine learning. It provides the compute layer for parallel processing so that you don’t need to be a distributed systems expert.
Ray 是一个开源统一框架,用于扩展 AI 和 Python 应用程序(如机器学习)。它提供了用于并行处理的计算层,因此您无需成为分布式系统专家。
1.2 安装
1.2.1 机器学习应用程序
pip install -U "ray[data,train,tune,serve]"
# For reinforcement learning support, install RLlib instead.
# pip install -U "ray[rllib]"
1.2.2 一般 Python 应用程序
pip install -U "ray[default]"
# If you don't want Ray Dashboard or Cluster Launcher, install Ray with minimal dependencies instead.
# pip install -U "ray"
2. 问题与解决
2.1 问题
问题出现在 recbole.quick_start.quick_start.objective_function 方法中的 tune.report(**test_result)。
错误提示为:
AttributeError: module ‘ray.tune’ has no attribute ‘report’
2.2 解决
Ray 官方文档给出了新的接口 API。
解决方案是先 from ray import train,然后通过 train.report(metrics: Dict, *, checkpoint: Checkpoint | None = None) → None 来执行。即更改为:
from ray import train
def objective_function(config_dict=None, config_file_list=None, saved=True):
...more code
test_result = trainer.evaluate(test_data, load_best_model=saved)
train.report(**test_result)
...more code
注意,这里 report 方法接收 Dict 参数,**test_result 中 ** 表示解析字典。