当前位置: 首页 > news >正文

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_signatureimport 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 hyperparametersmlflow.log_params(params)# Log the loss metricmlflow.log_metric("accuracy", accuracy)# Infer the model signaturesignature = infer_signature(X_train, lr.predict(X_train))# Log the model, which inherits the parameters and metricmodel_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 formlflow.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_namesresult = pd.DataFrame(X_test, columns=iris_feature_names)
result["actual_class"] = y_test
result["predicted_class"] = predictionsresult[: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

 

http://www.zskr.cn/news/77443.html

相关文章:

  • 2025年高光谱视觉设备企业TOP5推荐:彩鸿芯宇产品性价比
  • 2025年值得合作的 GEO 服务商推荐:独家测评深度指南
  • 2025年宝宝红屁屁选什么纸尿裤排行榜:准爸妈必看的权威选购指南
  • 2025年专业 GEO 服务商排名:独家TOP10深度揭秘
  • 保姆级教程:将N8N升级为“全模态”神器,看图、听音样样行!
  • 2025 企业知识库部署新选解析:Deepseek 知识库部署服务商 + BI 私有化部署方案商,AI 知识库部署厂商技术实践指南
  • 安卓松绑未验证应用安装:这波操作到底香不香?
  • 2025工业安全锁具TOP5权威推荐:工业安全锁具来样定制实
  • 肝病医院哪家好?按诊疗需求精准匹配9所权威机构
  • 2025年大型 GEO 公司有哪些推荐?:十大官方榜单解析
  • 2025年GEO 公司怎么收费合理?:深度报告与攻略揭秘
  • 2025年汽车停车棚工厂权威推荐榜单:自行车停车棚‌/充电桩车棚‌/运动场看台膜结构‌‌源头工厂精选
  • 不用死磕语法!MATLAB R2024b 下载安装教程 实时纠错 + 一行批量处理,新手也能快速上手
  • 2025年度中国工业安全锁具制造商排行榜:不错的工业安全锁具
  • 完整教程:Ken的Java学习之路——Java中关于面向对象
  • js 立即执行函数表达式(Immediately Invoked Function Expression,简称 IIFE):(function () {})();
  • IDEA的AI帮助写代码得插件
  • 2025年五大锤式破碎机品牌排行榜,新性能测评与安全保养指南
  • 2025年本地 GEO 公司哪家靠谱?:专业测评精选指南
  • 2025年全国知名 GEO 公司有哪些?:权威报告与攻略发布
  • 函数式接口
  • 北京翻译服务公司选型决策:2025年12月北京优质翻译服务公司综合实力解析
  • 2025上海商业场所保洁优质品牌推荐指南
  • 2025年最新快充/重卡/商用/电动车/新能源充电桩厂家 TOP5 推荐:技术落地 + 全域服务的实力之选
  • 某中心ICASSP 2022五十余篇论文技术概览
  • jenkins 通过ssh配置 将java应用程序部署到远程服务器
  • selenium常用界面交互操作
  • 大屏适缩放
  • 2025年最新数字人平台权威排行榜:全方位深度测评与精选推荐
  • 苏州永创智能科技详解“CMTI测试电源”共模瞬态抗扰度测试方案及标准 - FORCREAT