Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ S3method(bundle,H2OBinomialModel)
S3method(bundle,H2OMultinomialModel)
S3method(bundle,H2ORegressionModel)
S3method(bundle,bart)
S3method(bundle,catboost.Model)
S3method(bundle,default)
S3method(bundle,keras.engine.training.Model)
S3method(bundle,luz_module_fitted)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# bundle (development version)

* Added bundle method for catboost models and, by extension, `parsnip::boost_tree(engine = "catboost")` via bonsai (#82).

# bundle 0.1.3

* Updated to support new versions of xgboost models (#75).
Expand Down
73 changes: 73 additions & 0 deletions R/bundle_catboost.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#' @templateVar class a `catboost.Model`
#' @template title_desc
#'
#' @templateVar outclass `bundled_catboost.Model`
#' @templateVar default .
#' @template return_bundle
#' @family bundlers
#'
#' @param x A `catboost.Model` object returned from `catboost::catboost.train()`.
#' @template param_unused_dots
#' @rdname bundle_catboost
#' @seealso This method stores the raw serialized model bytes from the
#' catboost model object and restores the C++ handle on unbundle.
#' @template butcher_details
#' @examplesIf rlang::is_installed(c("catboost", "parsnip", "bonsai"))
#' # fit model and bundle ------------------------------------------------
#' library(parsnip)
#' library(bonsai)
#'
#' set.seed(1)
#'
#' mod <- boost_tree(trees = 10) %>%
#' set_engine("catboost", verbose = 0) %>%
#' set_mode("classification") %>%
#'
#' fit(Species ~ ., data = iris)
#' # extract the underlying catboost model
#' catboost_model <- mod$fit
#'
#' model_bundle <- bundle(catboost_model)
#'
#' # then, after saveRDS + readRDS or passing to a new session ----------
#' model_unbundled <- unbundle(model_bundle)
#' @aliases bundle.catboost.Model
#' @method bundle catboost.Model
#' @export
bundle.catboost.Model <- function(x, ...) {
rlang::check_installed("catboost")
rlang::check_dots_empty()

# Save model to temp file and read raw bytes
tmp_file <- withr::local_tempfile(fileext = ".cbm")
rlang::eval_tidy(rlang::call2(
"catboost.save_model",
x,
tmp_file,
.ns = "catboost"
))
object <- readBin(tmp_file, "raw", file.size(tmp_file))

bundle_constr(
object = object,
situate = situate_constr(function(object) {
# Write raw bytes to temp file and load model
tmp_file <- withr::local_tempfile(fileext = ".cbm")
writeBin(object, tmp_file)
model <- rlang::eval_tidy(rlang::call2(
"catboost.load_model",
tmp_file,
.ns = "catboost"
))

# Restore metadata
model$feature_importances <- !!x$feature_importances
model$tree_count <- !!x$tree_count
model$learning_rate <- !!x$learning_rate
model$feature_count <- !!x$feature_count

model
}),
desc_class = class(x)[1]
)
}
1 change: 1 addition & 0 deletions man/bundle.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/bundle_bart.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/bundle_caret.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

117 changes: 117 additions & 0 deletions man/bundle_catboost.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/bundle_embed.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/bundle_h2o.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/bundle_keras.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/bundle_parsnip.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/bundle_recipe.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/bundle_stacks.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/bundle_torch.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/bundle_workflows.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/bundle_xgboost.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading