【问题标题】:How to correctly load pretrained model in CatBoost in Python如何在 Python 中正确加载 CatBoost 中的预训练模型
【发布时间】:2019-05-13 12:34:47
【问题描述】:

我已经训练了CatBoostClassifier 来解决我的分类任务。现在我需要保存模型并在另一个应用程序中使用它进行预测。为此,我通过save_model 方法保存了模型,并通过load_model 方法恢复了它。

但是,每次我在恢复的模型中调用 predict 时都会收到错误消息:

CatboostError: There is no trained model to use predict(). Use fit() to train model. Then use predict().

所以看起来我需要再次训练我的模型,而我需要恢复预训练的模型并将其仅用于预测。

我在这里做错了什么?我应该使用一种特殊的方式来加载模型以进行预测吗?

我的训练过程是这样的:

model = CatBoostClassifier(
    custom_loss=['Accuracy'],
    random_seed=42,
    logging_level='Silent',
    loss_function='MultiClass')

model.fit(
    x_train, 
    y_train,
    cat_features=None,
    eval_set=(x_validation, y_validation),
    plot=True)

...

model.save("model.cbm")

我使用以下代码恢复模型:

model = CatBoostClassifier(
    custom_loss=['Accuracy'],
    random_seed=42,
    logging_level='Silent',
    loss_function='MultiClass')
model.load_model("model.cbm")

...


predict = self.model.predict(inputs)

【问题讨论】:

    标签: python catboost


    【解决方案1】:
    # After you train the model using fit(), save like this - 
    model.save_model('model_name')    # extension not required.
    
    # And then, later load - 
    from catboost import CatBoostClassifier
    model = CatBoostClassifier()      # parameters not required.
    model.load_model('model_name')
    
    # Now, try predict().
    

    【讨论】:

      【解决方案2】:

      几个小时后,我偶然找到了解决方案。模型加载在外部 python 模块中实现,然后导入 Jupyter Notebook。原来我只需要重启 Jupyter 内核。

      【讨论】:

        猜你喜欢
        • 2021-10-02
        • 2021-02-19
        • 2020-02-07
        • 2019-07-17
        • 2016-08-17
        • 2019-09-09
        • 2021-07-16
        • 1970-01-01
        • 2021-04-05
        相关资源
        最近更新 更多