Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Comparing and Managing Runs | Experiment Tracking and Versioning
MLOps for Machine Learning Engineers

bookComparing and Managing Runs

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
import logging import mlflow import mlflow.sklearn from mlflow.models import infer_signature from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s") # Load data iris = load_iris() X_train, X_test, y_train, y_test = train_test_split( iris.data, iris.target, test_size=0.2, random_state=42 ) # Optionally set/announce experiment experiment_name = "IrisRandomForest" mlflow.set_experiment(experiment_name) logging.info(f"Using experiment: {experiment_name}") # Define different hyperparameter sets hyperparams_list = [ {"n_estimators": 50, "max_depth": 2}, {"n_estimators": 100, "max_depth": 3}, {"n_estimators": 150, "max_depth": 4} ] for params in hyperparams_list: run_name = f"rf_n{params['n_estimators']}_d{params['max_depth']}" logging.info(f"Starting run: {run_name} with params: {params}") with mlflow.start_run(run_name=run_name) as run: clf = RandomForestClassifier( n_estimators=params["n_estimators"], max_depth=params["max_depth"], random_state=42 ) clf.fit(X_train, y_train) preds = clf.predict(X_test) acc = accuracy_score(y_test, preds) logging.info(f"Accuracy: {acc:.4f}") # Log hyperparameters and metrics mlflow.log_params(params) mlflow.log_metric("accuracy", acc) # Log the model with input example and signature to avoid warnings input_example = X_test[:5] signature = infer_signature(X_test, preds) mlflow.sklearn.log_model(clf, "model", input_example=input_example, signature=signature) run_id = run.info.run_id artifact_uri = mlflow.get_artifact_uri() logging.info(f"Run ID: {run_id}") logging.info(f"Artifact URI: {artifact_uri}")
copy

After logging multiple runs with different hyperparameters, you can compare results using MLflow's UI or API. To use the MLflow UI, start it by running mlflow ui in your terminal. The UI displays all runs, their parameters, and metrics, letting you sort or filter by accuracy, hyperparameter values, or tags. You can select multiple runs to compare their performance side by side and choose the best model based on your evaluation metric. Alternatively, you can use the MLflow Python API to search for runs and retrieve the best one programmatically, which is useful for automating model selection in production workflows.

Note
Note

Consistent naming and tagging of runs make large projects manageable. Use descriptive run names and tags to track experiment purpose, dataset version, or hyperparameter search group. This practice helps you quickly identify and compare relevant runs as your project grows.

question mark

Which of the following statements accurately describe how you can compare and select the best run in MLflow using the UI and Python API?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

How do I start the MLflow UI and access it in my browser?

Can you show me how to compare runs programmatically using the MLflow API?

What should I look for when choosing the best model from the runs?

Awesome!

Completion rate improved to 6.25

bookComparing and Managing Runs

Свайпніть щоб показати меню

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
import logging import mlflow import mlflow.sklearn from mlflow.models import infer_signature from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s") # Load data iris = load_iris() X_train, X_test, y_train, y_test = train_test_split( iris.data, iris.target, test_size=0.2, random_state=42 ) # Optionally set/announce experiment experiment_name = "IrisRandomForest" mlflow.set_experiment(experiment_name) logging.info(f"Using experiment: {experiment_name}") # Define different hyperparameter sets hyperparams_list = [ {"n_estimators": 50, "max_depth": 2}, {"n_estimators": 100, "max_depth": 3}, {"n_estimators": 150, "max_depth": 4} ] for params in hyperparams_list: run_name = f"rf_n{params['n_estimators']}_d{params['max_depth']}" logging.info(f"Starting run: {run_name} with params: {params}") with mlflow.start_run(run_name=run_name) as run: clf = RandomForestClassifier( n_estimators=params["n_estimators"], max_depth=params["max_depth"], random_state=42 ) clf.fit(X_train, y_train) preds = clf.predict(X_test) acc = accuracy_score(y_test, preds) logging.info(f"Accuracy: {acc:.4f}") # Log hyperparameters and metrics mlflow.log_params(params) mlflow.log_metric("accuracy", acc) # Log the model with input example and signature to avoid warnings input_example = X_test[:5] signature = infer_signature(X_test, preds) mlflow.sklearn.log_model(clf, "model", input_example=input_example, signature=signature) run_id = run.info.run_id artifact_uri = mlflow.get_artifact_uri() logging.info(f"Run ID: {run_id}") logging.info(f"Artifact URI: {artifact_uri}")
copy

After logging multiple runs with different hyperparameters, you can compare results using MLflow's UI or API. To use the MLflow UI, start it by running mlflow ui in your terminal. The UI displays all runs, their parameters, and metrics, letting you sort or filter by accuracy, hyperparameter values, or tags. You can select multiple runs to compare their performance side by side and choose the best model based on your evaluation metric. Alternatively, you can use the MLflow Python API to search for runs and retrieve the best one programmatically, which is useful for automating model selection in production workflows.

Note
Note

Consistent naming and tagging of runs make large projects manageable. Use descriptive run names and tags to track experiment purpose, dataset version, or hyperparameter search group. This practice helps you quickly identify and compare relevant runs as your project grows.

question mark

Which of the following statements accurately describe how you can compare and select the best run in MLflow using the UI and Python API?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3
some-alt