Method | Scenario |
Automatic recording | Use mainstream frameworks such as sklearn / xgboost / lightgbm / pytorch / tensorflow without manually writing log_*. Parameters, metrics, and models are automatically captured during framework training. |
Manual recording | Customize the training process; record metrics that are not captured by the framework by default, and fully control the timing and content of recording. |
mlflow.autolog() before mlflow.start_run(). During framework training, parameters, metrics, models, and training datasets are automatically captured:import mlflowfrom sklearn.datasets import load_diabetesfrom sklearn.ensemble import RandomForestRegressorfrom sklearn.model_selection import train_test_splitmlflow.set_experiment(experiment_id="3")mlflow.autolog() # Automatic loggingwith mlflow.start_run():db = load_diabetes()X_train, X_test, y_train, y_test = train_test_split(db.data, db.target)rf = RandomForestRegressor(n_estimators=100, max_depth=6)rf.fit(X_train, y_train)# Parameters (n_estimators, max_depth), training metrics, and model artifacts are all automatically recorded.
mlflow.log_param within the with mlflow.start_run() context:import mlflowwith mlflow.start_run(run_name="manual_run"):# Single parametermlflow.log_param("learning_rate", 0.01)mlflow.log_param("batch_size", 64)mlflow.log_param("optimizer", "Adam")# Batch recordingmlflow.log_params({"n_estimators": 100,"max_depth": 6,"max_features": 3,})
key is a string, and its maximum length is specified by the MLflow protocol.value type can be a string, an integer, or a floating-point number.mlflow.log_metric within the with mlflow.start_run() context. You can specify step to plot curves:import mlflowwith mlflow.start_run(run_name="manual_run"):# Single metricmlflow.log_metric("accuracy", 0.92)mlflow.log_metric("auc", 0.88)# Record loss by epoch during trainingfor epoch in range(100):loss = train_one_epoch(...)mlflow.log_metric("loss", loss, step=epoch)# Batch recordingmlflow.log_metrics({"precision": 0.91,"recall": 0.89,"f1": 0.90,})
key can be recorded multiple times and is organized into a time series by step, which is displayed as a line chart in the experiment details chart view.import mlflowwith mlflow.start_run():# Record a single filemlflow.log_artifact("output/eval_report.html")# Record to a subdirectorymlflow.log_artifact("config/params.yaml", artifact_path="configs")# Record an entire directorymlflow.log_artifacts("output/figures", artifact_path="figures")
log_model methods. Common examples:# scikit-learnmlflow.sklearn.log_model(sk_model=model,name="sklearn-model",input_example=X_train, # Provide an input example to automatically generate a signature.)# PyTorchmlflow.pytorch.log_model(model, "model")# Custom Python modelmlflow.pyfunc.log_model(artifact_path="model", python_model=my_model)
input_example= when calling log_model so that MLflow can infer the signature automatically, or to explicitly construct a mlflow.models.signature.ModelSignature before the call. Models without a signature will be rejected during subsequent registration.registered_model_name= parameter to register the model to the Catalog synchronously when calling log_model. For the same model name, the first registration creates V1, and each subsequent registration automatically increments the version number by +1.mlflow.sklearn.log_model(sk_model=model,name="sklearn-model",input_example=X_train,registered_model_name="my_catalog.my_schema.churn_model",)
import mlflowimport mlflow.sklearnfrom sklearn.datasets import make_regressionfrom sklearn.ensemble import RandomForestRegressorfrom sklearn.metrics import mean_squared_errorfrom sklearn.model_selection import train_test_splitmlflow.set_experiment("regression_demo")with mlflow.start_run() as run:X, y = make_regression(n_features=4, n_informative=2, random_state=0, shuffle=False)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)params = {"max_depth": 2, "random_state": 42}model = RandomForestRegressor(**params)model.fit(X_train, y_train)# Record parametersmlflow.log_params(params)# Record metricsy_pred = model.predict(X_test)mlflow.log_metrics({"mse": mean_squared_error(y_test, y_pred)})# Record and register the model (synchronously register it to the Catalog)mlflow.sklearn.log_model(sk_model=model,name="sklearn-model",input_example=X_train,registered_model_name="catalog_name.schema_name.model_name",)
import mlflowwith mlflow.start_run(run_name="Parent Run") as parent:mlflow.log_param("model_type", "RandomForest")for n in [50, 100, 200]:with mlflow.start_run(run_name=f"n_estimators={n}", nested=True):mlflow.log_param("n_estimators", n)# ... Train and record metrics ...
Tab | Content. |
Overview | Basic information about this run. Click to jump to the dataset, model, and code file pages. Edit tags and search parameter configurations and metrics. |
Model Metrics | Display visualization charts of metrics recorded by log_metric (bar charts, line charts, and more). |
System Metrics | System-level monitoring charts for CPU utilization, memory, network, disk, and more |
Model Files | All artifact files of the current Run, with support for browsing, copying, and downloading. |
mlflow.log_param Is Called?mlflow.log_metric(key, value, step=...) instead.step is not passed to mlflow.log_metric(key, value), values are accumulated in the order they are recorded by default. If it is called only once, the curve has only one point. Call log_metric(key, value, step=epoch) by epoch in the training loop to plot a complete curve.input_example= when calling log_model to let MLflow infer the signature automatically, or.mlflow.models.signature.ModelSignature and pass it through the signature= parameter, or.mlflow.autolog() to have the framework automatically log models with signatures.with mlflow.start_run() as run: context manager, which automatically calls mlflow.end_run() to close the Run even when an exception occurs.피드백