mlr-org / mlr3tuning

Hyperparameter optimization package of the mlr3 ecosystem
https://mlr3tuning.mlr-org.com/
GNU Lesser General Public License v3.0
53 stars 5 forks source link

Changing predict type after tuning, bug and enhancement #276

Closed RaphaelS1 closed 3 years ago

RaphaelS1 commented 3 years ago

I've found (thanks to @a-hanf) two separate problems occurring if the predict type of a learner is changed after tuning (which is a common enough use-case). In the reprex below setting model$predict_type has no effect but setting model$model$learner$predict_type does have an effect even though it claims to error #> Error: Field/Binding is read-only. So there is a bug as it actually has an effect when it shouldn't, but simultaneously this should be allowed.

My suggestion is to allow users to set model$predict_type, which internally updates model$model$learner$predict_type.

library(paradox)
library(mlr3)
library(mlr3tuning)

set.seed(1)

search_space = ParamSet$new(list(
  ParamInt$new("minsplit", lower = 1, upper = 2)
))

model = AutoTuner$new(
  lrn("classif.rpart", predict_type = "response"),
  rsmp("holdout"),
  msr("classif.acc"),
  search_space,
  trm("evals", n_evals = 1),
  tnr("random_search")
)

model$train(tsk("iris"))

# does nothing
model$predict_type = "prob"
model$predict(tsk("iris"))
#> <PredictionClassif> for 150 observations:
#>     row_id     truth  response
#>          1    setosa    setosa
#>          2    setosa    setosa
#>          3    setosa    setosa
#> ---                           
#>        148 virginica virginica
#>        149 virginica virginica
#>        150 virginica virginica

# does something that it technically shouldn't be allowed to (but is practically a good idea)
model$model$learner$predict_type = "prob"
#> Error: Field/Binding is read-only
model$predict(tsk("iris"))
#> <PredictionClassif> for 150 observations:
#>     row_id     truth  response prob.setosa prob.versicolor prob.virginica
#>          1    setosa    setosa           1      0.00000000      0.0000000
#>          2    setosa    setosa           1      0.00000000      0.0000000
#>          3    setosa    setosa           1      0.00000000      0.0000000
#> ---                                                                      
#>        148 virginica virginica           0      0.02173913      0.9782609
#>        149 virginica virginica           0      0.02173913      0.9782609
#>        150 virginica virginica           0      0.02173913      0.9782609

Created on 2020-10-23 by the reprex package (v0.3.0)

EDIT:

Along the same lines just found another bug:

library(paradox)
library(mlr3)
library(mlr3tuning)

model = AutoTuner$new(
  lrn("classif.rpart", predict_type = "response"),
  rsmp("holdout"),
  msr("classif.acc"),
  ParamSet$new(list(ParamInt$new("minsplit", lower = 1, upper = 2))),
  trm("evals", n_evals = 1),
  tnr("random_search")
)

model$learner$predict_type = "prob"
#> Error in (function () : unused argument (base::quote(<environment>))
model$predict_type
#> [1] "response"
model$learner$predict_type
#> [1] "prob"

Created on 2020-10-23 by the reprex package (v0.3.0)

be-marc commented 3 years ago

Fixed by https://github.com/mlr-org/mlr3tuning/commit/49e4d7179d7241405adeb7c3444cbb53f06cf383