-
Notifications
You must be signed in to change notification settings - Fork 3
/
compboost_vs_mboost.R
167 lines (126 loc) · 4.02 KB
/
compboost_vs_mboost.R
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# ============================================================================ #
# compboost vs mboost #
# ============================================================================ #
# Just a small comparison, maybe more a check if compboost does the same
# as mboost using mtcars.
# Prepare Data:
# -------------
df = mtcars
# Create new variable to check the polynomial baselearner with degree 2:
df$hp2 = df[["hp"]]^2
# Data for compboost:
X.hp = as.matrix(df[["hp"]], ncol = 1)
X.wt = as.matrix(df[["wt"]], ncol = 1)
y = df[["mpg"]]
# Hyperparameter for the algorithm:
learning.rate = 0.05
iter.max = 500
# Prepare compboost:
# ------------------
# Create new linear baselearner of hp and wt:
linear.factory.hp = PolynomialFactory$new(X.hp, "hp", 1)
linear.factory.wt = PolynomialFactory$new(X.wt, "wt", 1)
# Create new quadratic baselearner of hp:
quadratic.factory.hp = PolynomialFactory$new(X.hp, "hp", 2)
# Create new factory list:
factory.list = FactoryList$new()
# Register factorys:
factory.list$registerFactory(linear.factory.hp)
factory.list$registerFactory(linear.factory.wt)
factory.list$registerFactory(quadratic.factory.hp)
factory.list$printRegisteredFactorys()
# We use quadratic loss:
loss = QuadraticLoss$new()
# Use the greedy optimizer:
optimizer = GreedyOptimizer$new()
# Define logger. We want just the iterations as stopper but also track the
# time:
log.iterations = LogIterations$new(TRUE, iter.max)
log.time = LogTime$new(FALSE, 500, "microseconds")
logger.list = LoggerList$new()
logger.list$registerLogger(log.iterations)
logger.list$registerLogger(log.time)
# Run compboost:
# --------------
# Initialize object:
cboost = Compboost$new(
response = y,
learning_rate = learning.rate,
stop_if_all_stopper_fulfilled = TRUE,
factory_list = factory.list,
loss = loss,
logger_list = logger.list,
optimizer = optimizer
)
# Train the model (here we don't need the trace):
cboost$train(FALSE)
# Get vector selected baselearner:
cboost$getSelectedBaselearner()
# Get logger data:
cboost$getLoggerData()
# Get parameter estimator:
# Do the same with mboost:
# ------------------------
library(mboost)
mod = mboost(
formula = mpg ~ bols(hp, intercept = FALSE) +
bols(wt, intercept = FALSE) +
bols(hp2, intercept = FALSE),
data = df,
control = boost_control(mstop = iter.max, nu = learning.rate)
)
# Check if the selected baselearner are the same:
# -----------------------------------------------
cboost.xselect = match(
x = cboost$getSelectedBaselearner(),
table = c(
"hp: polynomial with degree 1",
"wt: polynomial with degree 1",
"hp: polynomial with degree 2"
)
)
all.equal(mod$xselect(), cboost.xselect)
# Check if the prediction is the same:
# ------------------------------------
all.equal(predict(mod), cboost$getPrediction())
# Check if parameter are the same:
# --------------------------------
mod$coef()
cboost$getEstimatedParameter()
# Benchmark:
# ----------
# Time comparison:
microbenchmark::microbenchmark(
"compboost" = cboost$train(FALSE),
"mboost" = mboost(
formula = mpg ~ bols(hp, intercept = FALSE) +
bols(wt, intercept = FALSE) +
bols(hp2, intercept = FALSE),
data = df,
control = boost_control(mstop = iter.max, nu = learning.rate)
),
times = 10L
)
# Profiling to compare used memory:
p = profvis::profvis({
cboost$train(FALSE)
mboost(
formula = mpg ~ bols(hp, intercept = FALSE) +
bols(wt, intercept = FALSE) +
bols(hp2, intercept = FALSE),
data = df,
control = boost_control(mstop = iter.max, nu = learning.rate)
)
})
print(p)
# Check if parameter of smaller iteration works:
# ----------------------------------------------
mod.reduced = mboost(
formula = mpg ~ bols(hp, intercept = FALSE) +
bols(wt, intercept = FALSE) +
bols(hp2, intercept = FALSE),
data = df,
control = boost_control(mstop = 200, nu = learning.rate)
)
mod.reduced$coef()
cboost$getEstimatedParameterOfIteration(200)