0
0
MLOpsdevops~5 mins

Logging parameters and metrics in MLOps - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you train a machine learning model, you want to keep track of what settings you used and how well the model performed. Logging parameters and metrics helps you remember these details so you can compare different runs and pick the best model.
When you want to save the learning rate and number of training steps used in a model run.
When you want to record the accuracy or loss of a model after training to see how well it did.
When you want to compare different model versions by their performance numbers.
When you want to keep a history of experiments to avoid repeating the same mistakes.
When you want to share your model results with teammates in a clear, organized way.
Commands
This command installs MLflow, a tool that helps you log parameters and metrics easily during machine learning experiments.
Terminal
pip install mlflow
Expected OutputExpected
Collecting mlflow Downloading mlflow-2.6.0-py3-none-any.whl (18.3 MB) Installing collected packages: mlflow Successfully installed mlflow-2.6.0
This runs a Python script that logs parameters and metrics using MLflow. It shows how to save settings and results for a model training run.
Terminal
python log_params_metrics.py
Expected OutputExpected
2024/06/01 12:00:00 INFO mlflow.tracking.fluent: Experiment with name 'Default' does not exist. Creating a new experiment. 2024/06/01 12:00:00 INFO mlflow.tracking.fluent: Experiment created with ID '1' 2024/06/01 12:00:01 INFO mlflow.tracking.fluent: Run with ID '1234567890abcdef' started 2024/06/01 12:00:01 INFO mlflow.tracking.fluent: Logged parameter: learning_rate = 0.01 2024/06/01 12:00:01 INFO mlflow.tracking.fluent: Logged parameter: epochs = 10 2024/06/01 12:00:01 INFO mlflow.tracking.fluent: Logged metric: accuracy = 0.85 2024/06/01 12:00:01 INFO mlflow.tracking.fluent: Run with ID '1234567890abcdef' ended
Key Concept

If you remember nothing else from this pattern, remember: logging parameters and metrics lets you track what you tried and how well it worked so you can improve your models.

Code Example
MLOps
import mlflow

with mlflow.start_run():
    mlflow.log_param("learning_rate", 0.01)
    mlflow.log_param("epochs", 10)
    mlflow.log_metric("accuracy", 0.85)
print("Parameters and metrics logged successfully.")
OutputSuccess
Common Mistakes
Not starting an MLflow run before logging parameters and metrics.
Without starting a run, MLflow does not know where to save the data, so logging calls fail or data is lost.
Always use mlflow.start_run() as a context manager before logging parameters and metrics.
Logging parameters or metrics with wrong data types, like passing a list instead of a number or string.
MLflow expects parameters as strings or numbers and metrics as numbers; wrong types cause errors.
Convert parameters to strings and metrics to floats or ints before logging.
Summary
Install MLflow to enable logging of parameters and metrics.
Use mlflow.start_run() to begin a logging session.
Log parameters with mlflow.log_param() and metrics with mlflow.log_metric() inside the run.
This process helps track experiment settings and results for better model comparison.