MLflow-projecten uitvoeren

Introductie tot MLflow

Weston Bassler

Senior MLOps Engineer

API en command line

Workflows

Introductie tot MLflow

Projects-API

mlflow.projects

mlflow.projects.run()

  • uri - URI naar MLproject-bestand

  • entry_point - Entry point om te starten vanuit MLproject

  • experiment_name - Experiment om de training-run te tracken

  • env_manager - Python-omgevingsmanager: local of virtualenv

# Voer MLflow-project uit
mlflow.projects.run(

uri='./',
entry_point='main',
experiment_name='My Experiment',
env_manager='virtualenv'
)
Introductie tot MLflow

MLproject

name: salary_model
python_env: python_env.yaml
entry_points:
  main:
    command: "python train_model.py"
Introductie tot MLflow

train_model.py

# Importeer libraries en modules
import mlflow
import mlflow.sklearn
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

# Trainingsdata df = pd.read_csv('Salary_predict.csv') X = df[["experience", "age", "interview_score"]] y = df[["Salary"]]
Introductie tot MLflow

train_model.py

# Train-test-split 
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7,
                                                    random_state=0)

# Zet autologging aan voor Scikit-learn flavor mlflow.sklearn.autolog() # Train model lr = LinearRegression() lr.fit(X_train, y_train)
Introductie tot MLflow

Projecten uitvoeren

# Importeer MLflow-module
import mlflow

# Voer lokaal project uit mlflow.projects.run(uri='./', entry_point='main', experiment_name='Salary Model')
Introductie tot MLflow

Run-output

# Voer lokaal project uit
mlflow.projects.run(uri='./', entry_point='main', 
                    experiment_name='Salary Model')
2023/04/02 14:33:23 INFO mlflow.projects: 'Salary Model' does not exist. 
Creating a new experiment

2023/04/02 14:33:23 INFO mlflow.utils.virtualenv: Installing python 3.10.8 if it does not exist 2023/04/02 14:33:23 INFO mlflow.utils.virtualenv: Creating a new environment /.mlflow/envs/mlflow-44f5094bba686a8d4a5c772 created virtual environment CPython3.10.8.final.0-64 in 236ms 2023/04/02 14:33:23 INFO mlflow.utils.virtualenv: Installing dependencies
Introductie tot MLflow

Run-output

2023/04/02 14:33:59 INFO mlflow.projects.backend.local: === Running command 
'source /.mlflow/envs/mlflow-44f5094bba686a8d4a5c772/bin/activate && python 
train_model.py' in run with ID '562916d45aeb48ec84c1c393d6e3f5b6' ===

2023/04/02 14:34:34 INFO mlflow.projects: === Run (ID '562916d45aeb48ec84c1c393d6e3f5b6') succeeded ===
Introductie tot MLflow

MLflow Tracking

MLflow Tracking-UI

Introductie tot MLflow

Command line

mlflow run
  • --entry-point - Entry point om run te starten vanuit MLproject

  • --experiment-name - Experiment om de training-run te tracken

  • --env-manager - Python-omgevingsmanager: local of virtualenv

  • URI - URI naar MLproject-bestand

Introductie tot MLflow

Run-commando

# Voer main-entry point uit in experiment Salary Model
mlflow run --entry-point main --experiment-name "Salary Model" ./
2023/04/02 15:23:34 INFO mlflow.utils.virtualenv: Installing python 3.10.8 if it 
does not exist
2023/04/02 15:23:34 INFO mlflow.utils.virtualenv: Environment 
/.mlflow/envs/mlflow-44f5094bba686a8d4a5c772 already exists

2023/04/02 15:23:34 INFO mlflow.projects.backend.local: === Running command 'source /Users/weston/.mlflow/envs/mlflow-44f5094bba686a8d4a5c772/bin/activate && python train_model.py' in run with ID 'da5b37b6f53245e5bca59ba8ed6d7dc1' ===
2023/04/02 15:23:38 INFO mlflow.projects: === Run (ID 'da5b37b6f53245e5bca59ba8ed6d7dc1') succeeded ===
Introductie tot MLflow

Laten we oefenen!

Introductie tot MLflow

Preparing Video For Download...