Debian Codegraph @

man/a-compatibility-note-for-saveRDS-save.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/utils.R
3 \name{a-compatibility-note-for-saveRDS-save}
4 \alias{a-compatibility-note-for-saveRDS-save}
5 \title{Model Serialization and Compatibility}
6 \description{
7 When it comes to serializing XGBoost models, it's possible to use R serializers such as
8 \code{\link[=save]{save()}} or \code{\link[=saveRDS]{saveRDS()}} to serialize an XGBoost model object, but XGBoost also provides
9 its own serializers with better compatibility guarantees, which allow loading
10 said models in other language bindings of XGBoost.
11
12 Note that an \code{xgb.Booster} object (\strong{as produced by \code{\link[=xgb.train]{xgb.train()}}}, see rest of the doc
13 for objects produced by \code{\link[=xgboost]{xgboost()}}), outside of its core components, might also keep:
14 \itemize{
15 \item Additional model configuration (accessible through \code{\link[=xgb.config]{xgb.config()}}), which includes
16 model fitting parameters like \code{max_depth} and runtime parameters like \code{nthread}.
17 These are not necessarily useful for prediction/importance/plotting.
18 \item Additional R specific attributes - e.g. results of callbacks, such as evaluation logs,
19 which are kept as a \code{data.table} object, accessible through
20 \code{attributes(model)$evaluation_log} if present.
21 }
22
23 The first one (configurations) does not have the same compatibility guarantees as
24 the model itself, including attributes that are set and accessed through
25 \code{\link[=xgb.attributes]{xgb.attributes()}} - that is, such configuration might be lost after loading the
26 booster in a different XGBoost version, regardless of the serializer that was used.
27 These are saved when using \code{\link[=saveRDS]{saveRDS()}}, but will be discarded if loaded into an
28 incompatible XGBoost version. They are not saved when using XGBoost's
29 serializers from its public interface including \code{\link[=xgb.save]{xgb.save()}} and \code{\link[=xgb.save.raw]{xgb.save.raw()}}.
30
31 The second ones (R attributes) are not part of the standard XGBoost model structure,
32 and thus are not saved when using XGBoost's own serializers. These attributes are
33 only used for informational purposes, such as keeping track of evaluation metrics as
34 the model was fit, or saving the R call that produced the model, but are otherwise
35 not used for prediction / importance / plotting / etc.
36 These R attributes are only preserved when using R's serializers.
37
38 In addition to the regular \code{xgb.Booster} objects produced by \code{\link[=xgb.train]{xgb.train()}}, the
39 function \code{\link[=xgboost]{xgboost()}} produces objects with a different subclass \code{xgboost} (which
40 inherits from \code{xgb.Booster}), which keeps other additional metadata as R attributes
41 such as class names in classification problems, and which has a dedicated \code{predict}
42 method that uses different defaults and takes different argument names. XGBoost's
43 own serializers can work with this \code{xgboost} class, but as they do not keep R
44 attributes, the resulting object, when deserialized, is downcasted to the regular
45 \code{xgb.Booster} class (i.e. it loses the metadata, and the resulting object will use
46 \code{\link[=predict.xgb.Booster]{predict.xgb.Booster()}} instead of \code{\link[=predict.xgboost]{predict.xgboost()}}) - for these \code{xgboost} objects,
47 \code{saveRDS} might thus be a better option if the extra functionalities are needed.
48
49 Note that XGBoost models in R starting from version \verb{2.1.0} and onwards, and
50 XGBoost models before version \verb{2.1.0}; have a very different R object structure and
51 are incompatible with each other. Hence, models that were saved with R serializers
52 like \code{\link[=saveRDS]{saveRDS()}} or \code{\link[=save]{save()}} before version \verb{2.1.0} will not work with latter
53 \code{xgboost} versions and vice versa. Be aware that the structure of R model objects
54 could in theory change again in the future, so XGBoost's serializers should be
55 preferred for long-term storage.
56
57 Furthermore, note that model objects from XGBoost might not be serializable with third-party
58 R packages like \code{qs} or \code{qs2}.
59 }
60 \details{
61 Use \code{\link[=xgb.save]{xgb.save()}} to save the XGBoost model as a stand-alone file. You may opt into
62 the JSON format by specifying the JSON extension. To read the model back, use
63 \code{\link[=xgb.load]{xgb.load()}}.
64
65 Use \code{\link[=xgb.save.raw]{xgb.save.raw()}} to save the XGBoost model as a sequence (vector) of raw bytes
66 in a future-proof manner. Future releases of XGBoost will be able to read the raw bytes and
67 re-construct the corresponding model. To read the model back, use \code{\link[=xgb.load.raw]{xgb.load.raw()}}.
68 The \code{\link[=xgb.save.raw]{xgb.save.raw()}} function is useful if you would like to persist the XGBoost model
69 as part of another R object.
70
71 Use \code{\link[=saveRDS]{saveRDS()}} if you require the R-specific attributes that a booster might have, such
72 as evaluation logs or the model class \code{xgboost} instead of \code{xgb.Booster}, but note that
73 future compatibility of such objects is outside XGBoost's control as it relies on R's
74 serialization format (see e.g. the details section in \link{serialize} and \code{\link[=save]{save()}} from base R).
75
76 For more details and explanation about model persistence and archival, consult the page
77 \url{https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html}.
78 }
79 \examples{
80 \dontshow{RhpcBLASctl::omp_set_num_threads(1)}
81 data(agaricus.train, package = "xgboost")
82
83 bst <- xgb.train(
84 data = xgb.DMatrix(agaricus.train$data, label = agaricus.train$label, nthread = 1),
85 nrounds = 2,
86 params = xgb.params(
87 max_depth = 2,
88 nthread = 2,
89 objective = "binary:logistic"
90 )
91 )
92
93 # Save as a stand-alone file; load it with xgb.load()
94 fname <- file.path(tempdir(), "xgb_model.ubj")
95 xgb.save(bst, fname)
96 bst2 <- xgb.load(fname)
97
98 # Save as a stand-alone file (JSON); load it with xgb.load()
99 fname <- file.path(tempdir(), "xgb_model.json")
100 xgb.save(bst, fname)
101 bst2 <- xgb.load(fname)
102
103 # Save as a raw byte vector; load it with xgb.load.raw()
104 xgb_bytes <- xgb.save.raw(bst)
105 bst2 <- xgb.load.raw(xgb_bytes)
106
107 # Persist XGBoost model as part of another R object
108 obj <- list(xgb_model_bytes = xgb.save.raw(bst), description = "My first XGBoost model")
109 # Persist the R object. Here, saveRDS() is okay, since it doesn't persist
110 # xgb.Booster directly. What's being persisted is the future-proof byte representation
111 # as given by xgb.save.raw().
112 fname <- file.path(tempdir(), "my_object.Rds")
113 saveRDS(obj, fname)
114 # Read back the R object
115 obj2 <- readRDS(fname)
116 # Re-construct xgb.Booster object from the bytes
117 bst2 <- xgb.load.raw(obj2$xgb_model_bytes)
118
119 }