fix segfault and add two function for handle and booster
This commit is contained in:
parent
0aef62dabc
commit
4c25600d2a
@ -36,12 +36,7 @@ setMethod("predict", signature = "xgb.Booster",
|
||||
if (class(object) != "xgb.Booster"){
|
||||
stop("predict: model in prediction must be of class xgb.Booster")
|
||||
} else {
|
||||
if (is.null(object$handle)) {
|
||||
object$handle <- xgb.load(object$raw)
|
||||
} else {
|
||||
if (is.null(object$raw))
|
||||
object$raw <- xgb.save.raw(object$handle)
|
||||
}
|
||||
object <- xgb.Booster.check(object, saveraw = FALSE)
|
||||
}
|
||||
if (class(newdata) != "xgb.DMatrix") {
|
||||
if (is.null(missing)) {
|
||||
|
||||
@ -6,9 +6,9 @@ setMethod("predict", signature = "xgb.Booster.handle",
|
||||
stop("predict: model in prediction must be of class xgb.Booster.handle")
|
||||
}
|
||||
|
||||
bst <- list(handle = object,raw = NULL)
|
||||
class(bst) <- 'xgb.Booster'
|
||||
bst$raw <- xgb.save.raw(bst$handle)
|
||||
bst <- xgb.handleToBooster(object)
|
||||
# Avoid save a handle without update
|
||||
# bst$raw <- xgb.save.raw(object)
|
||||
|
||||
ret = predict(bst, ...)
|
||||
return(ret)
|
||||
|
||||
@ -68,6 +68,26 @@ xgb.Booster <- function(params = list(), cachelist = list(), modelfile = NULL) {
|
||||
return(structure(handle, class = "xgb.Booster.handle"))
|
||||
}
|
||||
|
||||
# convert xgb.Booster.handle to xgb.Booster
|
||||
xgb.handleToBooster <- function(handle)
|
||||
{
|
||||
bst <- list(handle = handle, raw = NULL)
|
||||
class(bst) <- "xgb.Booster"
|
||||
return(bst)
|
||||
}
|
||||
|
||||
# Check whether an xgb.Booster object is complete
|
||||
xgb.Booster.check <- function(bst, saveraw = TRUE)
|
||||
{
|
||||
if (is.null(bst$handle)) {
|
||||
bst$handle <- xgb.load(bst$raw)
|
||||
} else {
|
||||
if (is.null(bst$raw) && saveraw)
|
||||
bst$raw <- xgb.save.raw(bst$handle)
|
||||
}
|
||||
return(bst)
|
||||
}
|
||||
|
||||
## ----the following are low level iteratively function, not needed if
|
||||
## you do not want to use them ---------------------------------------
|
||||
# get dmatrix from data, label
|
||||
|
||||
@ -41,12 +41,7 @@ xgb.dump <- function(model = NULL, fname = NULL, fmap = "", with.stats=FALSE) {
|
||||
if (class(model) != "xgb.Booster") {
|
||||
stop("model: argument must be type xgb.Booster")
|
||||
} else {
|
||||
if (is.null(model$handle)) {
|
||||
model$handle <- xgb.load(model$raw)
|
||||
} else {
|
||||
if (is.null(model$raw))
|
||||
model$raw <- xgb.save.raw(model$handle)
|
||||
}
|
||||
model <- xgb.Booster.check(model)
|
||||
}
|
||||
if (!(class(fname) %in% c("character", "NULL") && length(fname) <= 1)) {
|
||||
stop("fname: argument must be type character (when provided)")
|
||||
|
||||
@ -20,9 +20,8 @@ xgb.load <- function(modelfile) {
|
||||
if (is.null(modelfile))
|
||||
stop("xgb.load: modelfile cannot be NULL")
|
||||
|
||||
bst <- list(handle = NULL,raw = NULL)
|
||||
class(bst) <- 'xgb.Booster'
|
||||
bst$handle <- xgb.Booster(modelfile = modelfile)
|
||||
bst$raw <- xgb.save.raw(bst$handle)
|
||||
handle <- xgb.Booster(modelfile = modelfile)
|
||||
bst <- xgb.handleToBooster(handle)
|
||||
bst <- xgb.Booster.check(bst)
|
||||
return(bst)
|
||||
}
|
||||
|
||||
@ -22,9 +22,7 @@ xgb.save <- function(model, fname) {
|
||||
stop("xgb.save: fname must be character")
|
||||
}
|
||||
if (class(model) == "xgb.Booster") {
|
||||
if (is.null(model$handle)) {
|
||||
model$handle <- xgb.load(model$raw)
|
||||
}
|
||||
model <- xgb.Booster.check(model)
|
||||
.Call("XGBoosterSaveModel_R", model$handle, fname, PACKAGE = "xgboost")
|
||||
return(TRUE)
|
||||
}
|
||||
|
||||
@ -86,9 +86,8 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(),
|
||||
}
|
||||
params = append(params, list(...))
|
||||
|
||||
bst <- list(handle = NULL,raw = NULL)
|
||||
class(bst) <- 'xgb.Booster'
|
||||
bst$handle <- xgb.Booster(params, append(watchlist, dtrain))
|
||||
handle <- xgb.Booster(params, append(watchlist, dtrain))
|
||||
bst <- xgb.handleToBooster(handle)
|
||||
for (i in 1:nrounds) {
|
||||
succ <- xgb.iter.update(bst$handle, dtrain, i - 1, obj)
|
||||
if (length(watchlist) != 0) {
|
||||
@ -96,6 +95,6 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(),
|
||||
cat(paste(msg, "\n", sep=""))
|
||||
}
|
||||
}
|
||||
bst$raw <- xgb.save.raw(bst$handle)
|
||||
bst <- xgb.Booster.check(bst)
|
||||
return(bst)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user