2

I have created a ranger model using mlr3 library. I saved this model to my machine using following command. The created file is huge in size. The saved file also has the data along with the model. Is there a way to only save the model without the data?

learner_ranger = lrn("classif.ranger", predict_type = "prob", predict_sets = c("train", "test"), importance = "impurity", num.threads = 8)
learner_ranger$train(train_task])
save(learner_ranger, file = "model.rda")

When I try to load this saved model it does not load the model correctly.

learner_ranger = load("model.rda")
str(learner_ranger)
Error in str(learner_ranger) : object 'learner_ranger' not found

To reduce the file size and save only the model, I have tried following but I am getting an error

save(learner_ranger$model, file = "model.rda")

The error I am getting is -
Error in save(learner_ranger$model, file = "model.rda") : 
  object ‘learner_randomF$model’ not found
Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204
Saurabh
  • 1,566
  • 10
  • 23

1 Answers1

1

After some research found that there are two ways to save and load the model in R:

  1. using save(), load(): When we use save(), we will have to load it using the same name.

  2. using saveRDS(), loadRDS(): saveRDS() does not save the model name and we have the flexibility to load the model in any other name. But saveRDS() can only save one object at a time as it is a lower-level function.

Most people prefer saveRDS() over save() as it serializes the object.

I am still looking for ways to save the model without data.

Saurabh
  • 1,566
  • 10
  • 23