fix segfault and add two function for handle and booster

This commit is contained in:
hetong007 2015-02-09 17:28:48 -08:00
parent 0aef62dabc
commit 4c25600d2a
7 changed files with 32 additions and 26 deletions

View File

@ -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)) {

View File

@ -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)

View File

@ -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

View File

@ -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)")

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}