H2o.ai Productionizing

ARTIFICIAL INTELLIGENCE

DECEMBER 03, 2019

Demystifying H2O.ai | Productionizing

 

In this post let us see a sample of how to productionize the model we get from the H2O. The goal of this post is to develop a simple H2O based predictive model, convert it into a Plain Old Java Object (POJO), compile it along with other Java packages, and package the compiled class files into a deployable JAR file so that it can readily be deployed onto any Java-based application servers. This model will accept the input data set in the form of a CSV file and return the predicted output in CSV format.

For this example, I am using the “GBM_Airlines_Classification” flow from the examples of H2O’s flow dashboard. If you are unaware of how to use H2O’s FLow please go through the previous post in the series.

Follow the flow by running each cell until you get to the last cell to getModel.

Step 1: Download POJO object to the local system(Downloads folder).

Step 2: Download model pieces in a new terminal window. Note that H2O must still be running in terminal window #1:


$ mkdir experiment
$ cd experiment
$ mv ~/Downloads/gbm_pojo_test.java .
$ curl http://localhost:54321/3/h2o-genmodel.jar > h2o-genmodel.jar

Step 3: Create your main program in terminal window #2 by creating a new file called main.java

(vim main.java)
with the following contents:


import java.io.*;
import hex.genmodel.easy.RowData;
import hex.genmodel.easy.EasyPredictModelWrapper;
import hex.genmodel.easy.prediction.*;

public class main {
  private static String modelClassName = "gbm_pojo_test";

  public static void main(String[] args) throws Exception {
    hex.genmodel.GenModel rawModel;
    rawModel = (hex.genmodel.GenModel) Class.forName(modelClassName).newInstance();
    EasyPredictModelWrapper model = new EasyPredictModelWrapper(rawModel);

    RowData row = new RowData();
    row.put("Year", "1987");
    row.put("Month", "10");
    row.put("DayofMonth", "14");
    row.put("DayOfWeek", "3");
    row.put("CRSDepTime", "730");
    row.put("UniqueCarrier", "PS");
    row.put("Origin", "SAN");
    row.put("Dest", "SFO");

    BinomialModelPrediction p = model.predictBinomial(row);
    System.out.println("Label (aka prediction) is flight departure delayed: " + p.label);
    System.out.print("Class probabilities: ");
    for (int i = 0; i < p.classProbabilities.length; i++) {
      if (i > 0) {
        System.out.print(",");
      }
      System.out.print(p.classProbabilities[i]);
    }
    System.out.println("");
  }
}

Step 4: Compile the POJO in terminal window 2.


$ javac -cp h2o-genmodel.jar -J-Xmx2g -J-XX:MaxPermSize=128m 
gbm_pojo_test.java main.java

Step 5: Run the POJO in terminal window 2.


For Linux and OS X users: 
$ java -cp .:h2o-genmodel.jar main
For Windows users: 
$ java -cp .;h2o-genmodel.jar main
--------------------------------------------------------------------
Output:
Label (aka prediction) is flight departure delayed: YES
Class probabilities: 0.4319916897116479,0.5680083102883521

This post is one of the ways to use POJO file in production. Think of it as a java object and all nuances that apply for any Java object will apply for this as well.

Hoping these series on H2O would give you enough information and clear picture to get you started.

H2O appears to be a powerful tool at anyone’s tech stack, especially when dealing with tons of data without GPU’s. Also, it would be a cool tech to know of, that will make you stand out from your peers.

WRITTEN BY

Rehan Ahmad

AI Expert at wavelabs.ai

Want to explore all the ways you can start, run & grow your business?

Fill out the information below and we will get in touch with you shortly.

Related Articles