0% found this document useful (0 votes)
13 views2 pages

MLflow Python API Reference Guide

This document is a cheat sheet for the MLflow Python API, covering key functionalities such as model registration, logging parameters and metrics, running projects, and deploying models. It includes code snippets for various tasks like transitioning model versions, logging datasets, and enabling autologging. Additionally, it provides guidance on using MLflow with Databricks and managing experiments and deployments.

Uploaded by

Sergio Damian
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

MLflow Python API Reference Guide

This document is a cheat sheet for the MLflow Python API, covering key functionalities such as model registration, logging parameters and metrics, running projects, and deploying models. It includes code snippets for various tasks like transitioning model versions, logging datasets, and enabling autologging. Additionally, it provides guidance on using MLflow with Databricks and managing experiments and deployments.

Uploaded by

Sergio Damian
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

from mlflow.

tracking import MlflowClient client = MlflowClient()


MLflow Python API Cheat Sheet ■ client.update_registered_model(name="MyModel", description="Production ready")

Transition Model Version


The complete MLflow Python API reference for tracking, modeling, deployment, and more. client.transition_model_version_stage("MyModel", 1, stage="Production")

Search & Delete


1. MLflow Tracking client.search_registered_models() client.delete_registered_model("OldModel")

Start & End a Run


5. Logging Datasets ([Link])
import mlflow with mlflow.start_run(): mlflow.log_param("lr", 0.01) mlflow.log_metric("acc",
0.92) mlflow.end_run() Pandas
Log Parameters & Metrics from [Link].pandas_dataset import from_pandas ds = from_pandas(df, name="my_dataset")
mlflow.log_input(ds)
mlflow.log_param("batch_size", 32) mlflow.log_metric("loss", 0.25)

Log Artifacts NumPy


from [Link].numpy_dataset import from_numpy ds = from_numpy(features=X_np,
mlflow.log_artifact("[Link]") mlflow.log_artifact("[Link]", artifact_path="charts")
name="np_dataset") mlflow.log_input(ds)
Access Active Run
Delta
run = mlflow.active_run() print([Link].run_id)
from [Link].delta_dataset import load_delta ds = load_delta("[Link]",
version=1) mlflow.log_input(ds)
2. MLflow Projects
Run MLflow Project 6. Model Evaluation ([Link])
[Link](uri=".", parameters={"alpha": 0.01}) Evaluate Model
MLproject File Example [Link]( model="runs://model", data=test_df, targets="label", model_type="classifier" )
name: MyProject entry_points: main: parameters: alpha: {type: float, default: 0.5} command:
"python [Link] --alpha {alpha}" 7. MLflow Client ([Link])
3. MLflow Models Client Initialization
from [Link] import MlflowClient client = MlflowClient()
Save & Log Model
Create Experiment & Run
[Link].save_model(model, path="model_dir") [Link].log_model(python_model=model,
experiment_id = client.create_experiment("MyExp") run = client.create_run(experiment_id)
artifact_path="model")

PyFunc Model Registry Actions


client.create_registered_model("MyModel") client.create_model_version("MyModel",
import [Link] model = [Link].load_model("runs://model") [Link](input_data)
"runs:/id/model", run_id="id")
Scikit-learn
import [Link] [Link].log_model(sk_model, artifact_path="sk_model") 8. MLflow Deployments
PyTorch
Create & Predict
import [Link] [Link].log_model(pytorch_model, artifact_path="torch_model")
import [Link] client = [Link].get_deploy_client("databricks")
client.create_deployment("my_ep", "models:/Model/1", config={}) [Link]("my_ep", df)
4. MLflow Model Registry
List & Delete
Register Model client.list_deployments() client.delete_deployment("my_ep")
mlflow.register_model("runs://model", "MyModel")

Load Registered Model


9. MLflow Recipes
from [Link] import load_model model = load_model("models:/MyModel/Production") Initialize and Run
Update Model Description
from [Link] import Recipe recipe = Recipe(profile="local") [Link]() [Link]() [Link]() [Link]()

Create via CLI


mlflow recipes init my_recipe -t regression

10. Autologging
Enable Autologging
[Link]() # Automatically logs supported libraries like sklearn, keras, xgboost

Disable Autologging
[Link](disable=True)

11. MLflow on Databricks


Setup Databricks MLflow Tracking
mlflow.set_tracking_uri("databricks") mlflow.set_experiment("/Shared/my_experiment")

Log Model with Unity Catalog


mlflow.log_model( model, artifact_path="model", registered_model_name="[Link].model_name" )

Common questions

Powered by AI

MLflow offers integrated dataset management and logging strategies by providing specific functions for popular data formats, facilitating their tracking alongside model experiments. For pandas dataframes, datasets can be imported with 'from mlflow.data.pandas_dataset import from_pandas', then logged using 'mlflow.log_input(ds)' . Similar functionalities exist for numpy arrays and Delta tables, via their respective dataset import and logging functions . These strategies ensure datasets are tightly coupled with corresponding experiments, promoting data traceability and reproducibility across multiple runs and aiding in contextual data analysis.

Transitioning a model version's stage in the MLflow Model Registry involves changing its developmental stage, such as from 'Staging' to 'Production', using the 'client.transition_model_version_stage()' method . This process formalizes the model's lifecycle, ensuring it meets specific testing and quality criteria before deployment. Transitioning stages has critical implications for model governance and compliance, as it increases transparency and auditability of model approvals and usage . It also allows teams to systematically promote models through lifecycle stages, aligning with organizational standards for safety and performance.

MLflow Recipes enhances machine learning pipeline development by providing a structured approach to defining and executing complete ML workflows. It supports initializing, cleaning, running, inspecting, and testing machine learning tasks using a standardized interface, as seen in 'recipe.run()' and 'recipe.test()' . This form of execution ensures that each step of the ML pipeline is handled methodically, reducing errors and enhancing reproducibility. MLflow Recipes can be initialized via the CLI using 'mlflow recipes init my_recipe -t regression', enabling developers to quickly set up new projects with predefined templates that aid in rapid deployment and scalable development .

MLflow's model evaluation feature operates by assessing the performance of a machine learning model against a test dataset using the 'mlflow.evaluate()' function, which accepts parameters such as the model path, data, targets, and model type . It plays a critical role in the machine learning lifecycle by providing objective performance metrics crucial for comparing different models, identifying potential improvements, and making informed decisions for model deployment . Evaluating models systematically ensures consistent performance standards and contributes to the continuous improvement of ML models.

MLflow aids in organizing and managing machine learning experiments through its tracking and model registry features by providing a systematic way to log parameters, metrics, and artifacts associated with runs using the 'mlflow.start_run()', 'mlflow.log_param()', and 'mlflow.log_metric()' methods . The model registry feature allows models to be registered with the command 'mlflow.register_model()' and enables tracking of different versions, transitioning models between stages, and updating model descriptions . This structured approach ensures reproducibility, traceability, and easy retrieval of all experiment data and models.

MLflow's PyFunc offers a uniform interface for deploying machine learning models regardless of the underlying framework. It facilitates model deployment by loading models through 'mlflow.pyfunc.load_model()', allowing them to be served via an API endpoint or integrated into native applications . PyFunc bridges models from different frameworks such as scikit-learn, PyTorch, and others by saving and logging them using their respective MLflow libraries like 'mlflow.sklearn.log_model()' and 'mlflow.pytorch.log_model()' . This integration flexibility streamlines the deployment process across diverse development ecosystems.

The 'mlflow.projects.run()' function and the 'MLproject' file play crucial roles in experiment reproducibility in MLflow by encapsulating machine learning projects into a consistent and portable format. The 'mlflow.projects.run()' function executes MLflow projects, ensuring that specific parameters and configurations, such as 'alpha' in a training job, are consistently applied by defining them in the 'MLproject' file . The 'MLproject' file specifies project dependencies, entry points, and default parameters, which ensures that experiments can be easily reproduced on different environments with minimal setup variability .

MLflow's autologging feature simplifies the experiment logging process by automatically capturing relevant data such as parameters, metrics, and artifacts without explicit calls to logging functions . It supports multiple popular ML libraries including scikit-learn, Keras, and XGBoost, and can be enabled with 'mlflow.autolog()' or disabled using 'mlflow.autolog(disable=True)' . This automation reduces the amount of boilerplate code required and ensures consistent logging practices across various machine learning frameworks, improving productivity and experiment tracking consistency.

MLflow deployments can be managed and streamlined through its API via the 'mlflow.deployments' module, enabling creation, prediction, and deletion operations. Deployment is initiated with 'client.create_deployment()', where models are staged for serving with configurations . Prediction can be performed on deployed models using 'client.predict()', providing real-time inference capabilities on input data . The workflow is streamlined by listing active deployments with 'client.list_deployments()' and removing unused deployments using 'client.delete_deployment()', thereby maintaining an efficient environment for model serving .

Databricks integration significantly enhances MLflow's capabilities by leveraging its cloud-native features to improve model tracking and deployment. Setting the tracking URI with 'mlflow.set_tracking_uri("databricks")' ties MLflow's capabilities to Databricks' scalable infrastructure, allowing seamless experimentation and tracking within a collaborative environment . Models can be logged and managed directly with Unity Catalog, enhancing data governance and security. This integration brings a sophisticated layer of enterprise-level management to MLflow workflows, facilitating efficient sharing, tracking, and deployment of models in a team-oriented setting .

You might also like