how to save/load a trained model in H2o?

一曲冷凌霜 提交于 2019-12-05 03:14:38

I am, unfortunately, not familiar with the web interface but I can offer a workaround involving H2O in R. The functions

h2o.saveModel(object, dir = "", name = "", filename = "", force = FALSE)

and

h2o.loadModel(path, conn = h2o.getConnection())

Should offer what you need. I will try to have a look at H2O Flow.

Update

I cannot find the possibility to explicitly save a model either. What you can do instead is save the 'Flow'. You ergo could upload/import your file, build the model and then save / load the status :-)

When viewing the model in H2O Flow, you will see an 'Export' button as an action that can be taken against a model

From there, you will be prompted to specify a path in 'Export Model' dialog. Specify the path and hit the 'Export' button. That will save you model to disk.

I'm referring to H2O version 3.2.0.3

A working example that I've used recently while building a deep learning model in version 2.8.6 in h2o.The model was saved in hdfs.For latest version you probably have to remove the classification=T switch and have to replace data with training_frame

library(h2o)
h = h2o.init(ip="xx.xxx.xxx.xxx", port=54321, startH2O = F)

cTrain.h2o <- as.h2o(h,cTrain,key="c1")
cTest.h2o <- as.h2o(h,cTest,key="c2")

nh2oD<-h2o.deeplearning(x =c(1:12),y="tgt",data=cTrain.h2o,classification=F,activation="Tanh",
                        rate=0.001,rho=0.99,momentum_start=0.5,momentum_stable=0.99,input_dropout_ratio=0.2,                        
                        hidden=c(12,25,11,11),hidden_dropout_ratios=c(0.4,0.4,0.4,0.4),
                        epochs=150,variable_importances=T,seed=1234,reproducible = T,l1=1e-5,
                        key="dn")

hdfsdir<-"hdfs://xxxxxxxxxx/user/xxxxxx/xxxxx/models"

h2o.saveModel(nh2oD,hdfsdir,name="DLModel1",save_cv=T,force=T)

test=h2o.loadModel(h,path=paste0(hdfsdir,"/","DLModel1"))

This should be what you need:

library(h2o)
h2o.init()
path = system.file("extdata", "prostate.csv", package = "h2o")
h2o_df = h2o.importFile(path)
h2o_df$CAPSULE = as.factor(h2o_df$CAPSULE)
model = h2o.glm(y = "CAPSULE",
              x = c("AGE", "RACE", "PSA", "GLEASON"),
              training_frame = h2o_df,
              family = "binomial")
h2o.download_pojo(model)

http://h2o-release.s3.amazonaws.com/h2o/rel-slater/5/docs-website/h2o-docs/index.html#POJO%20Quick%20Start

How to save models in H2O Flow:

  1. go to "List All Models"

  2. in the model details, you will find an "Export" option

  3. enter the model name you want to save it as
  4. import it back again

How to save a model trained in h2o-py:

# say "rf" is your H2ORandomForestEstimator object. To export it
>>> path = h2o.save_model(rf, force=True) # save_model() returns the path
>>> path
u'/home/user/rf'

#to import it back again(as a new object)
>>> rafo = h2o.load_model(path)
>>> rafo   # prints model details
Model Details
=============
H2ORandomForestEstimator :  Distributed Random Forest
Model Key:  drf1
Model Summary:
######Prints model details...................
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!