Używamy cookies, żeby zwiększyć Twoje doświadczenia na stronie
CodeWorlds

MLflow - śledzenie eksperymentów

MLflow to platforma do zarządzania cyklem życia modeli ML - tracking, packaging, deployment!

Instalacja

1pip install mlflow

Tracking eksperymentów

1import mlflow
2import mlflow.sklearn
3from sklearn.ensemble import RandomForestClassifier
4from sklearn.metrics import accuracy_score
5
6# Ustaw eksperyment
7mlflow.set_experiment("safari-classification")
8
9# Rozpocznij run
10with mlflow.start_run(run_name="random_forest_v1"):
11    # Parametry
12    n_estimators = 100
13    max_depth = 10
14
15    # Loguj parametry
16    mlflow.log_param("n_estimators", n_estimators)
17    mlflow.log_param("max_depth", max_depth)
18
19    # Trenuj model
20    model = RandomForestClassifier(
21        n_estimators=n_estimators,
22        max_depth=max_depth,
23        random_state=42
24    )
25    model.fit(X_train, y_train)
26
27    # Ewaluacja
28    y_pred = model.predict(X_test)
29    accuracy = accuracy_score(y_test, y_pred)
30
31    # Loguj metryki
32    mlflow.log_metric("accuracy", accuracy)
33    mlflow.log_metric("train_samples", len(X_train))
34
35    # Loguj model
36    mlflow.sklearn.log_model(model, "model")
37
38    # Loguj artefakty (pliki)
39    # mlflow.log_artifact("feature_importance.png")
40
41    print(f"Run ID: {mlflow.active_run().info.run_id}")

MLflow UI

1# Uruchom UI
2mlflow ui --port 5000
3
4# Otwórz w przeglądarce: http://localhost:5000

Autologging

1# Automatyczne logowanie dla sklearn
2mlflow.sklearn.autolog()
3
4with mlflow.start_run():
5    model = RandomForestClassifier(n_estimators=100)
6    model.fit(X_train, y_train)
7    # Parametry i metryki logowane automatycznie!
8
9# Autolog dla PyTorch
10mlflow.pytorch.autolog()
11
12# Autolog dla XGBoost
13mlflow.xgboost.autolog()

Model Registry

1# Zarejestruj model
2with mlflow.start_run():
3    model = RandomForestClassifier()
4    model.fit(X_train, y_train)
5
6    # Rejestracja
7    mlflow.sklearn.log_model(
8        model,
9        "model",
10        registered_model_name="SafariClassifier"
11    )
12
13# Załaduj zarejestrowany model
14model_uri = "models:/SafariClassifier/Production"
15loaded_model = mlflow.sklearn.load_model(model_uri)
16predictions = loaded_model.predict(X_test)

Porównywanie eksperymentów

1import mlflow
2
3# Pobierz wszystkie runy z eksperymentu
4experiment = mlflow.get_experiment_by_name("safari-classification")
5runs = mlflow.search_runs(experiment_ids=[experiment.experiment_id])
6
7# Sortuj po accuracy
8best_runs = runs.sort_values("metrics.accuracy", ascending=False)
9print(best_runs[['params.n_estimators', 'params.max_depth', 'metrics.accuracy']].head())
10
11# Załaduj najlepszy model
12best_run_id = best_runs.iloc[0]['run_id']
13best_model = mlflow.sklearn.load_model(f"runs:/{best_run_id}/model")

Kompletny pipeline

1import mlflow
2from sklearn.model_selection import cross_val_score
3import xgboost as xgb
4
5mlflow.set_experiment("safari-xgboost-tuning")
6
7# Hyperparameter search z logowaniem
8param_combinations = [
9    {'max_depth': 3, 'learning_rate': 0.1},
10    {'max_depth': 5, 'learning_rate': 0.1},
11    {'max_depth': 3, 'learning_rate': 0.05},
12    {'max_depth': 5, 'learning_rate': 0.05},
13]
14
15for params in param_combinations:
16    with mlflow.start_run():
17        # Log params
18        mlflow.log_params(params)
19
20        # Train with cross-validation
21        model = xgb.XGBClassifier(**params, n_estimators=100)
22        cv_scores = cross_val_score(model, X_train, y_train, cv=5)
23
24        # Log metrics
25        mlflow.log_metric("cv_mean_accuracy", cv_scores.mean())
26        mlflow.log_metric("cv_std_accuracy", cv_scores.std())
27
28        # Train final model
29        model.fit(X_train, y_train)
30        test_accuracy = model.score(X_test, y_test)
31        mlflow.log_metric("test_accuracy", test_accuracy)
32
33        # Log model
34        mlflow.xgboost.log_model(model, "model")
35
36print("Tuning complete! Check MLflow UI for results.")
Przejdź do CodeWorlds