Model training and Deployment on the 5Analytics AI Platform

Mohtadi Ben Fraj
5Analytics
Published in
3 min readJul 25, 2018

In this tutorial, we demonstrate how to use the 5Analytics platform to train and deploy into production Machine Learning models. The idea is to create a JSON micro-service that takes data as input and classifies that data. To make it as simple as possible, we use sklearn to train a Random Forest Classifier to predict the flower class from the IRIS dataset.

The task is: Given some measures of a flower, determine its species. The IRIS dataset has 150 samples and 3 classes (setosa, versicolor and virginica). Each class has 50 samples. Additionally, each flower is described by 4 real and positive values:

  • Sepal length
  • Sepal width
  • Petal length
  • Petal width

Given the small dataset and as model performance is not our main concern in this tutorial, we will be using all of the data to train a Random Forest classifier.

Script creation

We create 2 methods:

The first method called fafun_train, will train a RF model each time it is invoked. It will also export the trained model as a pickle file to be used later by other methods and micro-services.

def fafun_train():
iris_dataset = load_iris()
X, y = iris_dataset.data, iris_dataset.target
model = RandomForestClassifier()
model.fit(X,y)
filename = ‘model.pkl’
pickle.dump(model, open(filename,”wb”))
return 'Model trained successfully'

The second method is for the prediction and named fafun_predict. This method can be queried multiple times with different flower measures and will predict and return the flower class each time.

def fafun_predict(sepal_length=5.5, sepal_width=2.3,        petal_length=4.0, petal_width=1.3):
model = pickle.load(open(“model.pkl”,”rb”))
x = [[sepal_length, sepal_width, petal_length, petal_width]]
pred = model.predict(x)
targets = [‘setosa’, ‘versicolor’, ‘virginica’]
return targets[pred[0]]

Before uploading our functions to the 5Analytics platform, we organize them into one script classification.py and add the module imports.

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import pickle

Deployment on the platform

The platform can be obtained as a docker image directly from docker or can be downloaded as .tar.gz. To start the platform, we run the following command:

docker run --rm -p 5050:5050 -i -t 5analytics/ada:latest

This will run the latest docker image of the platform and map the port 5050 of the docker image to the port 5050 of localhost so that we can access it outside docker.

Next, we open a browser and enter this URL:

http://localhost:5050/up/web/start/

This will get you on the homepage of the 5Analytics AI Platform. Navigate to Scripts and upload the python script classification.py by drag and drop. Then navigate to Packages and upload (by drag and drop again) the sklearn package:

  • scikit-learn-0.19.1_Python.5aP

If you don’t already have that package, you can download it from our download page.

Now that we have the script and packages uploaded, the platform will turn every method into a microservice that can be queried.

Let’s first train our model by running the fafun_train method. We can do that in 2 different ways:

  • From the browser tab by entering this URL
http://localhost:5050/if/json/python/v1/fafun_train?_token=test_token
  • From terminal
curl "http://localhost:5050/if/json/python/v1/fafun_train?_token=test_token"

In both scenarios, we get the message “Model Trained successfully”. This indicates that we saved a trained model on the platform server to be used later for predictions.

To predict a flower class based on it’s measure, we query the fafun_predict script with the measures as arguments to the method. Again, we have the choice between using the browser or running the URL using curl

curl "http://localhost:5050/if/json/python/v1/fafun_predict?_token=test_token&sepal_length=7.7&sepal_width=2.8&petal_length=6.7&petal_width=2.0"

We get the following output

{
"data": "virginica"
}

What we did is we invoked the micro-service fafun_predict that is running on the 5Analytics python engine, we passed it values for the 4 arguments, ran a prediction through the trained model and returned the result in json format.

Conclusion

This is a very simple example of how to use the 5Analytics platform to train and deploy Machine Learning models. We demonstrated how fast and simple to transition from model creation to deployment through micro-services. More advanced features of the Platform include the ability to connect to different Data Sources, run the micro-services on different End points and to be able to manage user and group access to remain compliant with data privacy laws.

--

--