以后如何使用保存的

以后如何使用保存的

本文介绍了以后如何使用保存的.rds h2o模型进行预测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用mlr和h2o软件包创建了一个R模型,如下所示:

I have created a R model using mlr and h2o package as below

library(h2o)
rfh20.lrn = makeLearner("classif.h2o.randomForest", predict.type = "prob")

完成模型调整,模型启动h2o JVM并将R连接到h2o集群,建模完成,我将模型另存为.rds文件.

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")

我做预测

pred_h2orf <- predict(h2orf_mod, newdata = newdata)

然后我关闭h2o

h2o.shutdown()

稍后我重新调用保存的模型

Later I re-call the saved model

h2orf_mod <- readRDS("h2orf_mod.rds")

启动h2o,以便JVM将R连接到h2o集群

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.

推荐答案

根据注释,而不是使用saveDRS/readRDS保存模型,而是将模型另存为

As per the comment instead of saving model using saveDRS/readRDS, save model as

h2oModelsaved <- h2o.saveModel(object = h2orf_model, path = "C:/User/Models/")

重新调用模型

h2o.init()
h2oModelLoaded <- h2o.loadModel(h2oModelsaved)

将测试数据转换为h2o Frame

Convert the test data as h2o Frame

newdata <- as.h2o(testdata)

然后调用预测

pred_h2orf2 <- predict(h2oModelLoaded, newdata = newdata)

这很完美

这篇关于以后如何使用保存的.rds h2o模型进行预测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 19:36