{{announcement.body}}
{{announcement.title}}

Applying MLflow to an Existing Machine Learning Project

DZone 's Guide to

Applying MLflow to an Existing Machine Learning Project

See how to apply MLflow to an existing machine learning project.

· AI Zone ·
Free Resource

What Is MLflow?

MLflow is an open-source platform to manage the ML lifecycle, including experimentation, reproducibility, deployment, and a central model registry. MLflow currently offers four components:

  • MLflow Tracking
  • MLflow Projects
  • MLflow Models
  • MLflow Registry

In this example, we are going to use an existing Keras Tensorflow example and add MLflow support to it. 

Example: Collaborative Filtering for Movie Recommendations” with Keras and Tensorflow 2.0.0

The example we have selected is “Collaborative Filtering for Movie Recommendations” with Keras and Tensorflow 2.0.0. This example is created by Siddhartha Banerjee and the example Source URL is as below: https://keras.io/examples/structured_data/collaborative_filtering_movielens/

The GitHub source for the sample example is here: 

https://github.com/keras-team/keras-io/blob/master/examples/structured_data/collaborative_filtering_movielens.py

Creating Necessary Files for MLflow:

  • Download the source code on your local machine. Run the code locally to make sure it works and you understand it
Java
 




x


1
$ python collaborative_filtering_movielens.py


  • Create conda.yaml as below:
Java
 




xxxxxxxxxx
1
12


 
1
name: tf_keras_recom_example
2
channels:
3
— defaults
4
— anaconda
5
— conda-forge
6
dependencies:
7
— python=3.6
8
— numpy=1.14.2
9
— pandas
10
— pip:
11
— mlflow
12
— tensorflow==2.0.0


  • Create MLProject as below:
Java
 




xxxxxxxxxx
1


 
1
name: movielens-recommendations-keras-tf2
2
conda_env: conda.yaml
3
entry_points:
4
    main:
5
        command: “python collaborative_filtering_movielens.py”


Now you must have 3 files as below:

Editing the Code File for MLflow:

  • Add the following python modules
Java
 




xxxxxxxxxx
1


 
1
import mlflow.tensorflow
2
mlflow.tensorflow.autolog()


  • Comment matplotlib specific code in the project so plotting UI specific thread will not block the code execution.
Java
 




xxxxxxxxxx
1
10


 
1
# import matplotlib.pyplot as plt
2
....
3
....
4
# plt.plot(history.history[“loss”])
5
# plt.plot(history.history[“val_loss”])
6
# plt.title(“model loss”)
7
# plt.ylabel(“loss”)
8
# plt.xlabel(“epoch”)
9
# plt.legend([“train”, “test”], loc=”upper left”)
10
# plt.show()


That’s all. Now we will run the code in 2 separate terminal environments. 

Environment 1: Open the MLflow UI

Java
 




xxxxxxxxxx
1


 
1
$ mlflow ui


Note: Please open your Web Browser with URL http://localhost:5000 to see the MLflow UI. 

Environment 2: You can run the code with MLflow as below to start the project:

Java
 




xxxxxxxxxx
1


 
1
$ mlflow run .


Once code start running the MLflow UI will show the progress as below:

Source Code for This Project:

https://github.com/Avkash/300seconds/tree/master/tf2-keras-recommendations

Project Walkthrough & Tutorial Video @ YouTube

https://youtu.be/RFtorfGDtJM

That’s all. Please stay safe and stay healthy. 

Topics:
artificial intelligence, deep leaerning, keras, machine learning, ml development cycles, tensorflow 2.0, tutorial

Published at DZone with permission of Avkash Chauhan , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}