• Classes
  • 13 - Tracking
  • Part 2

Centralization

In the last class we saw how to use MLflow to track experiments.

Question 1

Where was the information about the runs (metrics, parameters, artifacts) stored?

Answer!

In a mlruns folder created by MLflow.

Question 2

How will local storage of experiments impact a project developed by multiple ML scientists and engineers?

Answer!

We may have a problem, as one will not have vision of what was attempted by the other.

It would be appropriate to keep information about experiments centrally. Thus, whenever an experiment is carried out by a data scientist, the result will be available for analysis by others.

Question 3

Can you think of any solution to store metrics and parameter data centrally?

Answer!

In a relational database!

Question 4

Can you think of any solution to store artifacts, such as images and files, centrally?

Answer!

S3 bucket!

New scenario!

Let's propose a reconfiguration of the scenario from last class. In it, all structured information about the experiments will be stored in a PostgreSQL database, while the artifacts will be stored in an S3 bucket.

Important!

Now the results of the experiments will be available to everyone on the team!

The MLFlow server could also run centrally (like on an EC2 instance). However, we will keep it running locally, but storing data centrally.

Create Database

Question 5

Using the database credentials provided by the professor, make a connection to the database using DBeaver and create a database with the name following the pattern mlflow_INSPER_USERNAME.

Important

Replace INSPER_USERNAME with your Insper username!

When creating the database, use the tablespace Default instead of pg_default.

Tip! 1

When creating the connection in DBeaver, in the second tab "PostgreSQL", check the "Show all databases" option.

Create Bucket

Question 6

Create a bucket in AWS S3 to store your experiment artifacts.

As we already have the AWS CLI configured, we will use it for this task. If you want, you can also use Python codes for this task!

Important

Replace INSPER-USERNAME with your Insper username to configure S3 Bucket name properly.

$ aws s3api create-bucket --bucket mlflow-exp-tracking-INSPER-USERNAME --region us-east-2 --create-bucket-configuration LocationConstraint=us-east-2

Configure MLflow Server

Now let's start a local MLflow server that will connect to the database and the S3 bucket.

Question 7

Start the server with:

Important

Replace INSPER-USERNAME with your Insper username to configure S3 Bucket name properly.

Important

Fill in the database credentials provided by the professor: USERNAME, PASSWORD, HOST, PORT.

For the DATABASE, use the one created by you, following the pattern mlflow_INSPER_USERNAME.

$ mlflow server --backend-store-uri postgresql://USERNAME:PASSWORD@HOST:PORT/DATABASE --default-artifact-root s3://mlflow-exp-tracking-INSPER-USERNAME

Use MLflow Server

Question 8

We will continue working on the same project as last class.

Make a copy of the past class folder.

Let's configure the copy of the previous class project to connect to the server using the URL. Thus, MLflow will make requests to the REST API of the MLflow server that is running locally and, in turn, the server will store the experiment logs in PostgreSQL and AWS S3.

Question 9

Change the code in the train.py file, in the main function, so that it uses the MLFlow server URL:

Attention!

Add the line with mlflow.set_tracking_uri and keep the others!

def main():
    mlflow.set_tracking_uri("http://localhost:5000")
    mlflow.set_experiment("churn-exp")

Tip! 2

Check if your server was actually started on port 5000.

Tip! 3

Instead of leaving the URL hardcoded, try setting an environment variable.

Question 10

From the src directory, create a .env file with AWS credentials:

AWS_ACCESS_KEY_ID="*******"
AWS_SECRET_ACCESS_KEY="*******"
AWS_REGION="*******"
AWS_ACCOUNT_ID="*******"

Then, update the src/train.py to call load_dotenv().

Question 11

From the root directory, test the code with:

$ python src/train.py

Question 12

Go to http://localhost:5000 in your browser and check if the experiment results are available.

Make sure your artifact URLs point to S3.

Question 13

In DBeaver, explore the created tables and their contents.

Question 14

List the contents of the bucket and check the created objects.

Important

Replace INSPER-USERNAME with your Insper username to configure S3 Bucket name properly.

$ aws s3api list-objects-v2 --bucket  mlflow-exp-tracking-INSPER-USERNAME

Interact with friends!

Question 15

Talk to your colleagues to use the same database and bucket.

This way you will be able to see in practice that you can integrate the experiments and visualize each other's results.

Tip! 4

You can agree to run experiments with different names if you don't want to work on the same one!