部署MySQL
-
docker pull mysql:latest
-
docker run
-p 3306:3306
–name ml-mysql
-v ~/mysql/data:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=669988
-d mysql -
在mysql里创建一个叫mlflow的库
create database if not exists `mlflow`;
部署Minio
-
docker pull minio
-
docker run -d -p 9000:9000 -p 9090:9090
–name minio
-e “MINIO_ACCESS_KEY=minio”
-e “MINIO_SECRET_KEY=minio123”
-v /opt/minio/data:/data
-v /opt/minio/config:/root/.minio
minio/minio server /data
–console-address “:9000” --address “:9090”
// the port 9000 represents minio console port
// the port 9090 represents minio api port -
在minio的web ui上创建一个叫mlflow的bucket
-
网上教程一般都有这步,但我从来没成功过
export AWS_ACCESS_KEY_ID=minio
export AWS_SECRET_ACCESS_KEY=minio123
export MLFLOW_S3_ENDPOINT_URL=http://localhost:9000
装python环境
- 创建一个新环境
conda create -n mltest python==3.8
- pip install mlflow mysqlclient/pymysql boto3
启动MLflow
- mlflow server
–backend-store-uri mysql+pymysql://root:669988@localhost/mlflow
–host 0.0.0.0 -p 5002
–default-artifact-root s3://mlflow
MLflow访问Minio
- 把这些加在代码里
os.environ[‘MLFLOW_S3_ENDPOINT_URL’] = ‘http://888.88.88.888:9090/’
os.environ[‘AWS_ACCESS_KEY_ID’] = ‘minio’
os.environ[‘AWS_SECRET_ACCESS_KEY’] = ‘minio123’
mlflow使用
import boto3
import mlflow
import mlflow.sklearn
#设置远程路径
mlflow.set_tracking_uri("http://888.88.88.888:5002")
mlflow.set_experiment("my-experiment")
with mlflow.start_run():
# 记录模型param
mlflow.log_param("alpha", alpha)
mlflow.log_params(dict) # 多个param传个dict进去
# 记录模型metric
mlflow.log_metric("r2", r2)
mlflow.log_metrics(dict) # metrics同理
# 记录artifacts
mlflow.sklearn.log_model(lr, "model")
# Log the sklearn model and register as version 1
mlflow.sklearn.log_model(
sk_model=lr,
artifact_path="sklearn-model",
registered_model_name="sk-learn-random-forest-reg-model",
)
mlflow.end_run()