Log Metrics¶
In MLflow, you can log various types of data related to your experiments and runs. These logged data are crucial for tracking and understanding your machine learning processes. The main types of data that can be logged in MLflow include:
Tags and Notes: You can add tags and notes to your "runs" to provide additional information and context about what you're doing in that specific experiment. This makes it easier to search and understand your "runs" in the future.
Parameters: These are configurable values used in the execution of a "run." For example, the learning rate, maximum depth of a decision tree, or any other value that affects the behavior of your model.
Metrics: Metrics are quantitative measures of your model's performance. You can log metrics such as accuracy, mean squared error (MSE), logarithmic loss, etc. These metrics help you evaluate and compare the performance of different "runs" and approaches.
Logging these types of data in MLflow helps you maintain a detailed record of your experiments and enables you to analyze, compare, and effectively share your results. This is essential for the development of machine learning models as it allows you to understand which approaches work best and how to improve your work over time.
import mlflow
EXPERIMENT_NAME = "mlflow-demo" # ❗ make sure this experiment exists
RUN_NAME = "run-with-metrics"
experiment_id = mlflow.get_experiment_by_name(EXPERIMENT_NAME).experiment_id
with mlflow.start_run(
experiment_id=experiment_id,
run_name=RUN_NAME,
) as run:
# set the tags
mlflow.set_tags({
"model": "linear-regression",
"author": "mlops-cookbook",
})
# Log a parameter (key-value pair)
# Log the model parameters
mlflow.log_param("random_seed", 42)
mlflow.log_param("train_size", 0.7)
# Model training code here ...
# Log a metric; metrics can be updated throughout the run
mlflow.log_metric("precision", 0.92)
mlflow.log_metric("recall", 0.96)
# Print the run ID
print(f"Run ID: {run.info.run_id}")
Run ID: f7773173fca64d02bc55974d3c7762bb