Skip to content

predict() does not work if fitted workflow object has been saved as rds. #45

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
fderyckel opened this issue May 24, 2020 · 7 comments
Closed

Comments

@fderyckel
Copy link

I have used this page for help:
https://workflows.tidymodels.org/reference/predict-workflow.html

the workflow is a nlp task using lasso and textrecipes.

I fitted a workflow object using fit(wf, train_data)
Then I use the workflow object with predict(wf, newdata). no error. It works as expected.

If I saved the workflow object using write_rds(). Close the session, clean all objects, restart r.
I read the workflow object using read_rds(wf.rds), then apply predict to that object. It gives me error.

model_lasso_enhanced <- read_rds("disaster_tweets/models/model_lasso_enhanced.rds")
yo <- predict(model_lasso_enhanced, new_data = test, type = "prob")
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "c('lognet', 'glmnet')"
class(model_lasso_enhanced)
[1] "workflow"

@fderyckel
Copy link
Author

fderyckel commented May 24, 2020

OK I got it.
That is because when calling predict() in a new session, the library(glmnet) wasn't on. Although tidyverse, tidymodels, textrecipes, tidytext were called, glmnet wasn't explicitly called.

These libraries are loaded automatically in the workflow with the set_engine(). But they are not automatically loaded when calling back predict() later on, hence the error.

Maybe, the predict function could call these libraries like the set_engine() do?

Thanks again for that awesome set of packages. Still tinkering with it all.

@topepo
Copy link
Member

topepo commented May 24, 2020

We'll be fixing that soon: tidymodels/parsnip#308

@fderyckel
Copy link
Author

I see. Thanks again for all your work with your team. Phenomenal!

@DavisVaughan
Copy link
Member

We can track this with the parsnip issue

@andrewcstewart
Copy link

So is write_rds / read_rds the recommended way to serialize/deserialize a workflow?

@topepo
Copy link
Member

topepo commented Jun 26, 2020

yes unless it involves a model like keras that stores the data in memory (and not locally in the R object).

@github-actions
Copy link

github-actions bot commented Mar 6, 2021

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 6, 2021
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants