Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save All models from h2o automl

Tags:

r

h2o

I'm trying to save all the models from an h2o.automl as part of the h2o package. Currently I am able to save a single model using h2o.saveModel(aml@leader, path = "/home/data/user").

How can I save all the models?

Here is my attempt on a sample dataset:

 library(h2o)
 h2o.init()
 prostate.hex <- h2o.importFile(path = paste("https://raw.github.com",
    "h2oai/h2o-2/master/smalldata/logreg/prostate.csv", sep = "/"),
    destination_frame = "prostate.hex")

Get data from github or import via readr:

 library(readr)
 prostate <- read_csv("/home/data/user/prostate.csv")

 prostate.hex<- as.h2o(prostate, "prostate.hex")

 aml <- h2o.automl(y = "CAPSULE", x = c("AGE","RACE","PSA","DCAPS"),
    training_frame = prostate.hex,
    max_runtime_secs = 180,
    exclude_algos = c("StackedEnsemble")
    )

Now I'm trying to save the models within aml:

mod_ids <- as_tibble(aml@leaderboard$model_id)

Now I can't figure out how to save the models:

 for(i in 1:nrow(mod_ids)) {
   print(mod_ids[i,])
   #h2o.saveModel(object = aml@leaderboard[[i]], "/home/data/user/")
 }

Here is what I've tried:

tutorial automl

H2O AUTOML: How to save reuse and build on top of existing automl models

like image 929
Ryan John Avatar asked Sep 12 '18 15:09

Ryan John


People also ask

How to save H2O AutoML model?

If you autoML object called aml , then aml@leaderboard$model_id has the list of all model ids that were built. You can then pass each of those to h2o. getModel() (which you can then call h2o. saveModel() on).

How to save an H2O model?

In R and Python, you can save a model locally or to HDFS using the h2o. saveModel (R) or h2o. save_model (Python) function . This function accepts the model object and the file path.

How does H2O AutoML work?

H2O AutoML functionalitiesTrains a Random grid of algorithms like GBMs, DNNs, GLMs, etc. using a carefully chosen hyper-parameter space. Individual models are tuned using cross-validation. Two Stacked Ensembles are trained.

Does AutoML use cross-validation?

Cross-validation approach is applied. The default number of folds depends on the number of rows. If the dataset is less than 1,000 rows, 10 folds are used. If the rows are between 1,000 and 20,000, then three folds are used.


1 Answers

Try this, it'll do your job:

 for(i in 1:nrow(mod_ids)) {

    aml1 <- h2o.getModel(aml@leaderboard[i, 1]) # get model object in environment
    h2o.saveModel(object = aml1, "C:/Users/sm/Documents/stack/models") # pass that model object to h2o.saveModel as an argument

  }
like image 164
sm925 Avatar answered Sep 23 '22 06:09

sm925