0
0
MLOpsdevops~5 mins

MLflow setup and basics in MLOps - Commands & Configuration

Choose your learning style9 modes available
Introduction
MLflow helps you keep track of your machine learning experiments. It solves the problem of losing track of which model settings gave the best results by saving all details in one place.
When you want to compare different versions of a machine learning model easily.
When you need to save model parameters and results automatically during training.
When you want to share your model experiments with teammates.
When you want to deploy a model and keep track of its versions.
When you want to organize your machine learning workflow with clear records.
Commands
This command installs MLflow on your system so you can use it to track experiments.
Terminal
pip install mlflow
Expected OutputExpected
Collecting mlflow Downloading mlflow-2.7.0-py3-none-any.whl (17.0 MB) Installing collected packages: mlflow Successfully installed mlflow-2.7.0
This command starts the MLflow web interface locally so you can see your experiments in a browser.
Terminal
mlflow ui
Expected OutputExpected
2024/06/01 12:00:00 INFO mlflow.server: Starting MLflow server... 2024/06/01 12:00:00 INFO mlflow.server: Listening at http://127.0.0.1:5000
This runs your training script which uses MLflow to log parameters and results automatically.
Terminal
python train_model.py
Expected OutputExpected
Training started... Logged parameters and metrics to MLflow. Training completed successfully.
This command lists all MLflow experiments you have created or logged runs to.
Terminal
mlflow experiments list
Expected OutputExpected
Experiment ID Name 0 Default 1 My First Experiment
Key Concept

If you remember nothing else from this pattern, remember: MLflow tracks your machine learning experiments automatically so you never lose important details.

Code Example
MLOps
import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load data
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)

# Start MLflow run
with mlflow.start_run():
    # Set parameters
    n_estimators = 100
    max_depth = 3
    mlflow.log_param("n_estimators", n_estimators)
    mlflow.log_param("max_depth", max_depth)

    # Train model
    clf = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
    clf.fit(X_train, y_train)

    # Predict and evaluate
    preds = clf.predict(X_test)
    acc = accuracy_score(y_test, preds)
    mlflow.log_metric("accuracy", acc)

    # Log model
    mlflow.sklearn.log_model(clf, "model")

    print(f"Logged model with accuracy: {acc:.4f}")
OutputSuccess
Common Mistakes
Not installing MLflow before running commands.
The commands will fail because MLflow is not available on the system.
Always run 'pip install mlflow' first to install the tool.
Running 'mlflow ui' without keeping the terminal open.
The MLflow UI server stops when the terminal session ends.
Keep the terminal running or run 'mlflow ui' in a screen or background process.
Not adding MLflow logging code in the training script.
No experiment data will be saved or visible in the MLflow UI.
Add MLflow logging calls like mlflow.log_param() and mlflow.log_metric() in your training code.
Summary
Install MLflow using pip to start tracking experiments.
Run 'mlflow ui' to open the web interface and view experiment results.
Add MLflow logging calls in your training script to save parameters, metrics, and models.
Use MLflow commands to list and manage your experiments.