-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[R] Basic implementation for R for JSON serialization.
* Change `xgb.save.raw' into full serialization instead of simple model. * Add `xgb.load.raw' for unserialization. * Force renew.
- Loading branch information
1 parent
6601a64
commit 5b76aea
Showing
11 changed files
with
166 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#' 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. The serialization format is not stable across different xgboost versions. | ||
#' | ||
#' @param booster the booster instance | ||
#' | ||
#' @export | ||
xgb.serialize <- function(booster) { | ||
handle <- xgb.get.handle(booster) | ||
.Call(XGBoosterSerializeToBuffer_R, handle) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.