十月 10, 2024
·
机器学习
mlflow将模型数据保存至外部存储
安装:
设置环境变量:
AWS_ACCESS_KEY_ID=admin AWS_SECRET_ACCESS_KEY=admin MLFLOW_S3_ENDPOINT_URL=http://192.168.5.108:9000 MLFLOW_TRACKING_URI=http://192.168.5.108:15000/
例子:
import mlflow.sklearn from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression # 加载数据集 iris = load_iris() X, y = iris.data, iris.target # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 训练模型 lr = LogisticRegression(max_iter=1000) lr.fit(X_train, y_train) # 设置 MLflow 运行 with mlflow.start_run(): # 记录模型参数和性能指标(这里省略了具体指标的计算) mlflow.log_param("solver", lr.solver) mlflow.log_param("max_iter", lr.max_iter) # 保存模型到 MinIO mlflow.sklearn.log_model(sk_model=lr, artifact_path="iris_model", registered_model_name="iris-logistic-regression")
便可以在minio和mlflow里面看到对应的数据。
import mlflow.sklearn from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split iris = load_iris() X, y = iris.data, iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) loaded_model = mlflow.pyfunc.load_model("runs:/<run-id>/iris_model") predictions = loaded_model.predict(X_test) print(predictions)
在mlflow里面找到对应的run id,进行加载即可使用对应的模型。
扫码手机观看或分享: