David Leal
David Leal

Reputation: 6759

How to specify the file name when saving the model using h2o package from R

I am trying to save the model build using the function: h2o.saveModel(), based on function description on page 159 of the H2O user manual for R, the arguments only consider path. I looked at other similar function such as: h2o.saveModelDetails() but it uses the same argument. Please advise if there any another way to specify the name of the model.

Upvotes: 4

Views: 2564

Answers (4)

DataYoda
DataYoda

Reputation: 825

in python :

model_path = h2o.save_model(model=model, path="mymodel1", force=True)
path = os.path.dirname(os.path.abspath(model_path))
os.rename(model_path, os.path.join(path,f'h2o_new_name'))

Upvotes: 2

Madcat
Madcat

Reputation: 379

I think a better work around would be to generate a unique folder each time saving the model. When loading model there will always be only one model file under the path.

saved_model = os.path.join('UNIQUE_MODEL_PATH', os.listdir('UNIQUE_MODEL_PATH')[0])
loaded_model = h2o.load_model(saved_model)

Upvotes: 2

Sutirtha Thakur
Sutirtha Thakur

Reputation: 13

Here a possible way to do it:

output_dir <-getwd()
DRF_MO <- h2o.saveModel(object=aml, path=output_dir, force=TRUE)
DRF_MO <- file.path(output_dir, aml@algorithm) 
file.rename(file.path(output_dir, aml@model_id), DRF_MO)

Upvotes: 0

Erin LeDell
Erin LeDell

Reputation: 8819

The name of the model file will be determined by the ID of the model. So if you specify model_id when training your model, then you can customize it. Right now there is no way to change the ID of the model after it's been trained.

The file can be renamed once saved:

h2o.saveModel(object = fit, path = path.value, force = TRUE) # force overwriting
name <- file.path(path.value, fileName) # destination file name at the same folder location
file.rename(file.path(path.value, fit@model_id), name)

Upvotes: 4

Related Questions