Closed ZekeMarshall closed 2 weeks ago
Hi @ZekeMarshall, happy to help you out. What you posted is basically a "usage error", but some speed optimizations in the recent mlr3 have made these errors a bit more likely.... so we might go back to a more robust setting after this issue.
1) You didn't really ask about it, but still: Your params / ids look a bit "ugly" because of the "svm.svm" duplication.
That happens because you use a named list here.
learners_l = list(
svm = lrn("classif.svm", id = "svm"),
gam = lrn("classif.gam", id = "gam")
)
This list is passed to "gunion" to join the graphs (here: just 2 pipeops). If you name the list, the names are "prefixed" to the nodes of the graphs you join -- to avoid name clashes. But.... Your names are unique already. Use an unnamed list for simpler / nicer IDs.
a) the e1071 svm is slightly annoying, it has multiple "classification" modes. You need to set "type = "C-classification". For various reasons, this is not a default in mlr3; if you don't set it, the learner errors, with an informative error message, that you need to set it --- so far, so simple. So the error you get at the end of the autotuner is actually "good" -- and easily fixable.
(for completeness sake: you can also set "nu-classification", that's actually why the "type" param exists in e1071::svm....)
library(mlr3learners)
task = tsk("sonar")
learner = lrn("classif.svm", type = "C-classification", id = "svm")
# with search space
search_space = ps(
cost = p_dbl(1e-4, 1e4, logscale = TRUE),
kernel = p_fct(levels = c("polynomial", "radial", "sigmoid", "linear")),
degree = p_int(lower = 2, upper = 5, depends = kernel == "polynomial"),
gamma = p_dbl(1e-4, 1e4, logscale = TRUE)
)
tune(
tuner = tnr("grid_search"),
task = task,
learner = learner,
resampling = rsmp("holdout"),
measure = msr("classif.ce"),
search_space = search_space
term_evals = 2,
store_models = TRUE,
check_values = TRUE)
# with tune token
learner = lrn("classif.svm", type = "C-classification",
cost = to_tune(1e-4, 1e4, logscale = TRUE),
kernel = to_tune(levels = c("polynomial", "radial", "sigmoid", "linear")),
degree = to_tune(lower = 2, upper = 5),
gamma = to_tune(1e-4, 1e4, logscale = TRUE)
)
tune(
tuner = tnr("grid_search"),
task = task,
learner = learner,
resampling = rsmp("holdout"),
measure = msr("classif.ce"),
term_evals = 2,
store_models = TRUE,
check_values = TRUE)
b) in a certain sense, the real issue is, that your tuning code DOES NOT error. this happens because we removed a "safety check" here for speed reasons. I guess we will reactivate this now.
Have a look at the code that @be-marc posted. It contains multiple hints.
First of all, "check_values = TRUE" activates the safety check. You want that. At least for a "first try run" (and apparently our new defaults partially "sucks" here).
Second, Marc shows you how to set all required dependencies in the search space. If (!) you construct the search space yourself, you are responsible for "marking" subordinate params like this. Constructing search spaces yourself, fully, is the more flexible way. You can do this, I often do this myself, but there is also a "lazier" way.
Third: You can use the "tune tokens" in the learner, to implicitly create the search space in the learner. If (!) you do THAT, you can leave out certain information. That type of information which is already present in the "param_set" of the learners. That includes dependencies.
Does this help?
Hi @berndbischl and @be-marc , thank you very much for your explanations and advice! Yes that has answered my query exactly, I've adapted @be-marc's examples and implemented them in my workflow and everything is working perfectly. Thanks again!
Hi,
Thank you very much for creating such an incredible ecosystem of packages!
I have encountered an issue when training a
mlr3tuning::AutoTuner
object which contains aclassif.svm
learner, either individually, or in an ensemble. Specifically, training theAutoTuner
object fails to save the tuned model hyperparameters (and therefore the model) when the selected kernel does not include certain hyperparameters. I have used the default search space for svm beginning on line 142 in the mlr3tuningspaces package See a minimal reproducible example below:First, when just using the
tune
function to obtain the tuned hyperparameters the search succeeds, producing the following object:Second, when training the
at
object for the first time I receive an error message similar to this issue https://github.com/mlr-org/mlr3learners/issues/208 , i.e.Lastly, when training the
at
object a second time I receive the following messageIn this case the selected kernel is "linear" which does not require the gamma and degree hyperparameters,
Any help would be greatly appreciated!
Best regards,
Zeke
Session Information