File: test_base_hyperpars.R

package info (click to toggle)
r-cran-mlr 2.19.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,392 kB
  • sloc: ansic: 65; sh: 13; makefile: 5
file content (133 lines) | stat: -rwxr-xr-x 5,440 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

test_that("hyperpars", {

  # RWeka not avail
  skip_on_cran()
  skip_on_os("windows")

  lrn = makeLearner("classif.rpart", minsplit = 10)
  expect_equal(getHyperPars(lrn), list(xval = 0, minsplit = 10))

  m = train(lrn, task = multiclass.task)
  expect_true(!inherits(m, "FailureModel"))
  expect_equal(getHyperPars(m$learner), list(xval = 0, minsplit = 10))

  # test equality after removing using removeHyperPars
  lrn = makeLearner("classif.J48", C = 0.5)
  expect_identical(getHyperPars(makeLearner("classif.J48")),
    getHyperPars(removeHyperPars(lrn, "C")))

  # test a more complex param object
  lrn = makeLearner("classif.ksvm", class.weights = c(setosa = 1,
    versicolor = 2, virginica = 3))
  m = train(lrn, task = multiclass.task)

  # check warnings
  mlr.opts = getMlrOptions()
  configureMlr(on.par.without.desc = "warn", show.learner.output = FALSE)
  expect_warning(makeLearner("classif.rpart", foo = 1), "Setting parameter foo without")

  configureMlr(on.par.without.desc = "quiet")
  expect_warning(makeLearner("classif.rpart", foo = 1), NA)
  configureMlr(show.learner.output = FALSE)
  do.call(configureMlr, mlr.opts)
})


test_that("removing par settings works", {
  lrn = makeLearner("classif.qda")
  expect_error(removeHyperPars(lrn, "minsplit"), "Trying to remove")
  expect_error(removeHyperPars(lrn, "xxx"), "Trying to remove")
  lrn2 = setHyperPars(lrn, method = "mve", nu = 7)
  lrn3 = removeHyperPars(lrn2, "method")
  expect_equal(getHyperPars(lrn3), list(nu = 7))

  # now with wrapper
  lrn = makeBaggingWrapper(makeLearner("classif.qda"))
  lrn2 = setHyperPars(lrn, method = "mve", bw.iters = 9)
  lrn3 = removeHyperPars(lrn2, "method")
  expect_equal(getHyperPars(lrn3), list(bw.iters = 9))
  lrn3 = removeHyperPars(lrn2, "bw.iters")
  expect_equal(getHyperPars(lrn3), list(method = "mve"))

  # now remove all hyperpars using a wrapped wrapper
  lrn = makeOversampleWrapper(makeFilterWrapper(makeLearner("classif.qda", nu = 2),
    fw.perc = 0.5), osw.rate = 1)
  lrn1 = removeHyperPars(lrn, ids = names(getHyperPars(lrn)))
  expect_true(length(getHyperPars(lrn1)) == 0)
})

test_that("setting 'when' works for hyperpars", {
  lrn = makeLearner("regr.__mlrmocklearners__4", p1 = 1, p2 = 2, p3 = 3)
  hps = getHyperPars(lrn)
  expect_equal(hps, list(p1 = 1, p2 = 2, p3 = 3))
  # model stores p1 + p3 in fit, adds p2,p3 in predict to this (so it predicts constant val)
  m = train(lrn, regr.task)
  expect_equal(m$learner.model, list(foo = 1 + 3))
  p = predict(m, regr.task)
  expect_equal(p$data$response, rep(1 + 2 + 2 * 3, getTaskSize(regr.task)))
})

test_that("fuzzy matching works for mistyped hyperpars", {
  msg = "classif.ksvm: Setting parameter sigm without available description object!\nDid you mean one of these hyperparameters instead: sigma fit type\nYou can switch off this check by using configureMlr!"
  mlr.opts = getMlrOptions()

  # test if config arg works properly in combination with show.info
  cq = list(on.par.without.desc = "quiet")
  cw = list(on.par.without.desc = "warn")
  cs = list(on.par.without.desc = "stop")
  # never print message when quiet
  expect_silent(makeLearner("classif.ksvm", config = cq, sigm = 1))
  configureMlr(on.par.without.desc = "quiet")
  expect_silent(makeLearner("classif.ksvm", sigm = 1))

  # print message and warn
  expect_warning(makeLearner("classif.ksvm", config = cw, sigm = 1), msg)
  configureMlr(on.par.without.desc = "warn")
  expect_warning(makeLearner("classif.ksvm", sigm = 1), msg)

  # print message and error
  expect_error(makeLearner("classif.ksvm", config = cs, sigm = 1), msg)
  configureMlr(on.par.without.desc = "stop")
  expect_error(makeLearner("classif.ksvm", sigm = 1), msg)

  # docu says: for warn and quiet parameter is passed, check if this is true
  lrn = makeLearner("classif.ksvm",
    config = list(on.par.without.desc = "quiet"))
  expect_equal(getHyperPars(setHyperPars(lrn, sigm = 1))$sigm, 1)
  lrn = makeLearner("classif.ksvm",
    config = list(on.par.without.desc = "warn"))
  expect_warning(expect_equal(getHyperPars(setHyperPars(lrn, sigm = 1))$sigm, 1))

  do.call(configureMlr, mlr.opts)
})

test_that("options are respected", {
  # with local option

  lrn = makeLearner("classif.__mlrmocklearners__2")
  expect_error(setHyperPars(lrn, beta = 1), "available description object")
  lrn = makeLearner("classif.__mlrmocklearners__2", config = list(on.par.without.desc = "warn"))
  expect_warning(setHyperPars(lrn, beta = 1), "available description object")
  lrn = makeLearner("classif.__mlrmocklearners__2", config = list(on.par.without.desc = "quiet"))
  expect_s3_class(setHyperPars(lrn, beta = 1), "Learner")

  lrn = makeLearner("classif.__mlrmocklearners__2")
  expect_error(setHyperPars(lrn, alpha = 2), "feasible")
  lrn = makeLearner("classif.__mlrmocklearners__2", config = list(on.par.out.of.bounds = "warn"))
  expect_warning(setHyperPars(lrn, alpha = 2), "feasible")
  lrn = makeLearner("classif.__mlrmocklearners__2", config = list(on.par.out.of.bounds = "quiet"))
  expect_s3_class(setHyperPars(lrn, alpha = 2), "Learner")


  # with global option
  mlr.opts = getMlrOptions()

  lrn = makeLearner("classif.__mlrmocklearners__2")
  configureMlr(on.par.without.desc = "quiet")
  expect_s3_class(setHyperPars(lrn, beta = 1), "Learner")
  configureMlr(on.par.out.of.bounds = "quiet")
  expect_s3_class(setHyperPars(lrn, alpha = 2), "Learner")

  do.call(configureMlr, mlr.opts)
})