Comparing and Managing Runs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import 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}")
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.
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.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
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
Comparing and Managing Runs
Desliza para mostrar el menú
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import 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}")
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.
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.
¡Gracias por tus comentarios!