-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmethod_hierarchy_liblinear.py
268 lines (164 loc) · 7.02 KB
/
method_hierarchy_liblinear.py
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python2.5
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Written (W) 2009-2010 Christian Widmer
# Copyright (C) 2009-2010 Max-Planck-Society
"""
Created on 23.03.2009
@author: Christian Widmer
@summary: Hierarchical SVM-based multitask method using liblinear
"""
import shogun_factory_new as shogun_factory
from base_method import MultiMethod
class Method(MultiMethod):
"""
Hierarchical Multitask Method based on the SVM
"""
def _train(self, train_data, param):
"""
training procedure using training examples and labels
@param train_data: Data relevant to SVM training
@type train_data: dict<str, list<instances> >
@param param: Parameters for the training procedure
@type param: ParameterSvm
"""
for task_id in train_data.keys():
print "task_id:", task_id
root = param.taxonomy.data
grey_nodes = [root]
#####################################################
# top-down processing of taxonomy
#####################################################
while len(grey_nodes)>0:
node = grey_nodes.pop(0) # pop first item
# enqueue children
if node.children != None:
grey_nodes.extend(node.children)
# get data below current node
data = [train_data[key] for key in node.get_data_keys()]
#print "data at current level"
#for instance_set in data:
# print instance_set[0].dataset
# initialize containers
examples = []
labels = []
# concatenate data at level
for instance_set in data:
#print "train split_set:", instance_set[0].dataset.organism
for inst in instance_set:
examples.append(inst.example)
labels.append(inst.label)
print "done concatenating data"
#####################################################
# train predictors
#####################################################
# set up presvm
if node.is_root():
# no parent at root node
parent_svm = None
weight = 0
else:
# regularize against parent predictor
parent_svm = node.parent.predictor
weight = param.transform
print "current edge_weight:", weight, " ,name:", node.name
# create SVM object
svm = shogun_factory.create_initialized_domain_adaptation_svm(param, examples, labels, parent_svm, weight)
svm.set_train_factor(param.flags["train_factor"])
#print "============================================="
#print "WARNING: MAX TRAIN TIME SET TO 1!!!!!!!!!!!!!!!!"
#print "============================================="
#svm.set_max_train_time(0)
#svm.set_max_iterations(1)
print "invoking shogun training procedure"
# invoke training procedure
svm.train()
# attach svm to current node
node.predictor = svm
# save some information
# TODO refactor
#self.additional_information[node.name + " svm obj"] = svm.get_objective()
#self.additional_information[node.name + " svm num sv"] = svm.get_num_support_vectors()
#self.additional_information[node.name + " runtime"] = svm.get_runtime()
#####################################################
# Wrap things up
#####################################################
# wrap up predictors for later use
predictors = {}
for leaf in root.get_leaves():
predictors[leaf.name] = leaf.predictor
assert(leaf.predictor!=None)
# make sure we have the same keys (potentially in a different order)
sym_diff_keys = set(train_data.keys()).symmetric_difference(set(predictors.keys()))
assert len(sym_diff_keys)==0, "symmetric difference between keys non-empty: " + str(sym_diff_keys)
# clear tree to avoid having __del__ attributes in cyclic-reference graph
# otherwise, this will not be cleaned up by garbage collector
root.clear_predictors()
return predictors
def _predict(self, predictor, examples, task_id):
"""
make prediction on examples using trained predictor
@param predictor: trained predictor
@type predictor: array
@param examples: list of examples
@type examples: list
@param task_id: task identifier
@type task_id: int
@return: svm output
@rtype: list<float>
"""
#####################################################
# classification
#####################################################
svm = predictor
#shogun data
feat = shogun_factory.create_features(examples, self.param)
out = svm.classify(feat).get_labels()
# flush feats (not very elegant, but avoids having huge test sets floating around in mem)
one_feat = shogun_factory.create_features([examples[0]], self.param)
svm.classify(one_feat)
return out
def main():
print "starting debugging:"
SPLIT_POINTER = 1
from expenv import MultiSplitSet
from helper import Options
# select dataset
multi_split_set = MultiSplitSet.get(434)
# flags
flags = {}
flags["normalize_cost"] = False
flags["epsilon"] = 1.0
#0.005
flags["kernel_cache"] = 200
flags["use_bias"] = False
# arts params
flags["svm_type"] = "liblineardual"
flags["degree"] = 24
flags["degree_spectrum"] = 4
flags["shifts"] = 0 #32
flags["center_offset"] = 70
flags["train_factor"] = 1
#create mock param object by freezable struct
param = Options()
param.kernel = "Promoter"
param.cost = 1.0
param.transform = 1.0
param.id = 666
param.flags = flags
param.taxonomy = multi_split_set.taxonomy
param.freeze()
data_train = multi_split_set.get_train_data(SPLIT_POINTER)
data_eval = multi_split_set.get_eval_data(SPLIT_POINTER)
# train
mymethod = Method(param)
mymethod.train(data_train)
print "training done"
assessment = mymethod.evaluate(data_eval)
print assessment
assessment.destroySelf()
if __name__ == "__main__":
main()