qlib实践4-代码分析-init_instance_by_config

本文分析了qlib库的init_instance_by_config接口,该接口用于初始化数据集和模型。它首先检查配置文件中的class路径,然后导入并验证模块。get_callable_kwargs函数负责从路径中获取模块并进行参数校验,但并未实际执行初始化操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

init_instance_by_config

这个接口被高度抽象,dataset也从这里取,model也从这里取,那就下去看看

init_instance_by_config

def init_instance_by_config(
    config: InstConf,
    default_module=None,
    accept_types: Union[type, Tuple[type]] = (),
    try_kwargs: Dict = {},
    **kwargs,
) -> Any:

从定义中看出config必填,再看一下InstConf

InstConf = Union[InstDictConf, str, object, Path]

class InstDictConf(TypedDict):
    # class: str  # because class is a keyword of Python. We have to comment it
    kwargs: dict  # It is optional. {} will be used if not given
    module_path: str  # It is optional if module is given in the class

接下来看一下

    if isinstance(config, (str, Path)):
        if isinstance(config, str):
            # path like 'file:///<path to pickle file>/obj.pkl'
            pr = urlparse(config)
            if pr.scheme == "file":
                with open(os.path.join(pr.netloc, pr.path), "rb") as f:
                    return pickle.load(f)
        else:
            with config.open("rb") as f:
                return pickle.load(f)
#如果config是一个str,则用urlparse来解析,
#如果config是一个对象,那么当成pickle文件来读

接下来看关键的,加载class

    klass, cls_kwargs = get_callable_kwargs(config, default_module=default_module)

    try:
        return klass(**cls_kwargs, **try_kwargs, **kwargs)
    except (TypeError,):
        # TypeError for handling errors like
        # 1: `XXX() got multiple values for keyword argument 'YYY'`
        # 2: `XXX() got an unexpected keyword argument 'YYY'
        return klass(**cls_kwargs, **kwargs)

跳入实现函数get_callable_kwargs

这个函数细节不贴了,大致意思是从路径中找module_path,然后import进来,如果没有,则报错。返回对象名称和kwargs。

那么到这里,相当于class和cls_kwargs进行了查找校验。就是说这个函数仅仅从磁盘上找了配置中的路径是否存在,对参数做了校验,并没有执行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值