Search code examples
rh2omlr

How to save an h2o model using mlr in R?


How can I save an h2o model trained with the mlr package and load it in a new session to predict the target variable for a new data set? In the following example I tried it with save and h2o.saveModel, but it throws an error.

library(mlr)
a <- data.frame(y=factor(c(1,1,1,1,1,1,1,1,0,0,1,0)), 
                x1=rep(c("a","b", "c"), times=c(6,3,3)))
aTask <- makeClassifTask(data = a, target = "y", positive="1")
h2oLearner <- makeLearner("classif.h2o.deeplearning")
model <- train(h2oLearner, aTask)
# save mlr and h2o model separately:
save(file="saveh2omodel.rdata", list=c("model"))
h2o.saveModel(getLearnerModel(model), path="h2o_model")

# shutdown h2o and close R and open new session
h2o.shutdown()

library(mlr)
library(h2o)
h2o.init()
h2o.loadModel("h2o_model")
load(file="saveh2omodel.rdata")
#ERROR: Unexpected HTTP Status code: 412 Precondition Failed (url = http://localhost:54321/99/Models.bin/)
# Error in .h2o.doSafeREST(h2oRestApiVersion = h2oRestApiVersion, urlSuffix = page,  :                             
#  ERROR MESSAGE:
#  Illegal argument: dir of function: importModel: h2o_model

b <- data.frame(x1=rep(c("a","b", "c"), times=c(3,5,4)))
pred <- predict(model, newdata=b) 
# only works if h2o wasn't shut down!

Solution

  • You're using h2o.loadModel incorrectly (nothing to do with mlr). See the example usage in h2o's help -- h2o.saveModel returns the full path you need to give to h2o.loadModel. Full working example:

    library(mlr)
    library(h2o)
    a <- data.frame(y=factor(c(1,1,1,1,1,1,1,1,0,0,1,0)), 
                    x1=rep(c("a","b", "c"), times=c(6,3,3)))
    aTask <- makeClassifTask(data = a, target = "y", positive="1")
    h2oLearner <- makeLearner("classif.h2o.deeplearning")
    model <- train(h2oLearner, aTask)
    # save mlr and h2o model separately:
    save(file="saveh2omodel.rdata", list=c("model"))
    savedModel = h2o.saveModel(getLearnerModel(model), path="h2o_model")
    
    # shutdown h2o and close R and open new session
    h2o.shutdown()
    
    library(mlr)
    library(h2o)
    h2o.init()
    h2o.loadModel(savedModel)
    load(file="saveh2omodel.rdata")
    b <- data.frame(x1=rep(c("a","b", "c"), times=c(3,5,4)))
    pred <- predict(model, newdata=b)