Narayanan's first project on Hugging Face!
This project is experimenting on MLFlow framework.
Task0: Get Started
Welcome to the Track ML Experiments using MLflow project!
In essence, MLflow is a Python package. For your convenience, your workspace already has MLflow installed, so you don’t need to install it.
MLflow provides a CLI that you can use to run the MLflow Tracking Server. The Tracking Server is composed of a web application and REST API that allows you to view and manage your experiments. You can see the following command in the terminal which executes the MLflow server:
mlflow server --host 0.0.0.0 --port 5000
You can access the MLflow UI on the “App Browser” tab or you can open it in a new browser tab by clicking the arrow icon provided next to the URL.
In your workspace, open the Jupyter Notebook named notebook.ipynb, which contains the necessary code to train a classifier for the Iris flower dataset and track the results using MLflow.
Task 1: Create an MLflow Experiment
In this task, you’ll explore MLflow experiments for organizing machine learning projects. An MLflow experiment serves as a workspace to group-related experiment runs, aiding in the effective tracking, comparison, and management of results. Each experiment has a unique name for easy differentiation between projects or studies.
Let’s delve into creating and utilizing MLflow experiments with the following steps:
Import the MLflow library.
Define the MLflow Tracking Server URI as
http://localhost:5000.
Define or create the MLflow experiment named iris-classification.
Task 2: Log Parameters, Metrics, and Artifacts
In this task, you'll create an MLflow Run, which represents an individual execution of a machine learning experiment. It tracks all the information related to that specific execution, such as metrics, parameters, and artifacts. Perform the following steps in the notebook.ipynb file:
Start an MLflow Run.
Log the fig (figure) object as an artifact.
Log the X_train and X_test lengths and the model parameters as parameters.
Log the training_accuracy and test_accuracy as metrics.
Log the classification report as an artifact.
Task 3: Visualize Experiment Result
In this task, you’ll visualize the results of your MLflow Run. MLflow provides a web-based user interface (UI) where you can view and compare the logged metrics and parameters for different Runs within the same experiment. In the previous task, you created an MLflow Run and logged parameters and metrics to that. Now, you’ll visualize the MLflow Run in the MLflow UI.
Click the “iris-classification” experiment on the left side of the MLflow UI. You should be able to see all the Runs of this experiment.
Under the “Run Name” column, click the Run you have created.
It will show a page with the “Parameters” and “Metrics” sections, which allow you to see all the parameters and metrics tracked in this Run respectively.
Task 4: Compare Experiments and Models
In this task, you’ll learn how to compare the results of different machine learning experiments by customizing the MLflow UI as follows:
Click the “Columns” button on top of the “Runs” table.
Uncheck the “Dataset” and “Source” columns. Check the training_accuracy and test_accuracy metrics to add them to the table.
Now, you should be able to see the training_accuracy and test_accuracy metrics as columns in the “Runs” table.
By comparing experiment metrics, you can gain valuable insights into the effectiveness of different approaches and configurations. Through this analysis you can optimize your machine learning workflows, select the best-performing hyperparameters, and make data-driven decisions in your AI projects.
You can also add other dimensions such as parameters.
Task 5: Save and Log Models
In this task, log the trained model in the MLflow Run, which is useful if you want to use the model for inference or batch scoring later. You can use the scikit-learn submodule in the MLflow Model to log your trained model in this task.
Use the following code to complete this task:
mlflow.sklearn.log_model(model, "sklearn", input_example=X_train[:5])
After logging the model, you should be able to see the model files inside the MLflow Run.
The MLflow Run artifact section
Your model is now available as an artifact of this MLflow experiment Run. In the next task, you will register it in the MLflow Model Registry.
Task 6: Version and Manage Models
In this task, you’ll learn how to version and manage machine learning models using MLflow. Versioning models is crucial for reproducibility and ensuring that you can track changes to your models over time. MLflow provides two environment definitions that you can use as a symbolic reference to the versions of your model. The first one is the Staging environment, which is commonly used for testing and validation. The second one is the Production environment, which is used for production deployment. Perform the following steps in this task:
Register your logged model.
Assign the Version 1 of your registered model to the Stage environment.
Finish the MLflow Run.
Congratulations! You have your model assigned to the Staging environment. When you create new versions of your model, you can easily assign them to the Staging or Production environments in the same way you learned in this task.
Task 7: Use MLflow Model for Batch Inference
In this task, load the MLflow Model for batch scoring. In a batch job pipeline, you can load an MLflow Model and perform predictions over large volumes of data. After loading your model, you can run the next cell in the notebook.ipynb file that will predict the X_test.
Good work! It’s especially useful when you have a workflow that processes a huge amount of data in a specific schedule.
Task 8: Deploy MLflow Model for Real-Time Inference
In this task, serve the MLflow Model you created as a REST API for real-time inference. With the model deployed, you can use any programming language or tool that can make HTTP requests to interact with the model.
Perform the following steps in this task:
Open a “New Terminal” in the workspace.
Use the MLflow CLI to start the web server.
mlflow models serve -m models:/iris-classifier/staging -h 0.0.0.0 -p 8001
Run the cell over the section of this task in the notebook.ipynb file to make predictions using the deployed model’s API.
Task 9: Use Nested MLflow Runs
In this task, you’ll perform nested MLflow Runs. It’s a useful feature provided by MLflow that organizes the parent Runs and child Runs in a tree structure. In the MLflow UI, you can click a parent Run to expand it and see the child Runs.
Perform the following steps in this task:
Using the “Context Manager,” start a new MLflow Run.
Inside the code block of your MLflow Run, start a nested MLflow Run.
Log a parameter named run_name with child_{n} in the value, where n represents the number of your child Runs.
Note: Click the “+” button next to the Run name, which displays both of the nested Runs.
Task 10: Use MLflow Projects
In this task, you’ll use MLflow Projects to package and reproduce machine learning workflows. An MLflow Project is a standardized format for packaging reusable data science code. Each Project is simply a directory with code or a Git repository, making it easy to manage and reproduce machine learning workflows.
Run the MLflow Project with the following arguments:
The uri field with the argument /usercode/wine-quality-classifier (it’s a directory in your workspace).
The experiment_name field with the argument wine-quality-classifier.
The env_manager field with the argument virtualenv.
End
Congratulations on completing your project! You’ve done an excellent job. Applying your skills with hands-on projects like this is a great way to get comfortable with new techniques and technologies.
The techniques you learned in this project can be used to track machine learning experiments effectively, package and version models for reproducibility, and deploy ML models as REST APIs effortlessly. Moreover, you can apply basic HTTP authentication, optimize the utilization and administration of diverse large language modes, and use MLFlow plugins to upskill your MLFlow knowledge.