簡體   English   中英

如何在Python的CatBoost中正確加載預訓練的模型

[英]How to correctly load pretrained model in CatBoost in Python

我已經訓練過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模塊中實現了模型加載,然后將其導入Jupyter Notebook。 原來,我只需要重新啟動Jupyter內核即可。

# 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().

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM