-
-
Notifications
You must be signed in to change notification settings - Fork 55
feat: New base class for python-based learners #484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
AnnaNzrv
wants to merge
8
commits into
main
Choose a base branch
from
py-base-learner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9ffcbc0
zwischenstand
AnnaNzrv 3194661
base-class-py
AnnaNzrv c1d04b0
check marshalling
AnnaNzrv 28248d4
docs
AnnaNzrv 41f4a61
Merge branch 'main' into py-base-learner
AnnaNzrv 7b40dd6
make marshalling work for py learners
AnnaNzrv b4207d1
refactor python base learner
AnnaNzrv 1b5a0cd
feat: paramspace - configspace conversion
AnnaNzrv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
LearnerPythonClassif = R6::R6Class( | ||
"LearnerPythonClassif", | ||
inherit = mlr3::LearnerClassif, | ||
|
||
public = list( | ||
initialize = function(id, | ||
feature_types = c("logical","integer","numeric","factor","ordered"), | ||
predict_types = c("response", "prob"), | ||
param_set = ps(), | ||
properties = character(), | ||
packages = "reticulate", | ||
label = NA_character_, | ||
man = NA_character_, | ||
# Python requirements | ||
py_packages, | ||
python_version) { | ||
|
||
super$initialize( | ||
id = id, | ||
feature_types = feature_types, | ||
predict_types = predict_types, | ||
param_set = param_set, | ||
properties = properties, | ||
packages = union(c("mlr3", "mlr3extralearners"), packages), | ||
label = label, | ||
man = man | ||
) | ||
private$.py_packages = py_packages | ||
private$.py_version = python_version | ||
}, | ||
|
||
|
||
py_requirements = function(rhs) { | ||
assert_ro_binding(rhs) | ||
list( | ||
packages = private$.py_packages, | ||
python_version = private$.py_version | ||
) | ||
} | ||
), | ||
|
||
private = list( | ||
.py_packages = NULL, .py_version = NULL, | ||
|
||
.train = function(task) { | ||
py_requirements = self$py_requirements() | ||
do.call(assert_python_packages, py_requirements) | ||
|
||
out = named_list() | ||
|
||
fit = private$.train_py(task = task) | ||
assert_list(fit, all.missing = FALSE, min.len = 1) | ||
assert_names(names(fit), must.include = "model") | ||
|
||
structure( | ||
mlr3misc::insert_named(out, fit), | ||
class = c("pybytes_model", paste0(self$id, "_model")) | ||
) | ||
}, | ||
|
||
.predict = function(task) { | ||
py_requirements = self$py_requirements() | ||
do.call(assert_python_packages, py_requirements) | ||
|
||
newdata = ordered_features(task, self) | ||
preds = private$.predict_py(task = task, newdata = newdata, predict_type = self$predict_type) | ||
preds | ||
}, | ||
|
||
# ---- subclass hooks ---- | ||
.train_py = function(x, y, pars, task, ...) { | ||
stop("Subclass must implement .fit_py(x, y, pars, task, ...).") | ||
}, | ||
|
||
.predict_py = function(model, newdata, predict_types, meta) { | ||
stop("Subclass must implement .predict_py(model, newdata, predict_types, meta).") | ||
} | ||
) | ||
) | ||
|
||
# ---- Generic bytes-only marshaling for all python learners ---- | ||
|
||
#' @export | ||
marshal_model.pybytes_model <- function(model, inplace = FALSE, ...) { | ||
reticulate::py_require(model$py_modules, python_version = model$py_version) | ||
pickle = reticulate::import("pickle") | ||
|
||
raw = as.raw(pickle$dumps(model$model)) | ||
|
||
learner_class = setdiff(class(model), "pybytes_model") | ||
if (length(learner_class) > 1L) { | ||
stop(sprintf( | ||
"Expected at most one learner-specific class; got: %s", | ||
paste(learner_class, collapse = ", ") | ||
)) | ||
} | ||
|
||
raw_model = list( | ||
marshaled = raw, | ||
learner_class = learner_class, | ||
py_modules = model$py_modules, | ||
py_version = model$py_version | ||
) | ||
meta_data = setdiff(model, "model") | ||
|
||
structure( | ||
mlr3misc::insert_named(raw_model, meta_data), | ||
class = c("pybytes_model_marshaled", "marshaled") | ||
) | ||
} | ||
|
||
#' @export | ||
unmarshal_model.pybytes_model_marshaled <- function(model, inplace = FALSE, ...) { | ||
# use python requirements stored in the marshaled object | ||
reticulate::py_require(model$py_modules, python_version = model$py_version) | ||
pickle <- reticulate::import("pickle") | ||
fitted <- pickle$loads(reticulate::r_to_py(model$marshaled)) | ||
|
||
classes <- if (is.null(model$learner_class)) { | ||
"pybytes_model" | ||
} else { | ||
c("pybytes_model", model$learner_class) | ||
} | ||
meta_data = setdiff(model, "marshaled") | ||
out = list( | ||
model = fitted, | ||
learner_class = classes, | ||
py_modules = model$py_modules, | ||
py_version = model$py_version | ||
) | ||
|
||
structure( | ||
mlr3misc::insert_named(out, meta_data), | ||
class = classes | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.