A run identifier uniquely identifies one machine-learning experiment or training execution. MLflow associates it with parameters, metrics, models, datasets, and other artifacts, allowing a run to be inspected or retrieved later. section 4.1.2 section 9.3.4
Use the identifier to connect operational parts of the ML workflow:
- Separate exploratory runs from model-building runs by assigning an explicit run name or retaining MLflow's generated identifier. section 4.1.2
- Attach datasets to the exact training execution by incorporating the run ID into stored dataset paths. section 4.1.2
- Load a logged model for validation or inference with a URI such as runs:/<run_id>/model. section 9.3.4
- Continue logging evaluation metrics against the same run when training and validation are separate workflows. section 9.3.4
Training and validating one tracked run
Start a run, capture run.info.run_id, store training and test datasets under paths containing that identifier, then pass it to validation so MLflow loads the matching model and records evaluation metrics. section 4.1.2 section 9.3.4
Listing 4.5 Training a model with MLflow logging
BUCKET_NAME = "mlflow-datasets"
with mlflow.start_run() as run:
results = pd.DataFrame(
index=[
'Roc Auc Score test',
'Accuracy score train',
'Accuracy Score test',
'time to fit'
]
)
tree = DecisionTreeClassifier()
run_id = run.info.run_id
feature_df_path = f"income-classifier-datasets/feature_df-{run_id}.csv"
save_df_to_minio(feature_df, BUCKET_NAME, feature_df_path)
train_df = pd.concat([X_train, pd.Series(y_train.ravel())], axis=1)
train_df_path = f"income-classifier-datasets/train-{run_id}.csv"
save_df_to_minio(
train_df,
BUCKET_NAME,
train_df_path
) #1
test_df = pd.concat([X_test, pd.Series(y_test.ravel())], axis=1)
test_df_path = f"income-classifier-datasets/test-{run_id}.csv"
save_df_to_minio(test_df, BUCKET_NAME, test_df_path)
training_dataset = mlflow.data.from_pandas( #2
train_df, source=f"{BUCKET_NAME}/{train_df_path}"
)
test_dataset = mlflow.data.from_pandas(
test_df, source=f"{BUCKET_NAME}/{test_df_path}"
)
feature_dataset = mlflow.data.from_pandas(
feature_df, source=f"{BUCKET_NAME}/{feature_df_path}"
)
mlflow.log_input(
training_dataset,
context="training"
) #3
mlflow.log_input(test_dataset, context="testing")
mlflow.log_input(feature_dataset, context="reference")
tree.fit(X_train, y_train.ravel()) #4
roc_auc_score_train = roc_auc_score( #5
y_train == 1, tree.predict_proba(X_train)[:, 1]
)
roc_auc_score_test = roc_auc_score(
y_test == 1, tree.predict_proba(X_test)[:, 1]
)
training_accuracy = tree.score(X_train, y_train)
test_accuracy = tree.score(X_test, y_test)
mlflow.log_metric(
"roc_auc_score_train",
roc_auc_score_train
) #6
print(f"Roc Auc Score train: {roc_auc_score_train}\n")
mlflow.log_metric("roc_auc_score_test", roc_auc_score_test)
print(f"Roc Auc Score test: {roc_auc_score_test}\n")
mlflow.log_metric("training_accuracy", training_accuracy)
print(f"Accuracy train: {training_accuracy}")
mlflow.log_metric("test_accuracy", test_accuracy)
print(f"Accuracy test: {test_accuracy}")
mlflow.sklearn.log_model(tree, "income-classifier") #7
mlflow.log_params(tree.get_params()) #8
Shows how a training run exposes its identifier and uses it to create traceable dataset paths. section 4.1.2
