My Note on MLflow

ifeelfree
1 min readMay 4, 2021

Part 1: Concepts

  • Experiment
import mlflow

# Create an experiment name, which must be unique and case sensitive
experiment_id = mlflow.create_experiment("Social NLP Experiments")
experiment = mlflow.get_experiment(experiment_id)
print("Name: {}".format(experiment.name))
print("Experiment_id: {}".format(experiment.experiment_id))
print("Artifact Location: {}".format(experiment.artifact_location))
print("Tags: {}".format(experiment.tags))
print("Lifecycle_stage: {}".format(experiment.lifecycle_stage))
  • Runs
def print_auto_logged_info(r):
tags = {k: v for k, v in r.data.tags.items() if not k.startswith("mlflow.")}
artifacts = [f.path for f in MlflowClient().list_artifacts(r.info.run_id, "model")]
print("run_id: {}".format(r.info.run_id))
print("artifacts: {}".format(artifacts))
print("params: {}".format(r.data.params))
print("metrics: {}".format(r.data.metrics))
print("tags: {}".format(tags))
mlflow.autolog()
with mlflow.start_run() as run:
neigh = KNeighborsClassifier(n_neighbors=5)
neigh.fit(X,y)

print('active run_id: {}'.format(run.info.run_id))

print_auto_logged_info(mlflow.get_run(run_id=run.info.run_id))

Part 2: Experience

  1. When mlflow.autolog() is enabled, parameters/metrics will be kept automatically, and there is no need to use mlflow.log_param to keep the parameters unless the parameters are not within the model defined in sklearn
  2. when use mlflow.set_tracking_uri('file:/home/usr_name...') , you need to set the experiment id in mlflow_star_run
exp_id = mlflow.create_experiment("my_iris_classification_model")
with mlflow.start_run(experiment_id=exp_id) as run:
mlflow.pytorch.log_model(scripted_model, "model")

As a result, when you invoke mlflow ui, please indicate the uri location

mlflow ui --backend-store-uri file:/home/petra/mlrun_store

It is also possible to denote other information

--default-artifact-root file:/home/petra/mlrun_store \
--host 0.0.0.0 \
--port 5000

Part 3: Pattern

Reference

--

--