MLflow (机器学习/深度学习 mlops平台 kubeflow)

MLflow 是一个功能强大的ML生命周期管理平台,主要用于 ML/DL/LLM 实验管理、模型跟踪、模型部署等。

# 1.1 创建虚拟环境
conda create -n mlflow-env python=3.10
conda activate mlflow-env

# 1.2 Install mlflow
pip install mlflow

# 2.1 Run a local Tracking Server
mlflow server --host 127.0.0.1 --port 8080 # 默认5000

# 3. 构建demo训练
mkdir /your/path/mlflow-demo
cd /your/path/mlflow-demo

# 3.1 Train a model and prepare metadata for logging
vi train.py

========================================================
# train.py
import mlflow
from mlflow.models import infer_signature

import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score


# Load the Iris dataset
X, y = datasets.load_iris(return_X_y=True)

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Define the model hyperparameters
params = {
    "solver": "lbfgs",
    "max_iter": 1000,
    "multi_class": "auto",
    "random_state": 8888,
}

# Train the model
lr = LogisticRegression(**params)
lr.fit(X_train, y_train)

# Predict on the test set
y_pred = lr.predict(X_test)

# Calculate metrics
accuracy = accuracy_score(y_test, y_pred)
========================================================

# 3.2 Log the model and its metadata to MLflow
========================================================
# Set our tracking server uri for logging
mlflow.**set_tracking_uri**(uri="http://127.0.0.1:8080")

# Create a new MLflow Experiment
mlflow.set_experiment("MLflow Quickstart")

# Start an MLflow run
with mlflow.start_run():
    # Log the hyperparameters
    mlflow.log_params(params)

    # Log the loss metric
    mlflow.log_metric("accuracy", accuracy)

    # Infer the model signature
    signature = infer_signature(X_train, lr.predict(X_train))

    # Log the model, which inherits the parameters and metric
    model_info = mlflow.sklearn.log_model(
        sk_model=lr,
        name="iris_model",
        signature=signature,
        input_example=X_train,
        registered_model_name="tracking-quickstart",
    )

    # Set a tag that we can use to remind ourselves what this model was for
    mlflow.set_logged_model_tags(
        model_info.model_id, {"Training Info": "Basic LR model for iris data"}
    )
========================================================

# 4. Load the model as a Python Function (pyfunc) and use it for inference
========================================================
# Load the model back for predictions as a generic Python Function model
loaded_model = mlflow.pyfunc.**load_model**(model_info.model_uri)

predictions = loaded_model.predict(X_test)

iris_feature_names = datasets.load_iris().feature_names

result = pd.DataFrame(X_test, columns=iris_feature_names)
result["actual_class"] = y_test
result["predicted_class"] = predictions

result[:4]
========================================================

# 注意在启动训练前,需要保证mlflow server已经启动
# 5.1 View the Run and Model in the MLflow UI
python train.py

# 5.1 启动ui,然后打开浏览器访问:http://127.0.0.1:8080 查看运行结果。
mlflow ui

 

posted @ 2025-12-08 15:45  wangssd  阅读(0)  评论(0)    收藏  举报