-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmethod_hierarchy_svm_new.py
225 lines (133 loc) · 6.27 KB
/
method_hierarchy_svm_new.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
#!/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 Christian Widmer
# Copyright (C) 2009 Max-Planck-Society
"""
Created on 23.03.2009
@author: Christian Widmer
@summary: Hierarchical SVM-based multitask method
"""
import shogun
from shogun.Classifier import DomainAdaptationSVM
import shogun_factory_new
from base_method import MultiMethod
debug = False
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)
#####################################################
# init data structures
#####################################################
# 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
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)
# create shogun data objects
k = shogun_factory_new.create_kernel(examples, param)
lab = shogun_factory_new.create_labels(labels)
cost = param.cost
#cost = node.cost
print "using cost:", cost
#####################################################
# train predictors
#####################################################
# init predictor variable
svm = None
# set up SVM
if node.is_root():
print "training svm at top level"
svm = SVMLight(cost, k, lab)
else:
# regularize vs parent predictor
#weight = node.edge_weight
weight = param.transform
print "current edge_weight:", weight, " ,name:", node.name
parent_svm = node.parent.predictor
svm = DomainAdaptationSVM(cost, k, lab, parent_svm, weight)
#svm.set_train_factor(param.base_similarity)
if param.flags["normalize_cost"]:
norm_c_pos = param.cost / float(len([l for l in lab.get_labels() if l==1]))
norm_c_neg = param.cost / float(len([l for l in lab.get_labels() if l==-1]))
svm.set_C(norm_c_neg, norm_c_pos)
# set epsilon
if param.flags.has_key("epsilon"):
svm.set_epsilon(param.flags["epsilon"])
# enable output
svm.io.enable_progress()
svm.io.set_loglevel(shogun.Classifier.MSG_INFO)
svm.set_train_factor(param.flags["train_factor"])
svm.train()
# attach svm to node
node.predictor = svm
# save some information
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 (potentiall 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)
# save graph plot
mypath = "/fml/ag-raetsch/share/projects/multitask/graphs/"
filename = mypath + "graph_" + str(param.id)
root.plot(filename)#, plot_cost=True, plot_B=True)
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
#####################################################
# shogun data
feat = shogun_factory_new.create_features(examples, self.param)
out = predictor.classify(feat).get_labels()
return out