write_h2o: Saves an H2O model to file that is contained within a fitted...

View source: R/read_h2o.R

write_h2oR Documentation

Saves an H2O model to file that is contained within a fitted parsnip model specification or contained within a workflow

Description

H2O models cannot be saved using the typical R approaches such as saveRDS because the actual H2O model is contained within a Java virtual machine. H2O models need to be saved and restored using the 'h2o::h2o.saveModel' and 'h2o::h2o.loadModel' functions. This is inconvenient for using H2O models contained within parsnip model specifications or workflow objects.

Usage

write_h2o(object, filename, ...)

read_h2o(object, filename, ...)

## S3 method for class 'workflow'
write_h2o(object, filename, ...)

## S3 method for class 'model_fit'
write_h2o(object, filename, ...)

## S3 method for class 'workflow'
read_h2o(object, filename, ...)

## S3 method for class 'model_fit'
read_h2o(object, filename, ...)

Arguments

object

Either a 'workflows::workflow()' object contained a fitted model when using the workflows package, or a 'model_spec' object from a fitted model when using the parsnip package directly.

filename

A 'character' specifying the file path used to save the model. H2O models do not require a specific file extension.

...

Currently not used.

Details

The 'write_h2o' function extracts the H2O model from within a parsnip or workflow fitted model and saves it to file using the 'h2o::h2o.saveModel' function. To restore a model and insert it back into a previously fitted model use the 'read_h2o' function.

Value

The file path used to save the model.

Examples

library(parsnip)
library(h2o)

# start a h2o session
h2o.init()

# fit a parsnip model using the h2o engine
clf <- mlp(mode = "classification") %>%
  set_engine("h2o")

model_fit <- clf %>% fit(Species ~ ., iris)

# save the parsnip model
saveRDS(model_fit, file.path(tempdir(), "my_model.rds"))

# save the h2o component of the model
write_h2o(object = model_fit, filename = file.path(tempdir(), "my_h2o_model.mod"))
h2o.shutdown(prompt = FALSE)

# restore a model
h2o.init()
model_fit <- readRDS(file.path(tempdir(), "my_model.rds"))

# read and insert the H2O model back into the parsnip object
model_fit <- read_h2o(model_fit, file.path(tempdir(), "my_h2o_model.mod"))

stevenpawley/h2oparsnip documentation built on June 20, 2022, 12:48 p.m.