-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
[R] Provide better guidance for persisting XGBoost model #5964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -308,18 +308,64 @@ xgb.createFolds <- function(y, k = 10) | |
| #' @name xgboost-deprecated | ||
| NULL | ||
|
|
||
| #' Do not use saveRDS() for long-term archival of models. Use xgb.save() instead. | ||
| #' Do not use \code{\link[base]{saveRDS}} or \code{\link[base]{save}} for long-term archival of | ||
| #' models. Instead, use \code{\link{xgb.save}} or \code{\link{xgb.save.raw}}. | ||
| #' | ||
| #' It is a common practice to use the built-in \code{saveRDS()} function to persist R objects to | ||
| #' the disk. While \code{xgb.Booster} objects can be persisted with \code{saveRDS()} as well, it | ||
| #' is not advisable to use it if the model is to be accessed in the future. If you train a model | ||
| #' with the current version of XGBoost and persist it with \code{saveRDS()}, the model is not | ||
| #' guaranteed to be accessible in later releases of XGBoost. To ensure that your model can be | ||
| #' accessed in future releases of XGBoost, use \code{xgb.save()} instead. For more details and | ||
| #' explanation, consult the page | ||
| #' It is a common practice to use the built-in \code{\link[base]{saveRDS}} function (or | ||
| #' \code{\link[base]{save}}) to persist R objects to the disk. While it is possible to persist | ||
| #' \code{xgb.Booster} objects using \code{\link[base]{saveRDS}}, it is not advisable to do so if | ||
| #' the model is to be accessed in the future. If you train a model with the current version of | ||
| #' XGBoost and persist it with \code{\link[base]{saveRDS}}, the model is not guaranteed to be | ||
| #' accessible in later releases of XGBoost. To ensure that your model can be accessed in future | ||
| #' releases of XGBoost, use \code{\link{xgb.save}} or \code{\link{xgb.save.raw}} instead. | ||
| #' | ||
| #' @details | ||
| #' Use \code{\link{xgb.save}} to save the XGBoost model as a stand-alone file. You may opt into | ||
| #' the JSON format by specifying the JSON extension. To read the model back, use | ||
| #' \code{\link{xgb.load}}. | ||
| #' | ||
| #' Use \code{\link{xgb.save.raw}} to save the XGBoost model as a sequence (vector) of raw bytes | ||
| #' in a future-proof manner. Future releases of XGBoost will be able to read the raw bytes and | ||
| #' re-construct the corresponding model. To read the model back, use \code{\link{xgb.load.raw}}. | ||
| #' The \code{\link{xgb.save.raw}} function is useful if you'd like to persist the XGBoost model | ||
| #' as part of another R object. | ||
| #' | ||
| #' Note: Do not use \code{\link{xgb.serialize}} to store models long-term. It persists not only the | ||
| #' model but also internal configurations and parameters, and its format is not stable across | ||
| #' multiple XGBoost versions. Use \code{\link{xgb.serialize}} only for checkpointing. | ||
| #' | ||
| #' For more details and explanation about model persistence and archival, consult the page | ||
| #' \url{https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html}. | ||
| #' | ||
| #' @name a-compatibility-note-for-saveRDS | ||
| #' @examples | ||
| #' data(agaricus.train, package='xgboost') | ||
| #' bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 2, | ||
| #' eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic") | ||
| #' | ||
| #' # Save as a stand-alone file; load it with xgb.load() | ||
| #' xgb.save(bst, 'xgb.model') | ||
| #' bst2 <- xgb.load('xgb.model') | ||
| #' | ||
| #' # Save as a stand-alone file (JSON); load it with xgb.load() | ||
| #' xgb.save(bst, 'xgb.model.json') | ||
| #' bst2 <- xgb.load('xgb.model.json') | ||
| #' | ||
| #' # Save as a raw byte vector; load it with xgb.load.raw() | ||
| #' xgb_bytes <- xgb.save.raw(bst) | ||
| #' bst2 <- xgb.load.raw(xgb_bytes) | ||
|
Comment on lines
+353
to
+355
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: Currently |
||
| #' | ||
| #' # Persist XGBoost model as part of another R object | ||
| #' obj <- list(xgb_model_bytes = xgb.save.raw(bst), description = "My first XGBoost model") | ||
| #' # Persist the R object. Here, saveRDS() is okay, since it doesn't persist | ||
| #' # xgb.Booster directly. What's being persisted is the future-proof byte representation | ||
| #' # as given by xgb.save.raw(). | ||
| #' saveRDS(obj, 'my_object.rds') | ||
| #' # Read back the R object | ||
| #' obj2 <- readRDS('my_object.rds') | ||
| #' # Re-construct xgb.Booster object from the bytes | ||
| #' bst2 <- xgb.load.raw(obj2$xgb_model_bytes) | ||
| #' | ||
| #' @name a-compatibility-note-for-saveRDS-save | ||
| NULL | ||
|
|
||
| # Lookup table for the deprecated parameters bookkeeping | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.