Bird
Raised Fist0
MLOpsdevops~20 mins

Logging artifacts and models in MLOps - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
MLflow Artifact & Model Logging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this MLflow artifact logging command?
Consider the following Python code snippet using MLflow to log an artifact file. What will be the output printed after running this code?
MLOps
import mlflow
with open('model_info.txt', 'w') as f:
    f.write('Model version 1.0')
with mlflow.start_run() as run:
    mlflow.log_artifact('model_info.txt')
    print(run.info.artifact_uri)
Afile:///path/to/mlruns/0/<run_id>/artifacts
Bs3://mlflow-bucket/artifacts/<run_id>
Chttp://localhost:5000/api/2.0/mlflow/artifacts/<run_id>
DError: artifact_uri attribute not found
Attempts:
2 left
💡 Hint
The artifact_uri shows the local path where artifacts are stored during the run.
🧠 Conceptual
intermediate
1:30remaining
Which MLflow command logs a model for later deployment?
You want to save a trained scikit-learn model in MLflow so it can be deployed later. Which command correctly logs the model?
Amlflow.log_metric('model', model)
Bmlflow.sklearn.log_model(model, 'model')
Cmlflow.log_param('model', model)
Dmlflow.log_artifact(model, 'model')
Attempts:
2 left
💡 Hint
Logging a model uses a specific MLflow flavor function.
Troubleshoot
advanced
2:30remaining
Why does MLflow fail to log artifacts in this code?
You run this code but no artifacts appear in the MLflow UI. What is the most likely cause?
MLOps
import mlflow
with mlflow.start_run():
    mlflow.log_artifact('output.txt')
# output.txt is created after the run block
AThe artifact file does not exist when log_artifact is called
Boutput.txt must be a directory, not a file
Clog_artifact only works outside the run context
DMLflow requires explicit run ID to log artifacts
Attempts:
2 left
💡 Hint
Check when the file is created relative to when it is logged.
🔀 Workflow
advanced
3:00remaining
Order the steps to log a model and its metrics in MLflow
Arrange these steps in the correct order to log a model and its evaluation metrics using MLflow.
A1,3,2,4,5
B1,2,3,4,5
C2,1,4,3,5
D2,1,3,4,5
Attempts:
2 left
💡 Hint
You must start the run before logging anything.
Best Practice
expert
2:00remaining
What is the best practice for versioning models in MLflow?
You want to keep track of multiple versions of your models in MLflow for easy rollback and comparison. Which approach is best?
AStore models in separate folders outside MLflow
BManually rename model files with version numbers before logging
CUse MLflow Model Registry to register and version models
DLog models without versioning and rely on timestamps
Attempts:
2 left
💡 Hint
MLflow provides a built-in system for model versioning.

Practice

(1/5)
1. What is the main purpose of logging artifacts and models in MLOps?
easy
A. To speed up model training
B. To delete old models automatically
C. To create new datasets from artifacts
D. To save files and models for tracking and reuse

Solution

  1. Step 1: Understand the role of logging in MLOps

    Logging artifacts and models helps keep track of work and reuse it later.
  2. Step 2: Identify the correct purpose

    Saving files and models for tracking and reuse matches the main goal of logging.
  3. Final Answer:

    To save files and models for tracking and reuse -> Option D
  4. Quick Check:

    Logging = Save and track work [OK]
Hint: Logging means saving work for later use [OK]
Common Mistakes:
  • Thinking logging deletes models
  • Confusing logging with speeding training
  • Assuming logging creates new data
2. Which of the following is the correct syntax to log a file artifact using MLflow?
easy
A. mlflow.log_model('path/to/file.txt')
B. mlflow.log_artifact('path/to/file.txt')
C. mlflow.log_artifacts('path/to/file.txt')
D. mlflow.log('path/to/file.txt')

Solution

  1. Step 1: Recall MLflow function for logging files

    The correct function is mlflow.log_artifact() for single files.
  2. Step 2: Check syntax correctness

    mlflow.log_artifact('path/to/file.txt') matches the correct syntax.
  3. Final Answer:

    mlflow.log_artifact('path/to/file.txt') -> Option B
  4. Quick Check:

    Single file logging = log_artifact() [OK]
Hint: Use log_artifact() for single files [OK]
Common Mistakes:
  • Using log_model() for files
  • Using plural log_artifacts() incorrectly
  • Using generic log() function
3. What will be the output of this code snippet?
import mlflow
with mlflow.start_run():
    mlflow.log_artifact('data.csv')
    mlflow.log_model(model, 'model')
print('Run finished')
medium
A. Error because model is not defined
B. Run finished printed; artifacts and model logged in current run
C. No output; code hangs
D. Run finished printed; but nothing logged

Solution

  1. Step 1: Analyze the code snippet

    The code tries to log a file and a model inside a run.
  2. Step 2: Check for errors

    The variable 'model' is not defined, so mlflow.log_model(model, 'model') causes a NameError.
  3. Final Answer:

    Error because model is not defined -> Option A
  4. Quick Check:

    Undefined variable causes error [OK]
Hint: Check if variables are defined before logging [OK]
Common Mistakes:
  • Assuming code runs without defining model
  • Thinking print means success
  • Ignoring variable definitions
4. You run this code but no artifacts appear in MLflow UI:
mlflow.log_artifact('output.txt')

What is the most likely reason?
medium
A. log_artifact() only works inside a run
B. The file output.txt does not exist
C. No active MLflow run was started
D. MLflow server is down

Solution

  1. Step 1: Understand MLflow run context

    Logging artifacts requires an active run to group logs.
  2. Step 2: Identify missing run

    Without mlflow.start_run(), logs are not saved properly.
  3. Final Answer:

    No active MLflow run was started -> Option C
  4. Quick Check:

    Logging needs active run [OK]
Hint: Always start a run before logging [OK]
Common Mistakes:
  • Assuming logging works without a run
  • Ignoring file existence
  • Blaming server without checking run
5. You want to log multiple files and a trained model in one MLflow run. Which code snippet correctly does this?
hard
A. with mlflow.start_run(): mlflow.log_artifact('file1.txt') mlflow.log_artifact('file2.txt') mlflow.log_model(model, 'model')
B. mlflow.log_artifact(['file1.txt', 'file2.txt']) mlflow.log_model(model, 'model')
C. with mlflow.start_run(): mlflow.log_artifacts(['file1.txt', 'file2.txt']) mlflow.log_model(model, 'model')
D. mlflow.start_run() mlflow.log_artifact('file1.txt') mlflow.log_artifacts('file2.txt') mlflow.log_model(model, 'model') mlflow.end_run()

Solution

  1. Step 1: Identify correct way to log multiple files

    For multiple individual files, call mlflow.log_artifact() for each file.
  2. Step 2: Confirm run context and model logging

    Using with mlflow.start_run(): ensures proper context; logs each file and the model correctly.
  3. Final Answer:

    with mlflow.start_run(): mlflow.log_artifact('file1.txt') mlflow.log_artifact('file2.txt') mlflow.log_model(model, 'model') -> Option A
  4. Quick Check:

    Multiple files = log_artifact() for each inside run [OK]
Hint: Call log_artifact() for each file inside a run [OK]
Common Mistakes:
  • Passing list to log_artifacts()
  • Not using a run context
  • Using log_artifacts() on single file