问题
I have created a R model using mlr and h2o package as below
library(h2o)
rfh20.lrn = makeLearner("classif.h2o.randomForest", predict.type = "prob")
Done the model tunings and model initiates h2o JVM and connects R to h2o cluster, modelling is done and I saved the model as .rds file.
saveRDS(h2orf_mod, "h2orf_mod.rds")
I do the prediction as
pred_h2orf <- predict(h2orf_mod, newdata = newdata)
then i shutdown h2o
h2o.shutdown()
Later I re-call the saved model
h2orf_mod <- readRDS("h2orf_mod.rds")
Initiate h2o so JVM connects R to h2o cluster
h2o.init()
Now the model is from local saved location, cluster doesn't know the model, when i do prediction, I get error which is obvious
ERROR: Unexpected HTTP Status code: 404 Not Found (url = http://localhost:54321/4/Predictions/models/DRF_model_R_1553297204511_743/frames/data.frame_sid_b520_1)
water.exceptions.H2OKeyNotFoundArgumentException
[1] "water.exceptions.H2OKeyNotFoundArgumentException: Object 'DRF_model_R_1553297204511_743' not found in function: predict for argument: model"
Error in .h2o.doSafeREST(h2oRestApiVersion = h2oRestApiVersion, urlSuffix = page, : ERROR MESSAGE: Object 'DRF_model_R_1553297204511_743' not found in function: predict for argument: model
May I know how to handle this, whether the saved model uploaded to cluster or something else, as every time building the model is NOT the effective way.
回答1:
As per the comment instead of saving model using saveDRS/readRDS, save model as
h2oModelsaved <- h2o.saveModel(object = h2orf_model, path = "C:/User/Models/")
Re-call model
h2o.init()
h2oModelLoaded <- h2o.loadModel(h2oModelsaved)
Convert the test data as h2o Frame
newdata <- as.h2o(testdata)
Then Call the predict
pred_h2orf2 <- predict(h2oModelLoaded, newdata = newdata)
This works perfect
来源:https://stackoverflow.com/questions/55823004/how-to-use-the-saved-rds-h2o-model-for-prediction-afterwards