| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.Booster.R |
| 3 |
\name{xgb.is.same.Booster} |
| 4 |
\alias{xgb.is.same.Booster} |
| 5 |
\title{Check if two boosters share the same C object} |
| 6 |
\usage{ |
| 7 |
xgb.is.same.Booster(obj1, obj2) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{obj1}{Booster model to compare with \code{obj2}.} |
| 11 |
|
| 12 |
\item{obj2}{Booster model to compare with \code{obj1}.} |
| 13 |
} |
| 14 |
\value{ |
| 15 |
Either \code{TRUE} or \code{FALSE} according to whether the two boosters share the |
| 16 |
underlying C object. |
| 17 |
} |
| 18 |
\description{ |
| 19 |
Checks whether two booster objects refer to the same underlying C object. |
| 20 |
} |
| 21 |
\details{ |
| 22 |
As booster objects (as returned by e.g. \code{\link[=xgb.train]{xgb.train()}}) contain an R 'externalptr' |
| 23 |
object, they don't follow typical copy-on-write semantics of other R objects - that is, if |
| 24 |
one assigns a booster to a different variable and modifies that new variable through in-place |
| 25 |
methods like \code{\link[=xgb.attr<-]{xgb.attr<-()}}, the modification will be applied to both the old and the new |
| 26 |
variable, unlike typical R assignments which would only modify the latter. |
| 27 |
|
| 28 |
This function allows checking whether two booster objects share the same 'externalptr', |
| 29 |
regardless of the R attributes that they might have. |
| 30 |
|
| 31 |
In order to duplicate a booster in such a way that the copy wouldn't share the same |
| 32 |
'externalptr', one can use function \code{\link[=xgb.copy.Booster]{xgb.copy.Booster()}}. |
| 33 |
} |
| 34 |
\examples{ |
| 35 |
library(xgboost) |
| 36 |
|
| 37 |
data(mtcars) |
| 38 |
|
| 39 |
y <- mtcars$mpg |
| 40 |
x <- as.matrix(mtcars[, -1]) |
| 41 |
|
| 42 |
model <- xgb.train( |
| 43 |
params = xgb.params(nthread = 1), |
| 44 |
data = xgb.DMatrix(x, label = y, nthread = 1), |
| 45 |
nrounds = 3 |
| 46 |
) |
| 47 |
|
| 48 |
model_shallow_copy <- model |
| 49 |
xgb.is.same.Booster(model, model_shallow_copy) # same C object |
| 50 |
|
| 51 |
model_deep_copy <- xgb.copy.Booster(model) |
| 52 |
xgb.is.same.Booster(model, model_deep_copy) # different C objects |
| 53 |
|
| 54 |
# In-place assignments modify all references, |
| 55 |
# but not full/deep copies of the booster |
| 56 |
xgb.attr(model_shallow_copy, "my_attr") <- 111 |
| 57 |
xgb.attr(model, "my_attr") # gets modified |
| 58 |
xgb.attr(model_deep_copy, "my_attr") # doesn't get modified |
| 59 |
} |
| 60 |
\seealso{ |
| 61 |
\code{\link[=xgb.copy.Booster]{xgb.copy.Booster()}} |
| 62 |
} |