如何在Azure机器学习中更改MLFlow中的Sklearn Flavors版本?

发布于 2025-02-09 02:36:18 字数 2901 浏览 2 评论 0原文

当我记录训练有素的模型时在推理期间。 中设置conda.yml文件中的sklearn版本

我可以通过在mlflow.sklearn.log_model(conda_env ='my_env')

。 net/urygm.png“ rel =“ nofollow noreferrer”> “在此处输入映像说明”

这是屏幕拍摄需求.txt

但是,mlModel文件中的口味下的Sklearn版本保持不变,这是引起问题的文件:

“在此处输入映像说明”

,我用这是我用来在Azure中创建此mlflow实验的脚本机器学习笔记本。

import mlflow
from sklearn.tree import DecisionTreeRegressor

from azureml.core import Workspace
from azureml.core.model import Model
from azureml.mlflow import register_model


def run_model(ws, experiment_name, run_name, x_train, y_train):
    
    # set up MLflow to track the metrics
    mlflow.set_tracking_uri(ws.get_mlflow_tracking_uri())
    mlflow.set_experiment(experiment_name)  
    
    with mlflow.start_run(run_name=run_name) as run:
        
        # fit model
        regression_model = DecisionTreeRegressor()
        regression_model.fit(x_train, y_train)
    
        # log training score 
        training_score = regression_model.score(x_train, y_train)
        mlflow.log_metric("Training score", training_score)

        my_conda_env = {
                    "name": "mlflow-env",
                    "channels": ["conda-forge"],
                    "dependencies": [
                        "python=3.8.5",
                        {
                            "pip": [
                                "pip",
                                "scikit-learn~=1.0.0",
                                "uuid==1.30",
                                "lz4==4.0.0",
                                "psutil==5.9.0",
                                "cloudpickle==1.6.0",
                                "mlflow",
                            ],
                        },
                    ],
                }

        
        # register the model
        mlflow.sklearn.log_model(regression_model, "model", conda_env=my_conda_env)

    model_uri = f"runs:/{run.info.run_id}/model"
    model = mlflow.register_model(model_uri, "sklearn_regression_model")

if __name__ == '__main__':

    # connect to your workspace
    ws = Workspace.from_config()

    # create experiment and start logging to a new run in the experiment
    experiment_name = "exp_name"

    # mlflow run name
    run_name= '1234'

  
    # get train data
    x_train, y_train  = get_train_data()
    
    run_model(ws, experiment_name, run_name, x_train, y_train)

任何想法如何将mlmodel文件中的风味sklearn版本从“ 0.22.1” ”更改为“ 1.0.0” 在我的脚本中?

预先感谢!

I need to change the flavors "sklearn_version" in mlflow from "0.22.1" to "1.0.0" on azure machine learning when I log my trained model, since this model will be incompatible with the sklearn version that I am using for deployment during inference. I could change the version of sklearn in conda.yml file by setting "conda_env" in

mlflow.sklearn.log_model(conda_env= 'my_env')

enter image description here

here is the screen shot of requirements.txt
enter image description here

however, sklearn version under flavors in MLmodel file remains unchanged and that is the file that causes problem:

enter image description here

and here is script that I use to create this mlflow experiment in azure machine learning notebooks.

import mlflow
from sklearn.tree import DecisionTreeRegressor

from azureml.core import Workspace
from azureml.core.model import Model
from azureml.mlflow import register_model


def run_model(ws, experiment_name, run_name, x_train, y_train):
    
    # set up MLflow to track the metrics
    mlflow.set_tracking_uri(ws.get_mlflow_tracking_uri())
    mlflow.set_experiment(experiment_name)  
    
    with mlflow.start_run(run_name=run_name) as run:
        
        # fit model
        regression_model = DecisionTreeRegressor()
        regression_model.fit(x_train, y_train)
    
        # log training score 
        training_score = regression_model.score(x_train, y_train)
        mlflow.log_metric("Training score", training_score)

        my_conda_env = {
                    "name": "mlflow-env",
                    "channels": ["conda-forge"],
                    "dependencies": [
                        "python=3.8.5",
                        {
                            "pip": [
                                "pip",
                                "scikit-learn~=1.0.0",
                                "uuid==1.30",
                                "lz4==4.0.0",
                                "psutil==5.9.0",
                                "cloudpickle==1.6.0",
                                "mlflow",
                            ],
                        },
                    ],
                }

        
        # register the model
        mlflow.sklearn.log_model(regression_model, "model", conda_env=my_conda_env)

    model_uri = f"runs:/{run.info.run_id}/model"
    model = mlflow.register_model(model_uri, "sklearn_regression_model")

if __name__ == '__main__':

    # connect to your workspace
    ws = Workspace.from_config()

    # create experiment and start logging to a new run in the experiment
    experiment_name = "exp_name"

    # mlflow run name
    run_name= '1234'

  
    # get train data
    x_train, y_train  = get_train_data()
    
    run_model(ws, experiment_name, run_name, x_train, y_train)

Any idea how can change the flavor sklearn version in MLmodel file from "0.22.1" to "1.0.0" in my script?

With many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望喜 2025-02-16 02:36:18

版本的修改必须来自 “ unignts.txt” 。我们需要手动覆盖所需的版本,并将构建移至管道。

手动编辑以下

conda_env = {
    'channels': ['conda-forge'],
    'dependencies': [
        'python=3.8.8',
        'pip'],
    'pip': [
        'mlflow',
        'scikit-learn==0.23.2',
        'cloudpickle==1.6.0'
    ],
    'name': 'mlflow-env'
}
mlflow.sklearn.log_model(model, "my_model", conda_env=conda_env)

应在conda中

name: mlflow-env
channels:
  - conda-forge
dependencies:
- python=3.8.8
- pip
- pip:
  - mlflow
  - scikit-learn==0.23.2
  - cloudpickle==1.6.0

python: 3.8.8
build_dependencies:
  - pip==21.1.3
  - setuptools==57.4.0
  - wheel==0.37.0
dependencies:
  - -r requirements.txt

代码

mlflow
scikit-learn==0.23.2
cloudpickle==1.6.0

The modifications of the versions must be from "requirements.txt". We need to manually override the versions we need and move the build to the pipeline.

Manually edit the following code

conda_env = {
    'channels': ['conda-forge'],
    'dependencies': [
        'python=3.8.8',
        'pip'],
    'pip': [
        'mlflow',
        'scikit-learn==0.23.2',
        'cloudpickle==1.6.0'
    ],
    'name': 'mlflow-env'
}
mlflow.sklearn.log_model(model, "my_model", conda_env=conda_env)

The below code should be in conda.yml

name: mlflow-env
channels:
  - conda-forge
dependencies:
- python=3.8.8
- pip
- pip:
  - mlflow
  - scikit-learn==0.23.2
  - cloudpickle==1.6.0

The following code block must be there in python_env.yaml

python: 3.8.8
build_dependencies:
  - pip==21.1.3
  - setuptools==57.4.0
  - wheel==0.37.0
dependencies:
  - -r requirements.txt

The following thing must be there in requirements.txt

mlflow
scikit-learn==0.23.2
cloudpickle==1.6.0
狼性发作 2025-02-16 02:36:18

我终于能够解决这个问题。显然,MLFLOW MLFILE中的口味使用工作空间中安装的Scikit-Learn版本。我需要做的就是从工作区内的CLI升级Scikit-Learn。

  1. 在Azure Machine Learning Notebook中打开CLI
  2. 贴上您预定的风味包装。我的案例是Scikit-Learn,就我而言,我使用1.0.2:
 pip install --upgrade scikit-learn==1.0.2
  1. 在MLFlow中登录模型,通过再运行训练脚本一次。

I was finally able to solve this issue. Apparently flavors within mlflow MLfile use the version of installed scikit-learn within the workspace. all I needed to to was to upgrade the scikit-learn from cli within workspace.

  1. open cli in azure machine learning notebooks
  2. upgarde your intended flavor package. My case was scikit-learn, in my case I used 1.0.2:
 pip install --upgrade scikit-learn==1.0.2
  1. log your model in mlflow by running your training script one more time.
    enter image description here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文