Bird
Raised Fist0
MLOpsdevops~15 mins

MLflow setup and basics in MLOps - Deep Dive

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Overview - MLflow setup and basics
What is it?
MLflow is a tool that helps you manage machine learning projects easily. It tracks experiments, records results, and organizes models so you can reuse and share them. It works by letting you log data about your training runs and then view or compare them later. This makes machine learning work more organized and less confusing.
Why it matters
Without MLflow, managing machine learning experiments can become messy and error-prone. You might lose track of which model performed best or which settings you used. MLflow solves this by keeping everything in one place, making it easier to reproduce results and collaborate with others. This saves time and reduces mistakes in real projects.
Where it fits
Before learning MLflow, you should understand basic machine learning concepts and how to run training scripts. After MLflow basics, you can explore advanced model deployment, automated pipelines, and cloud-based experiment tracking. MLflow fits into the MLOps journey as the tool that organizes and tracks your machine learning work.
Mental Model
Core Idea
MLflow acts like a smart notebook that automatically records every detail of your machine learning experiments so you never lose track.
Think of it like...
Imagine you are baking different cakes trying new recipes. MLflow is like a kitchen journal where you write down each recipe, ingredients, baking time, and how the cake turned out, so you can always bake the best one again or share it with friends.
┌───────────────────────────────┐
│          MLflow Setup         │
├─────────────┬─────────────────┤
│ Components  │ Description     │
├─────────────┼─────────────────┤
│ Tracking    │ Logs experiments│
│ Server      │ Stores logs     │
│ Projects    │ Organizes code  │
│ Models      │ Manages models  │
└─────────────┴─────────────────┘
Build-Up - 7 Steps
1
FoundationInstalling MLflow and dependencies
🤔
Concept: Learn how to install MLflow and prepare your environment.
To start using MLflow, you first install it using Python's package manager. Run: pip install mlflow This command downloads MLflow and its required parts. You also need Python installed on your computer. After installation, you can check MLflow version by running: mlflow --version
Result
MLflow is installed and ready to use on your system.
Knowing how to install MLflow correctly is the first step to using it effectively and avoiding setup errors.
2
FoundationStarting MLflow tracking server locally
🤔
Concept: Set up a local server to store experiment data.
MLflow uses a tracking server to save experiment info. You can start a local server by running: mlflow server --backend-store-uri sqlite:///mlflow.db --default-artifact-root ./mlruns --host 127.0.0.1 --port 5000 This command creates a local database and folder to save logs and artifacts.
Result
A local MLflow tracking server runs on your machine at http://127.0.0.1:5000.
Running a local server helps you keep all experiment data organized and accessible through a web interface.
3
IntermediateLogging experiments with MLflow API
🤔Before reading on: do you think MLflow automatically tracks all your code changes or do you need to add logging commands? Commit to your answer.
Concept: Learn how to record parameters, metrics, and models during training using MLflow code commands.
In your training script, import MLflow and use its functions to log data: import mlflow with mlflow.start_run(): mlflow.log_param('learning_rate', 0.01) mlflow.log_metric('accuracy', 0.95) mlflow.log_artifact('model.pkl') This records the learning rate, accuracy, and saves the model file to MLflow.
Result
Experiment details are saved and visible in the MLflow UI for later review.
Explicit logging lets you control exactly what information is saved, making your experiments reproducible and comparable.
4
IntermediateUsing MLflow UI to compare runs
🤔Before reading on: do you think MLflow UI shows only the latest run or all past runs? Commit to your answer.
Concept: Explore the web interface to view and compare experiment results visually.
Open your browser and go to http://127.0.0.1:5000. You will see a list of experiment runs with parameters and metrics. You can select multiple runs to compare their performance side-by-side. This helps you find the best model settings quickly.
Result
You can visually analyze and compare all your experiment runs in one place.
A visual interface makes it easier to understand experiment outcomes and share results with teammates.
5
IntermediateOrganizing projects with MLflow Projects
🤔
Concept: Learn how to package your ML code and dependencies for easy sharing and reproducibility.
MLflow Projects use a simple file called MLproject to describe your project: name: MyProject conda_env: conda.yaml entry_points: main: parameters: learning_rate: {type: float, default: 0.01} command: python train.py --lr {learning_rate} This lets others run your project with the same environment and parameters.
Result
Your ML code is packaged with environment info, making it easy to run anywhere.
Packaging projects ensures consistent environments and reduces 'it works on my machine' problems.
6
AdvancedManaging models with MLflow Model Registry
🤔Before reading on: do you think MLflow Model Registry only stores model files or also tracks model versions and stages? Commit to your answer.
Concept: Use MLflow to register, version, and stage machine learning models for deployment.
After training, register your model: from mlflow.tracking import MlflowClient client = MlflowClient() model_uri = 'runs://model' client.create_registered_model('MyModel') client.create_model_version('MyModel', model_uri, '') You can then move models through stages like 'Staging' or 'Production' to manage deployment readiness.
Result
Models are tracked with versions and deployment stages, improving lifecycle management.
Model Registry adds control and safety to deploying ML models in real systems.
7
ExpertScaling MLflow with remote servers and storage
🤔Before reading on: do you think MLflow tracking server can handle multiple users and large data by default? Commit to your answer.
Concept: Learn how to configure MLflow to use remote databases and cloud storage for team collaboration and scalability.
For teams, run MLflow server with a remote database like PostgreSQL: mlflow server --backend-store-uri postgresql://user:pass@host/dbname --default-artifact-root s3://mybucket/mlflow This setup stores metadata in a robust database and artifacts in cloud storage, allowing multiple users to track experiments safely.
Result
MLflow can support team workflows and large-scale projects with reliable storage and access.
Understanding scalable MLflow setups is key for professional MLOps in production environments.
Under the Hood
MLflow works by running a tracking server that stores experiment metadata in a database and artifacts like models or logs in a file system or cloud storage. When you call MLflow logging functions in your code, they send data to this server via an API. The server organizes data by experiments and runs, allowing retrieval and comparison. The UI queries this data to display results. MLflow Projects use environment files and entry points to recreate consistent runs. The Model Registry tracks model versions and stages in the database, enabling lifecycle management.
Why designed this way?
MLflow was designed to be modular and flexible, supporting many ML frameworks and storage backends. Using a server-client model separates experiment tracking from code execution, allowing remote access and collaboration. Storing metadata in databases ensures query efficiency, while artifact storage is decoupled for scalability. This design avoids locking users into specific tools and supports both local and cloud workflows.
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ MLflow Client │──────▶│ Tracking      │──────▶│ Backend Store │
│ (Your Script) │       │ Server (API)  │       │ (Database)    │
└───────────────┘       └───────────────┘       └───────────────┘
         │                      │                      │
         │                      │                      │
         │                      ▼                      ▼
         │               ┌───────────────┐      ┌───────────────┐
         │               │ Artifact Store│      │ Model Registry│
         │               │ (File System/ │      │ (Metadata DB) │
         │               │  Cloud)       │      └───────────────┘
         ▼
┌─────────────────┐
│ MLflow UI       │
│ (Web Interface) │
└─────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does MLflow automatically track every change in your code without any logging commands? Commit to yes or no.
Common Belief:MLflow automatically tracks all code changes and parameters without extra commands.
Tap to reveal reality
Reality:MLflow only tracks what you explicitly tell it to log using its API calls in your code.
Why it matters:Assuming automatic tracking leads to missing important experiment details and makes reproducing results impossible.
Quick: Can MLflow replace your entire machine learning pipeline automation? Commit to yes or no.
Common Belief:MLflow is a full pipeline automation tool that handles data processing, training, and deployment end-to-end.
Tap to reveal reality
Reality:MLflow focuses on experiment tracking, project packaging, and model management but does not automate data pipelines or deployment by itself.
Why it matters:Misusing MLflow as a pipeline tool can cause confusion and incomplete automation, requiring other tools for full MLOps.
Quick: Is it safe to use the default local MLflow server for team collaboration? Commit to yes or no.
Common Belief:The default local MLflow server setup is sufficient for multiple users working together.
Tap to reveal reality
Reality:The local server is single-user and not designed for concurrent access; production teams need remote servers with proper databases and storage.
Why it matters:Using local servers for teams risks data loss, conflicts, and poor scalability.
Quick: Does MLflow Model Registry only store model files without version control? Commit to yes or no.
Common Belief:Model Registry is just a storage place for model files without tracking versions or stages.
Tap to reveal reality
Reality:Model Registry tracks multiple versions of models and their lifecycle stages like staging or production.
Why it matters:Ignoring version control leads to deployment errors and difficulty managing model updates.
Expert Zone
1
MLflow's artifact storage can be configured separately from metadata storage, allowing flexible use of cloud buckets or local disks depending on project needs.
2
The MLflow Projects format supports multiple environment managers like Conda or Docker, enabling reproducible runs across diverse systems.
3
Model Registry integrates with CI/CD pipelines to automate model promotion and deployment, but requires careful permission and stage management.
When NOT to use
MLflow is not suitable when you need full pipeline orchestration or real-time model serving; in those cases, use tools like Kubeflow Pipelines or TensorFlow Serving. Also, for very large-scale experiment tracking, specialized platforms may be more efficient.
Production Patterns
Teams run MLflow tracking servers on cloud VMs with PostgreSQL and S3 storage for reliability. They integrate MLflow with CI pipelines to automatically log runs and register models. Model Registry stages control deployment approvals. Projects are packaged with Docker for consistent environments. The UI is used for experiment review and audit.
Connections
Version Control Systems (e.g., Git)
Both track changes and history of work artifacts over time.
Understanding version control helps grasp how MLflow tracks experiment versions and model changes systematically.
Continuous Integration/Continuous Deployment (CI/CD)
MLflow integrates with CI/CD pipelines to automate model testing and deployment.
Knowing CI/CD concepts clarifies how MLflow fits into automated workflows for reliable ML production.
Scientific Lab Notebooks
MLflow serves as a digital lab notebook for machine learning experiments.
Recognizing MLflow as a lab notebook highlights its role in organizing and documenting experiments for reproducibility.
Common Pitfalls
#1Not starting the MLflow tracking server before logging experiments.
Wrong approach:import mlflow mlflow.log_param('lr', 0.01) mlflow.log_metric('accuracy', 0.9)
Correct approach:Start the server first: mlflow server --backend-store-uri sqlite:///mlflow.db --default-artifact-root ./mlruns Then in code: import mlflow with mlflow.start_run(): mlflow.log_param('lr', 0.01) mlflow.log_metric('accuracy', 0.9)
Root cause:Trying to log without a running server causes errors or lost data because MLflow has nowhere to store logs.
#2Using local file paths for artifact storage in a multi-user environment.
Wrong approach:mlflow server --backend-store-uri sqlite:///mlflow.db --default-artifact-root ./mlruns
Correct approach:Use shared cloud storage: mlflow server --backend-store-uri postgresql://user:pass@host/db --default-artifact-root s3://mybucket/mlflow
Root cause:Local paths are not accessible to all users, causing missing artifacts and collaboration issues.
#3Not explicitly calling mlflow.start_run() before logging parameters and metrics.
Wrong approach:mlflow.log_param('batch_size', 32) mlflow.log_metric('loss', 0.2)
Correct approach:with mlflow.start_run(): mlflow.log_param('batch_size', 32) mlflow.log_metric('loss', 0.2)
Root cause:Logging outside a run context results in errors or logs not being saved properly.
Key Takeaways
MLflow organizes machine learning experiments by tracking parameters, metrics, and models in a central place.
You must explicitly log data in your code and run a tracking server to save experiment details.
The MLflow UI helps visualize and compare experiment runs, making analysis easier.
Model Registry manages model versions and deployment stages, improving production workflows.
Scaling MLflow for teams requires remote servers, databases, and cloud storage for reliability and collaboration.

Practice

(1/5)
1. What is the primary purpose of MLflow in machine learning projects?
easy
A. To deploy machine learning models to mobile devices
B. To write machine learning algorithms from scratch
C. To create datasets for training models
D. To track and organize machine learning experiments

Solution

  1. Step 1: Understand MLflow's role

    MLflow is designed to help manage and track experiments, not to build models or datasets.
  2. Step 2: Identify the correct purpose

    Tracking and organizing experiments is the core feature of MLflow.
  3. Final Answer:

    To track and organize machine learning experiments -> Option D
  4. Quick Check:

    MLflow = experiment tracking [OK]
Hint: Remember MLflow tracks experiments, not builds models [OK]
Common Mistakes:
  • Confusing MLflow with model building libraries
  • Thinking MLflow creates datasets
  • Assuming MLflow deploys models directly
2. Which command correctly installs MLflow using pip?
easy
A. pip install mlflow
B. pip get mlflow
C. install mlflow pip
D. pip mlflow install

Solution

  1. Step 1: Recall pip install syntax

    The correct syntax to install a package is 'pip install package_name'.
  2. Step 2: Match the command

    Only 'pip install mlflow' matches the correct syntax.
  3. Final Answer:

    pip install mlflow -> Option A
  4. Quick Check:

    pip install + package = correct [OK]
Hint: Use 'pip install' followed by package name [OK]
Common Mistakes:
  • Using incorrect order of words
  • Using 'pip get' instead of 'pip install'
  • Omitting 'install' keyword
3. What happens when you run the command mlflow ui in your terminal?
medium
A. It starts a web interface to view and compare ML experiments
B. It installs MLflow on your system
C. It runs your machine learning model training
D. It deletes all previous MLflow experiments

Solution

  1. Step 1: Understand the 'mlflow ui' command

    This command launches the MLflow tracking server's web interface.
  2. Step 2: Identify the effect

    The UI lets users view and compare experiments visually in a browser.
  3. Final Answer:

    It starts a web interface to view and compare ML experiments -> Option A
  4. Quick Check:

    mlflow ui = launch web UI [OK]
Hint: Think 'ui' means user interface for experiments [OK]
Common Mistakes:
  • Confusing UI launch with installation
  • Assuming it runs training automatically
  • Thinking it deletes experiments
4. You try to start MLflow UI with mlflow ui but get an error saying 'command not found'. What is the most likely cause?
medium
A. You need to run 'mlflow start' instead
B. Your Python version is too new for MLflow
C. MLflow is not installed or not in your system PATH
D. You must run the command inside a Jupyter notebook

Solution

  1. Step 1: Analyze the error message

    'command not found' means the system cannot locate the 'mlflow' command.
  2. Step 2: Identify common causes

    This usually happens if MLflow is not installed or its executable is not in the system PATH.
  3. Final Answer:

    MLflow is not installed or not in your system PATH -> Option C
  4. Quick Check:

    Command not found = missing install or PATH [OK]
Hint: Check if MLflow is installed and in PATH [OK]
Common Mistakes:
  • Trying wrong commands like 'mlflow start'
  • Blaming Python version without checking install
  • Assuming it must run inside Jupyter
5. You want to create a new MLflow experiment named 'MyExperiment' and log a parameter 'alpha' with value 0.5 in a Python script. Which code snippet correctly does this?
hard
A. import mlflow mlflow.create_experiment('MyExperiment') mlflow.log_param('alpha', 0.5)
B. import mlflow mlflow.set_experiment('MyExperiment') with mlflow.start_run(): mlflow.log_param('alpha', 0.5)
C. import mlflow mlflow.start_experiment('MyExperiment') mlflow.log_param('alpha', 0.5)
D. import mlflow mlflow.set_experiment('MyExperiment') mlflow.log_param('alpha', 0.5)

Solution

  1. Step 1: Set the experiment name

    Use mlflow.set_experiment('MyExperiment') to select or create the experiment.
  2. Step 2: Start a run and log parameters

    Use 'with mlflow.start_run():' block to start a run, then log parameters inside it.
  3. Step 3: Identify correct snippet

    import mlflow mlflow.set_experiment('MyExperiment') with mlflow.start_run(): mlflow.log_param('alpha', 0.5) correctly uses set_experiment, start_run context, and logs parameter.
  4. Final Answer:

    import mlflow mlflow.set_experiment('MyExperiment') with mlflow.start_run(): mlflow.log_param('alpha', 0.5) -> Option B
  5. Quick Check:

    Set experiment + start run + log param = correct [OK]
Hint: Always start a run before logging parameters [OK]
Common Mistakes:
  • Logging parameters outside a run
  • Using non-existent functions like create_experiment
  • Not using 'with' block for start_run