-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvic.R
204 lines (177 loc) · 7.78 KB
/
vic.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
source("init.R")
library(batchtools)
library(ggplot2)
library(tidyr)
library(data.table)
# writeable = TRUE only once !!!!
regr = batchtools::loadRegistry("XXX", writeable = TRUE)
# Define Cluster-Configurations
regr$cluster.functions = makeClusterFunctionsSocket(ncpus = 12)
# Define Problem
addProblem("fromlist", fun = function(data, job, taskname) {
task = list.tasks[[taskname]]
# Fix logical features (for FeatureImp)
if(taskname == "bs"){
task_data = as.data.frame(task$data())
task_id = task$id
task_target = task$target_names
for(i in seq_along(task_data)) {
if (is.logical(task_data[[i]])) task_data[[i]] = as.factor(task_data[[i]])
}
task = as_task_regr(task_data, target = task_target, id = task_id)
}
# Return of the validation split
generateCanonicalDataSplits(task, ratio = 2 / 3, seed = 1)$validation
})
# Define Algorithm for VIC calculation based on PFI
addAlgorithm("calculate_vic_pfi", fun = function(data, instance, job, learnername, model.no) {
# browser()
name = sprintf("/media/external/rashomon/datafiles/%s/%s/samplemodel_%s_%s_%04d.rds",
job$pars$prob.pars$taskname, learnername, learnername, job$pars$prob.pars$taskname, model.no)
model = readRDS(name)
# Fix models in case of task bs (logical features)
if(job$pars$prob.pars$taskname == "bs"){
# fix model
holiday.special = ppl("convert_types", "factor", "logical", selector_name(c("holiday", "working_day")), id = "holiday.special")
invisible(holiday.special$train(instance)) # list.tasks$bs))
xstate = model$state
gr = holiday.special$clone(deep = TRUE) %>>% model$clone(deep = TRUE)
lr = as_learner(gr$clone(deep = TRUE))
lr$state = xstate
lr$state$train_task = instance$clone(deep = TRUE)$filter(0)
lr$model = gr$state
lr$model[[gr$ids()[[2]]]] = xstate
model = lr
rm(gr, lr, xstate, holiday.special)
}
# Function calculating PFI
calculate_pfi = function(task, model, seed, perm.reps){
set.seed(seed)
X = task$data(cols = task$feature_names)
if(model$task_type == "regr"){
y = task$data(cols = task$target_names)
predictor = Predictor$new(model = model, data = X, y = y)
# PFI via ratio (default). Alternative: compare = "difference"
FeatureImp$new(predictor, loss = "rmse", compare = "difference",
n.repetitions = perm.reps)
} else if (model$task_type == "classif") { # only binary targets
y = task$data(cols = task$target_names)[[1]]
y = ifelse(y == task$positive, 1, 0)
# Define a custom prediction function that returns class labels (not probabilities)
predict_function_class = function(model, newdata) {
model$predict_newdata(newdata)$prob[,1]
}
predictor = Predictor$new(model = model, data = X, y = y, predict.function = predict_function_class)
FeatureImp$new(predictor, loss = "mse", compare = "difference",
n.repetitions = perm.reps)
} else {
stop("Unsupported task type")
}
}
# Calculate VIC
calculate_pfi(task = instance, model = model, seed = 1, perm.reps = 10)
})
# (glmnet,tree)x(bs,gc)
run_models = readRDS("/media/external/rashomon/datafiles/model_info/run_models.rds")
# (glmnet,tree)x(cs,st)
run_models_2 = readRDS("/media/external/rashomon/datafiles/model_info/run_models_2.rds")
# (xgb) x (bs,gc, cs,st)
run_models_3 = readRDS("/media/external/rashomon/datafiles/model_info/run_models_3.rds")
# (nnet) x all
run_models_4 = readRDS("/media/external/rashomon/datafiles/model_info/run_models_nnet.rds")
# (svm) x all
run_models_5 = readRDS("/media/external/rashomon/datafiles/model_info/run_models_svm.rds")
run_models_no = data.table(sapply(run_models$torun.samples, function(x) table(x$taskname))[c("gc", "bs"), c("tree", "glmnet")], keep.rownames = TRUE)
run_models_no = merge(run_models_no, data.table(sapply(run_models_2$torun.samples, function(x) table(x$taskname)), keep.rownames = TRUE), all = TRUE)
run_models_no = merge(run_models_no, data.table(sapply(run_models_3$torun.samples, function(x) table(x$taskname)), keep.rownames = TRUE), by = "rn")
run_models_no = merge(run_models_no, data.table(sapply(run_models_4$torun.samples, function(x) table(x$taskname)), keep.rownames = TRUE), by = "rn")
run_models_no = merge(run_models_no, data.table(sapply(run_models_5$torun.samples, function(x) table(x$taskname)), keep.rownames = TRUE), by = "rn")
# change the following 2 lines
pre_design = data.table(pivot_longer(run_models_no[3,], !rn, names_to = "learnername", values_to = "count"))
design = pre_design[, .(rn = rep(rn, each = count),
learnername = rep(learnername, each = count),
model.no = sequence(count)), by = .(rn, learnername)]
design = design[,-(1:2)]
rm(run_models, run_models_2, run_models_3, run_models_no)
# save(pre_design, design, file = paste0("data/design.RData"))
# save(pre_design, design, file = paste0("data/design_", design$rn[1], ".RData"))
addExperiments(
prob.designs = list(fromlist = data.table(taskname = design$rn)),
algo.designs = list(calculate_vic_pfi = design[,-"rn"]),
repls = 1,
combine = "bind"
)
testJob(1)
submitJobs(findErrors())
submitJobs()
waitForJobs()
#### Extract results ###########################################################
# writeable = TRUE only once !!!!
regr = batchtools::loadRegistry("/media/external/XXX/bs_nnet_svm", writeable = TRUE)
# setDefaultRegistry(regr)
## save results per data set and learner
save_results = function(job_table, ids, learnername){
list.pfi_tmp = list()
# save median importance and feature from batchtools results
list.pfi_tmp[[learnername]] = reduceResultsList(ids = ids, reg = regr, fun = function(x) {
tab = x$results
subset(tab, select = c(feature, importance))
})
# merge in one data.frame per data set > VIC
vic_tmp = data.frame(feature = list.pfi_tmp[[learnername]][[1]]$feature)
for(j in 1:length(ids)){
vic_tmp = merge(vic_tmp,
list.pfi_tmp[[learnername]][[j]][,c("feature","importance")],
by = "feature")
colnames(vic_tmp)[j+1] = paste0("pfi_", learnername, "_m", ids[j])
}
res.list = list()
res.list$list.pfi = list.pfi_tmp
res.list$vic = vic_tmp
return(res.list)
}
job_table = getJobTable()
list.pfi = list()
vic = list()
names(list.pfi)
for(i in 1:length(pre_design$count)){
num = cumsum(pre_design$count)[i]
learnername = pre_design$learnername[i]
if(i == 1){
ids = job_table$job.id[1:num]
} else {
ids = job_table$job.id[(num-(pre_design$count[i]-1)):num]
}
rm(num)
ids = ids[is.na(job_table$error[job_table$job.id %in% ids])]
res = save_results(job_table, ids, learnername)
if(!(pre_design$rn[i] %in% names(list.pfi))) list.pfi[[pre_design$rn[i]]] = list()
list.pfi[[pre_design$rn[i]]][[learnername]] = res$list.pfi[[learnername]]
if(!(pre_design$rn[i] %in% names(vic))){
vic[[pre_design$rn[i]]] = res$vic
} else {
vic[[pre_design$rn[i]]] = merge(vic[[pre_design$rn[i]]],
res$vic,
by = "feature")
}
rm(res)
}
# normalize VIC: max importance = 1
vic_normalized = list()
for(i in 1:length(pre_design$count)){
vic_normalized[[pre_design$rn[i]]] = vic[[pre_design$rn[i]]]
names = names(vic[[pre_design$rn[i]]])
max_per_model = apply(vic_normalized[[pre_design$rn[i]]][,-1], 2, max)
for(j in 1:length(max_per_model)){
vic_normalized[[pre_design$rn[i]]][,j+1] = vic_normalized[[pre_design$rn[i]]][,j+1]/max_per_model[j]
}
}
if(length(unique(design$learnername)) == 1){
tmp_vic = vic
tmp_vic_norm = vic_normalized
load("data/results_vic_bs.RData")
learnername = unique(design$learnername)
vic$bs = cbind(vic$bs, tmp_vic$bs[-1])
vic_normalized$bs = cbind(vic_normalized$bs, tmp_vic_norm$bs[-1])
}
save(vic, vic_normalized, file = paste0("data/results_vic_", design$rn[1], ".RData"))