Machine learning development requires saving hyperparameters, metrics, and artifacts for each run to compare results. The MLflow platform logs key metrics and registers trained models in a central registry. We will install MLflow, run a script to log parameters, and register the model.
Let's install the
mlflowand
scikit-learnlibraries to conduct and log a test experiment. 📦
pip install mlflow scikit-learn
The dependencies for managing ML experiments have been successfully installed. ✅
Now, let's create a Python script called
train.pythat will train a simple model, log metrics, and save it to MLflow. 🐍
import mlflow
from sklearn.ensemble import RandomForestClassifier
mlflow.set_experiment("demo_experiment")
with mlflow.start_run():
params = {"n_estimators": 100, "max_depth": 5}
mlflow.log_params(params)
model = RandomForestClassifier(**params)
mlflow.log_metric("accuracy", 0.95)
mlflow.sklearn.log_model(model, "rf_model")
The training script and metric logging are set up and ready to be executed. 🚀
Let's run the training script to capture the results and parameters in the local MLflow storage.
python3 train.py
The experiment has been successfully completed, and the parameters and model artifacts have been saved. 📊
# verification (check for registered runs in MLflow)
mlflow runs list --experiment-name demo_experiment
Expected output:
demo_experiment ... FINISHED
# cleanup (remove the generated directory with artifacts and the script)
rm -rf mlruns train.py
Using MLflow helps avoid chaos when tuning hyperparameters and ensures reproducibility of results. Deploy an MLflow server on a separate host for the entire team to collaborate on the project. 🌐
#MLflow #MachineLearning #Python #DataScience #MLOps #AI
✨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk
⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A