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
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).
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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With