R h2o load a saved model from disk in MOJO or POJO format

前端 未结 3 2208
一生所求
一生所求 2021-02-15 14:22

I\'m catching up on h2o\'s MOJO and POJO model format. I\'m able to save a model in MOJO/POJO with

h2o.download_mojo(model, path = \"/media/somewhe         


        
3条回答
  •  清歌不尽
    2021-02-15 14:45

    h2o.loadModel is meant to be used with h2o.saveModel. If you want to compile and run a MOJO you need to do the following:

    first let's say you created a MOJO from a GBM:

    library(h2o)
    h2o.init(nthreads=-1)
    path = "http://h2o-public-test-data.s3.amazonaws.com/smalldata/prostate/prostate.csv"
    h2o_df = h2o.importFile(path)
    h2o_df$RACE = as.factor(h2o_df$RACE)
    model = h2o.gbm(y="CAPSULE",
            x=c("AGE", "RACE", "PSA", "GLEASON"),
            training_frame=h2o_df,
            distribution="bernoulli",
            ntrees=100,
            max_depth=4,
            learn_rate=0.1)
    

    and then downloaded the MOJO and the resulting h2o-genmodel.jar file to a new experiment folder. Note that the h2o-genmodel.jar file is a library that supports scoring and contains the required readers and interpreters. This file is required when MOJO models are deployed to production.

    modelfile = model.download_mojo(path="~/experiment/", get_genmodel_jar=True)
    print("Model saved to " + modelfile)
    Model saved to /Users/user/GBM_model_R_1475248925871_74.zip"
    

    Then you would open a new terminal window and change into the experiment directory where you have have the MOJO files .zip and .jar.

    $ cd experiment
    

    Then you would create your main program in the experiment folder by creating a new file called main.java (for example, using "vim main.java"). Include the following contents. Note that this file is referencing the GBM model created above using R.

    import java.io.*;
    import hex.genmodel.easy.RowData;
    import hex.genmodel.easy.EasyPredictModelWrapper;
    import hex.genmodel.easy.prediction.*;
    import hex.genmodel.MojoModel;
    
    public class main {
      public static void main(String[] args) throws Exception {
        EasyPredictModelWrapper model = new EasyPredictModelWrapper(MojoModel.load("GBM_model_R_1475248925871_74.zip"));
    
        RowData row = new RowData();
        row.put("AGE", "68");
        row.put("RACE", "2");
        row.put("DCAPS", "2");
        row.put("VOL", "0");
        row.put("GLEASON", "6");
    
        BinomialModelPrediction p = model.predictBinomial(row);
        System.out.println("Has penetrated the prostatic capsule (1=yes; 0=no): " + 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("");
      }
    }
    

    Then compile and run in terminal window 2 to get a display of predicted probabilities

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

提交回复
热议问题