0Pricing
Machine Learning Academy · レッスン

MLflowによる実験管理:パラメータ、指標、アーティファクトの記録

mlflow.log_paramとmlflow.log_metricを使って学習スクリプトに計測処理を組み込み、MLflow UIを起動して、ハイパーパラメータ設定ごとの実行結果を比較します。

「MLflowによる実験管理:パラメータ、指標、アーティファクトの記録」はCoddyKit上の無料Machine Learning Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMachine Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Machine Learning Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Experiment Tracking Matters

When you train multiple models with different hyperparameters, it is easy to lose track of which run achieved the best result. Experiment tracking automatically records every training run's parameters, metrics, and code version in a searchable database. Without it, teams resort to messy spreadsheets or simply forget what worked. MLflow is the industry-standard open-source tool for tracking ML experiments and managing the model lifecycle.

Installing and Starting MLflow

MLflow is a pure Python package that requires no external database for getting started — it stores runs locally in an mlruns/ directory. Install with pip, then launch the tracking UI to visualise runs in a browser. The UI shows all experiments, each run's parameters and metrics, and lets you compare runs side-by-side with interactive charts.

# Install MLflow
# pip install mlflow scikit-learn

import mlflow
import mlflow.sklearn

# Start the tracking UI (run this in a terminal):
# mlflow ui --host 0.0.0.0 --port 5000
# Then open http://localhost:5000

# Check MLflow version
print('MLflow version:', mlflow.__version__)

# Default tracking URI stores to ./mlruns
print('Tracking URI:', mlflow.get_tracking_uri())

Creating Experiments

MLflow organises runs into experiments — logical groupings of related runs. Create a named experiment with mlflow.set_experiment. All subsequent runs belong to this experiment. Use descriptive names that identify the project and date, e.g., 'random_forest_imdb_2024'. Each run within an experiment gets a unique ID, a human-readable name, and its own parameter/metric/artifact store.

import mlflow

# Create or switch to an experiment
experiment_name = 'sentiment_classification'
mlflow.set_experiment(experiment_name)

# List all experiments
for exp in mlflow.search_experiments():
    print(f'ID: {exp.experiment_id}  Name: {exp.name}')

# Get current experiment info
experiment = mlflow.get_experiment_by_name(experiment_name)
print('Artifact location:', experiment.artifact_location)

Logging Parameters with mlflow.log_param

Parameters are the configuration choices made before training begins: hyperparameters like learning rate, number of estimators, and regularisation strength. Log them with mlflow.log_param(key, value) inside an active run context. Parameters are immutable once logged — they define the experiment setup and help you filter runs to find the configuration that worked best.

import mlflow
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

with mlflow.start_run(run_name='rf_baseline'):
    n_estimators = 100
    max_depth = 5
    mlflow.log_param('n_estimators', n_estimators)
    mlflow.log_param('max_depth', max_depth)
    mlflow.log_param('random_state', 42)

    clf = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
    clf.fit(X_train, y_train)
    print('Params logged.')

Logging Metrics with mlflow.log_metric

Metrics are the quantitative results produced during and after training: accuracy, F1, loss values. Log them with mlflow.log_metric(key, value) or mlflow.log_metric(key, value, step=i) to track progress across epochs. Logging metrics at each epoch lets MLflow plot learning curves in the UI, making it easy to compare convergence speed across runs.

import mlflow
from sklearn.metrics import accuracy_score, f1_score

with mlflow.start_run(run_name='rf_run_1'):
    mlflow.log_param('n_estimators', 100)
    mlflow.log_param('max_depth', 5)

    # Train model (abbreviated)
    clf = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=42)
    clf.fit(X_train, y_train)
    preds = clf.predict(X_test)

    acc = accuracy_score(y_test, preds)
    f1 = f1_score(y_test, preds)

    mlflow.log_metric('test_accuracy', acc)
    mlflow.log_metric('test_f1', f1)
    print(f'Logged accuracy={acc:.4f}, f1={f1:.4f}')

Logging Artifacts

Artifacts are files associated with a run: trained model weights, plots, confusion matrices, or feature importance charts. Log them with mlflow.log_artifact(local_path). MLflow stores the file in the run's artifact directory and makes it downloadable from the UI. Logging the confusion matrix image alongside metrics gives reviewers a complete picture without diving into code.

import mlflow
import matplotlib.pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay
import numpy as np

with mlflow.start_run():
    clf.fit(X_train, y_train)
    preds = clf.predict(X_test)

    # Save confusion matrix as an artifact
    fig, ax = plt.subplots(figsize=(5, 4))
    ConfusionMatrixDisplay.from_predictions(y_test, preds, ax=ax)
    plt.tight_layout()
    plt.savefig('/tmp/confusion_matrix.png')
    plt.close()

    mlflow.log_artifact('/tmp/confusion_matrix.png')
    print('Artifact logged.')

Auto-logging with mlflow.sklearn.autolog

For scikit-learn, MLflow provides auto-logging that captures all parameters, metrics, and the trained model automatically without any manual log calls. Simply call mlflow.sklearn.autolog() before fitting. Auto-logging supports XGBoost, LightGBM, PyTorch, TensorFlow, and more with similar one-line activation. It is the fastest way to start tracking without modifying existing training code.

import mlflow
import mlflow.sklearn
from sklearn.ensemble import GradientBoostingClassifier

# Enable auto-logging -- no manual log calls needed
mlflow.sklearn.autolog()

with mlflow.start_run(run_name='gb_autolog'):
    clf = GradientBoostingClassifier(n_estimators=200, max_depth=3, learning_rate=0.05)
    clf.fit(X_train, y_train)
    # MLflow automatically logs:
    # - All constructor params
    # - Training accuracy
    # - Model artifact
    print('Auto-logged run complete.')

Comparing Runs in the MLflow UI

After several runs with different hyperparameters, open the MLflow UI at http://localhost:5000. Select multiple runs with checkboxes and click Compare to view them side by side. The comparison page shows a parallel coordinates plot where each axis is a parameter or metric, making it easy to spot which parameter combination led to the highest accuracy. You can also sort the run table by any metric column.

import mlflow

# Search runs programmatically (useful in CI/CD pipelines)
runs = mlflow.search_runs(
    experiment_names=['sentiment_classification'],
    order_by=['metrics.test_accuracy DESC']
)

if not runs.empty:
    best_run = runs.iloc[0]
    print('Best run ID:', best_run['run_id'])
    print('Best accuracy:', best_run['metrics.test_accuracy'])
    print('n_estimators:', best_run['params.n_estimators'])
    print('max_depth:', best_run['params.max_depth'])

Logging Models with mlflow.sklearn.log_model

Beyond logging model files as generic artifacts, use mlflow.sklearn.log_model to log the model in MLflow's standardised format. This format includes the model object, its flavour metadata, and an automatically generated MLmodel config file. The standardised format enables one-click deployment to local REST servers (mlflow models serve) and cloud platforms like Azure ML and AWS SageMaker.

import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier

with mlflow.start_run(run_name='rf_logged_model'):
    clf = RandomForestClassifier(n_estimators=100, random_state=42)
    clf.fit(X_train, y_train)

    mlflow.log_metric('accuracy', accuracy_score(y_test, clf.predict(X_test)))

    # Log in sklearn flavour -- enables mlflow models serve
    mlflow.sklearn.log_model(
        sk_model=clf,
        artifact_path='random_forest',
        registered_model_name='SentimentRF'
    )
    print('Model registered in Model Registry.')

Tagging Runs for Organisation

MLflow runs can have tags: key-value metadata that goes beyond structured parameters and metrics. Use tags to store the dataset version, code commit hash, experimenter name, or notes about what changed. Tags are searchable and filterable in the UI. A good tagging convention — like always logging dataset_version and git_sha — ensures every run is reproducible and auditable months later.

import mlflow
import subprocess

def get_git_sha():
    try:
        return subprocess.check_output(
            ['git', 'rev-parse', 'HEAD'], text=True
        ).strip()
    except Exception:
        return 'unknown'

with mlflow.start_run():
    mlflow.set_tag('dataset_version', 'imdb_v2')
    mlflow.set_tag('git_sha', get_git_sha())
    mlflow.set_tag('author', 'mehmet.canker')
    mlflow.set_tag('notes', 'Testing higher max_depth after previous plateau')
    mlflow.log_param('max_depth', 8)
    print('Run tagged.')

Remote Tracking Server Setup

For team collaboration, point MLflow at a remote tracking server instead of the local mlruns/ folder. Set the tracking URI to your server's URL before any MLflow calls. All team members log to the same server, see each other's runs, and access shared artifacts. The server backend can use a PostgreSQL database for metadata and S3/GCS for artifact storage, providing enterprise-grade durability and access control.

import mlflow
import os

# Point to a remote MLflow tracking server
os.environ['MLFLOW_TRACKING_URI'] = 'http://mlflow.yourcompany.com:5000'
os.environ['MLFLOW_TRACKING_USERNAME'] = 'data_team'
os.environ['MLFLOW_TRACKING_PASSWORD'] = 'secret'

mlflow.set_tracking_uri(os.environ['MLFLOW_TRACKING_URI'])

# All subsequent mlflow calls go to the remote server
mlflow.set_experiment('shared_team_experiment')
with mlflow.start_run():
    mlflow.log_param('model_type', 'xgboost')
    mlflow.log_metric('auc', 0.94)
    print('Logged to remote server.')

Quick Check

Test your understanding of Machine Learning with Python concepts from this lesson.

Lesson Recap

In this lesson you learned: MLflow tracks parameters, metrics, and artifacts for every training run in a searchable database, mlflow.sklearn.autolog() captures everything automatically without manual log calls, and logged models in the sklearn flavour can be served as REST APIs or promoted through the Model Registry. Next up we build reproducible Docker containers for ML training to eliminate environment differences between machines.

よくある質問

「MLflowによる実験管理:パラメータ、指標、アーティファクトの記録」レッスンは無料ですか?

はい。「MLflowによる実験管理:パラメータ、指標、アーティファクトの記録」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Machine Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Machine Learning Academyコースには全4レッスンが含まれています。

「MLflowによる実験管理:パラメータ、指標、アーティファクトの記録」で何を学びますか?

mlflow.log_paramとmlflow.log_metricを使って学習スクリプトに計測処理を組み込み、MLflow UIを起動して、ハイパーパラメータ設定ごとの実行結果を比較します。 ブラウザで直接実行するハンズオンコードでMachine Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Machine Learning Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMachine Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「MLflowによる実験管理:パラメータ、指標、アーティファクトの記録」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMachine Learning Academyレッスンでコードを書いて実行できますか?

はい。すべてのMachine Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. MLflowによる実験管理:パラメータ、指標、アーティファクトの記録
  2. ML向けDockerによる再現可能な環境
  3. モデルレジストリ:ステージング、本番、アーカイブ
  4. GitHub Actionsによる自動再学習パイプライン
← Machine Learning Academyに戻る