-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
[R] Basic implementation for R JSON serialization. #5123
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #' Load serialised xgboost model from R's raw vector | ||
| #' | ||
| #' User can generate raw memory buffer by calling xgb.save.raw | ||
| #' | ||
| #' @param buffer the buffer returned by xgb.save.raw | ||
| #' | ||
| #' @export | ||
| xgb.load.raw <- function(buffer) { | ||
| cachelist <- list() | ||
| handle <- .Call(XGBoosterCreate_R, cachelist) | ||
| .Call(XGBoosterLoadModelFromRaw_R, handle, buffer) | ||
| class(handle) <- "xgb.Booster.handle" | ||
| return (handle) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,23 @@ | ||
| #' Save xgboost model to R's raw vector, | ||
| #' user can call xgb.load to load the model back from raw vector | ||
| #' | ||
| #' user can call xgb.load.raw to load the model back from raw vector | ||
| #' | ||
| #' Save xgboost model from xgboost or xgb.train | ||
| #' | ||
| #' | ||
| #' @param model the model object. | ||
| #' | ||
| #' | ||
| #' @examples | ||
| #' data(agaricus.train, package='xgboost') | ||
| #' data(agaricus.test, package='xgboost') | ||
| #' train <- agaricus.train | ||
| #' test <- agaricus.test | ||
| #' bst <- xgboost(data = train$data, label = train$label, max_depth = 2, | ||
| #' bst <- xgboost(data = train$data, label = train$label, max_depth = 2, | ||
| #' eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic") | ||
| #' raw <- xgb.save.raw(bst) | ||
| #' bst <- xgb.load(raw) | ||
| #' bst <- xgb.load.raw(raw) | ||
| #' pred <- predict(bst, test$data) | ||
| #' | ||
| #' @export | ||
| xgb.save.raw <- function(model) { | ||
| model <- xgb.get.handle(model) | ||
| .Call(XGBoosterModelToRaw_R, model) | ||
| handle <- xgb.get.handle(model) | ||
| .Call(XGBoosterModelToRaw_R, handle) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #' Serialize the booster instance into R's raw vector. The serialization method differs | ||
| #' from \code{\link{xgb.save.raw}} as the latter one saves only the model but not | ||
| #' parameters. This serialization format is not stable across different xgboost versions. | ||
| #' | ||
| #' @param booster the booster instance | ||
| #' | ||
| #' @examples | ||
| #' data(agaricus.train, package='xgboost') | ||
| #' data(agaricus.test, package='xgboost') | ||
| #' train <- agaricus.train | ||
| #' test <- agaricus.test | ||
| #' bst <- xgboost(data = train$data, label = train$label, max_depth = 2, | ||
| #' eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic") | ||
| #' raw <- xgb.serialize(bst) | ||
| #' bst <- xgb.unserialize(raw) | ||
| #' | ||
| #' @export | ||
| xgb.serialize <- function(booster) { | ||
| handle <- xgb.get.handle(booster) | ||
| .Call(XGBoosterSerializeToBuffer_R, handle) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #' Load the instance back from \code{\link{xgb.serialize}} | ||
| #' | ||
| #' @param buffer the buffer containing booster instance saved by \code{\link{xgb.serialize}} | ||
| #' | ||
| #' @export | ||
| xgb.unserialize <- function(buffer) { | ||
| cachelist <- list() | ||
| handle <- .Call(XGBoosterCreate_R, cachelist) | ||
| .Call(XGBoosterUnserializeFromBuffer_R, handle, buffer) | ||
| class(handle) <- "xgb.Booster.handle" | ||
| return (handle) | ||
| } |
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.
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.