| 1 |
#' Save XGBoost model to R's raw vector |
| 2 |
#' |
| 3 |
#' Save XGBoost model from [xgboost()] or [xgb.train()]. |
| 4 |
#' Call [xgb.load.raw()] to load the model back from raw vector. |
| 5 |
#' |
| 6 |
#' @param model The model object. |
| 7 |
#' @param raw_format The format for encoding the booster: |
| 8 |
#' - "json": Encode the booster into JSON text document. |
| 9 |
#' - "ubj": Encode the booster into Universal Binary JSON. |
| 10 |
#' - "deprecated": Encode the booster into old customized binary format. |
| 11 |
#' |
| 12 |
#' @examples |
| 13 |
#' \dontshow{RhpcBLASctl::omp_set_num_threads(1)} |
| 14 |
#' data(agaricus.train, package = "xgboost") |
| 15 |
#' data(agaricus.test, package = "xgboost") |
| 16 |
#' |
| 17 |
#' ## Keep the number of threads to 1 for examples |
| 18 |
#' nthread <- 1 |
| 19 |
#' data.table::setDTthreads(nthread) |
| 20 |
#' |
| 21 |
#' train <- agaricus.train |
| 22 |
#' test <- agaricus.test |
| 23 |
#' |
| 24 |
#' bst <- xgb.train( |
| 25 |
#' data = xgb.DMatrix(train$data, label = train$label, nthread = 1), |
| 26 |
#' nrounds = 2, |
| 27 |
#' params = xgb.params( |
| 28 |
#' max_depth = 2, |
| 29 |
#' nthread = nthread, |
| 30 |
#' objective = "binary:logistic" |
| 31 |
#' ) |
| 32 |
#' ) |
| 33 |
#' |
| 34 |
#' raw <- xgb.save.raw(bst) |
| 35 |
#' bst <- xgb.load.raw(raw) |
| 36 |
#' |
| 37 |
#' @export |
| 38 |
xgb.save.raw <- function(model, raw_format = "ubj") { |
| 39 |
handle <- xgb.get.handle(model) |
| 40 |
args <- list(format = raw_format) |
| 41 |
.Call(XGBoosterSaveModelToRaw_R, handle, jsonlite::toJSON(args, auto_unbox = TRUE)) |
| 42 |
} |