gpt4 book ai didi

mlflow - 自定义python模型: succeed to load but fail to predict/serve

转载 作者:行者123 更新时间:2023-12-04 11:27:23 26 4
gpt4 key购买 nike

我有一个自定义的 python 模型,它基本上设置了 scikit-learn 估计器的几个扰动。我确实成功地使用 mlflow run project_directory 运行了该项目CLI,使用 save_model() 保存模型陈述。它出现在仪表板上,带有 mlflow ui .我什至可以在我的 main.py 中加载保存的模型脚本并预测 pandas.DataFrame 没有任何问题。

当我尝试 mlflow models serve -m project/models/run_id 时,我的问题出现了的 mlflow models predict -m project/models/run_id -i data.json .我收到以下错误:
ModuleNotFoundError: No module named 'multi_model'
在 MLflow 文档中,没有提供自定义模型的示例,因此我无法弄清楚如何解决此依赖性问题。这是我的项目树:

project/
├── MLproject
├── __init__.py
├── conda.yaml
├── loader.py
├── main.py
├── models
│   └── 0ef267b0c9784a118290fa1ff579adbe
│   ├── MLmodel
│   ├── conda.yaml
│   └── python_model.pkl
├── multi_model.py
multi_model.py :
import numpy as np
from mlflow.pyfunc import PythonModel
from sklearn.base import clone

class MultiModel(PythonModel):

def __init__(self, estimator=None, n=10):
self.n = n
self.estimator = estimator

def fit(self, X, y=None):
self.estimators = []
for i in range(self.n):
e = clone(self.estimator)
e.set_params(random_state=i)
X_bootstrap = X.sample(frac=1, replace=True, random_state=i)
y_bootstrap = y.sample(frac=1, replace=True, random_state=i)
e.fit(X_bootstrap, y_bootstrap)
self.estimators.append(e)
return self

def predict(self, context, X):
return np.stack([
np.maximum(0, self.estimators[i].predict(X))
for i in range(self.n)], axis=1
)
main.py :
import os
import click
from sklearn.ensemble import RandomForestRegressor
import mlflow.pyfunc
import multi_model

@click(...) # define the click options according to MLproject file
def run(next_week, window_size, nfold):
train = loader.load(start_week, current_week)
x_train, y_train = train.drop(columns=['target']), train['target']

model = multi_model.MultiModel(RandomForestRegressor())

with mlflow.start_run() as run:
model.fit(x_train, y_train)
model_path = os.path.join('models', run.info.run_id)
mlflow.pyfunc.save_model(
path=model_path,
python_model=model,
)

if __name__ == '__main__':
run()

最佳答案

问题解决:在main.py ,只需更新 save_model()命令:

mlflow.pyfunc.save_model(
path=model_path,
python_model=model,
code_path=['multi_model.py'],
conda_env={
'channels': ['defaults', 'conda-forge'],
'dependencies': [
'mlflow=1.2.0',
'numpy=1.16.5',
'python=3.6.9',
'scikit-learn=0.21.3',
'cloudpickle==1.2.2'
],
'name': 'mlflow-env'
}
)

关于mlflow - 自定义python模型: succeed to load but fail to predict/serve,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58199221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com