✍️ Exercise: Intro to MLFlow - Part I¶
In this exercise, we will cover the basics of MLFlow. MLFlow is an open-source platform for the complete machine learning lifecycle. It is designed to work with any machine learning library and to be agnostic to the execution environment. It is also designed to be scalable and to support the complete machine learning lifecycle, including experimentation, reproducibility, and deployment.
In this first part, we will cover the following topics:
- How to Install MLFlow.
- How to launch the MLFlow Server.
- How to create a new MLFlow Experiment.
- How to create a new MLFlow Run.
- How to log parameters, metrics, and artifacts.
How to Install MLFlow¶
💡 Remember: We can simply install MLFlow using pip 🎉
pip install mlflow
How to launch the MLFlow Server¶
💡 Remember: After installing MLFlow, we can launch the MLFlow server using the following command in the terminal:
mlflow server
You will see the following output:
[2024-02-21 23:29:52 +0100] [725738] [INFO] Starting gunicorn 21.2.0
[2024-02-21 23:29:52 +0100] [725738] [INFO] Listening at: http://127.0.0.1:5000 (725738)
[2024-02-21 23:29:52 +0100] [725738] [INFO] Using worker: sync
[2024-02-21 23:29:52 +0100] [725739] [INFO] Booting worker with pid: 725739
[2024-02-21 23:29:52 +0100] [725740] [INFO] Booting worker with pid: 725740
[2024-02-21 23:29:53 +0100] [725741] [INFO] Booting worker with pid: 725741
👉 Then, we can access the mlflow server by opening the following URL in a web browser: http://localhost:5000.
Exercise I: Connecting to the MLFlow Server¶
- 👉 Connect to MLFlow using
mlflow.set_tracking_uri()
and set the URI tohttp://localhost:5000
. - 👉 Use
mlflow.search_experiments()
to list all the experiments.
Exercise II: Creating a New MLFlow Experiment¶
- 👉 Create a new MLFlow Experiment using
mlflow.create_experiment()
and set the name tointro-to-mlflow
. - 👉 Check if the experiment was created by using
mlflow.get_experiment_by_name()
. - 👉 Print the experiment ID.
Exercise III: Creating a New MLFlow Run¶
- 👉 Create a new MLFlow Run using
mlflow.start_run()
and set the experiment_id to the ID of theintro-to-mlflow
experiment. - 👉 Check if the run was created by using
run.info.run_id
. - 👉 Print the run_id.
Exercise IV: Logging Tags, Parameters and Metrics¶
Imagine you have the following information about the run:
- model_type: "RandomForest"
- accuracy: 0.85
- max_depth: 10
- precision: 0.90
- learning_rate: 0.01
- recall: 0.80
- 👉 Think. What should you log as a tag, parameter, and metric?
- 👉 Create a new MLFlow Run using
mlflow.start_run()
and set the experiment_id to the ID of theintro-to-mlflow
experiment. - 👉 Log the tags using
mlflow.set_tags()
. - 👉 Log the parameters using
mlflow.log_param()
. - 👉 Log the metrics using
mlflow.log_metric()
.