Log Artifacts¶
Artifacts in MLflow are files or data that are important for your machine learning projects. They can be things like trained models, datasets, images, configuration files, or any other files that are relevant to your project.
Artifacts are used in MLflow to track and manage the different versions of these files throughout the machine learning lifecycle. MLflow provides a centralized repository called the artifact store where you can store and retrieve these artifacts. By storing artifacts in MLflow, you can keep track of the exact version of the files used during each step of your machine learning workflow.
✨ Create a simple artifact¶
An image (png) of a confussion Matrix.
In [1]:
Copied!
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# generate a random confusion matrix
confusion_matrix = np.array([
[ 9, 1, 0],
[ 1, 9, 0],
[ 0, 0, 10]
])
# plot the confusion matrix
ax = sns.heatmap(confusion_matrix, cmap="Blues", annot=True)
ax.set_xlabel("Predicted labels")
ax.set_ylabel("True labels")
ax.set_title("Confusion Matrix")
# save the confusion matrix to a file
plt.savefig("confusion_matrix.png") # 👈 saving your artifact (remember artifacts are files)
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# generate a random confusion matrix
confusion_matrix = np.array([
[ 9, 1, 0],
[ 1, 9, 0],
[ 0, 0, 10]
])
# plot the confusion matrix
ax = sns.heatmap(confusion_matrix, cmap="Blues", annot=True)
ax.set_xlabel("Predicted labels")
ax.set_ylabel("True labels")
ax.set_title("Confusion Matrix")
# save the confusion matrix to a file
plt.savefig("confusion_matrix.png") # 👈 saving your artifact (remember artifacts are files)
💾 Log the artifact¶
In [2]:
Copied!
import mlflow
EXPERIMENT_NAME = "mlflow-demo" # ❗ make sure this experiment exists
RUN_NAME = "run-with-model"
experiment_id = mlflow.get_experiment_by_name(EXPERIMENT_NAME).experiment_id
with mlflow.start_run(
experiment_id=experiment_id,
run_name=RUN_NAME,
) as run:
# log the confusion matrix as an artifact
mlflow.log_artifact("confusion_matrix.png") # 👈 logging your artifact
# Print the run ID
print(f"Run ID: {run.info.run_id}")
import mlflow
EXPERIMENT_NAME = "mlflow-demo" # ❗ make sure this experiment exists
RUN_NAME = "run-with-model"
experiment_id = mlflow.get_experiment_by_name(EXPERIMENT_NAME).experiment_id
with mlflow.start_run(
experiment_id=experiment_id,
run_name=RUN_NAME,
) as run:
# log the confusion matrix as an artifact
mlflow.log_artifact("confusion_matrix.png") # 👈 logging your artifact
# Print the run ID
print(f"Run ID: {run.info.run_id}")
Run ID: 5bce4f5add21457b80655b3b77482b45