0
0
MLOpsdevops~5 mins

Logging artifacts and models in MLOps - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you train machine learning models, you want to save the results and files so you can use them later or share them. Logging artifacts and models means saving these files in a safe place automatically during training.
When you want to save your trained model to use it later for predictions.
When you want to keep track of files like plots or data samples created during training.
When you want to compare different model versions by saving each one separately.
When you want to share your model and related files with your team easily.
When you want to keep a history of your experiments and their outputs.
Commands
This command installs MLflow, a tool that helps you log models and artifacts easily.
Terminal
pip install mlflow
Expected OutputExpected
Collecting mlflow Downloading mlflow-2.7.0-py3-none-any.whl (18.7 MB) Installing collected packages: mlflow Successfully installed mlflow-2.7.0
This runs a Python script that trains a model and logs the model and an artifact file using MLflow.
Terminal
python log_model.py
Expected OutputExpected
2024/06/01 12:00:00 INFO mlflow.tracking.fluent: Experiment with name 'Default' does not exist. Creating a new experiment. 2024/06/01 12:00:01 INFO mlflow.tracking.fluent: Logging model and artifact Model and artifact logged successfully
Key Concept

If you remember nothing else from this pattern, remember: logging saves your model and files automatically so you never lose your work.

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
import joblib

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

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

# Save a sample artifact
joblib.dump(model, 'model.joblib')

# Start MLflow run
with mlflow.start_run():
    # Log model
    mlflow.sklearn.log_model(model, 'random_forest_model')
    # Log artifact file
    mlflow.log_artifact('model.joblib')

print('Model and artifact logged successfully')
OutputSuccess
Common Mistakes
Not calling mlflow.start_run() before logging artifacts or models
MLflow needs a run context to save logs; without it, logging commands fail silently or cause errors.
Always wrap your logging code inside mlflow.start_run() as a context manager.
Logging model files without specifying the correct path
If the path is wrong, MLflow cannot find the files to save, so nothing gets logged.
Make sure the file paths you pass to mlflow.log_artifact() or mlflow.sklearn.log_model() are correct and files exist.
Summary
Install MLflow to enable logging of models and artifacts.
Use mlflow.start_run() to create a logging session.
Log your trained model and any files you want to keep using MLflow functions.
This process helps you save and track your machine learning work automatically.