-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmethod_augmented_cvxopt.py
316 lines (199 loc) · 7.74 KB
/
method_augmented_cvxopt.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/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-2011 Christian Widmer
# Copyright (C) 2009-2011 Max-Planck-Society
"""
Created on 02.06.2009
@author: Christian Widmer
@summary: Implementation of the augmented SVM multitask method
This methods uses a modified kernel such that tasks,
which are close to each other are more similar by default.
This implementation uses openopt as solver.
"""
import numpy
import unittest
import shogun_factory_new as shogun_factory
from base_method import MultiMethod
from openopt import QP
from helper import Options
debug = False
class Method(MultiMethod):
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
"""
# fix dimensions
M = len(train_data)
N = 0
for key in train_data.keys():
N += len(train_data[key])
# init containers
examples = []
labels = []
# vector to indicate to which task each example belongs
task_vector = []
task_num = 0
tmp_examples = 0
label_matrix = numpy.zeros((M,N))
# extract training data
for (task_id, instance_set) in train_data.items():
print "train task id:", task_id
#assert(instance_set[0].dataset.organism==task_id)
examples.extend([inst.example for inst in instance_set])
tmp_labels = [inst.label for inst in instance_set]
labels.extend(tmp_labels)
begin_idx = tmp_examples
end_idx = tmp_examples + len(tmp_labels)
# fill matrix row
label_matrix[task_num, begin_idx:end_idx] = tmp_labels
task_vector.extend([task_num]*len(instance_set))
task_num += 1
tmp_examples += len(tmp_labels)
# fetch gammas from parameter object
# TODO: compute gammas outside of this
gammas = numpy.ones((M,M)) + numpy.eye(M)
#gammas = numpy.eye(M)
# create kernel
kernel = shogun_factory.create_kernel(examples, param)
y = numpy.array(labels)
print "computing kernel matrix"
km = kernel.get_kernel_matrix()
km = reweight_kernel_matrix(km, gammas, task_vector)
# "add" labels to Q-matrix
km = numpy.transpose(y.flatten() * (km*y.flatten()).transpose())
print "done computing kernel matrix, calling solver"
f = -numpy.ones(N)
b = numpy.zeros((M,1))
# set up QP
p = QP(km, f, Aeq=label_matrix, beq=b, lb=numpy.zeros(N), ub=param.cost*numpy.ones(N))
p.debug=1
# run solver
r = p.solve('cvxopt_qp', iprint = 0)
print "done with training"
alphas = r.xf
objective = r.ff
print "alphas:", alphas
predictors = {}
for (k, task_id) in enumerate(train_data.keys()):
# pack all relevant information in predictor
predictors[task_id] = (alphas, param, task_vector, k, gammas, examples, labels)
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: str
"""
(alphas, param, task_vector_lhs, task_num, gammas, train_examples, train_labels) = predictor
print "length alphas:", len(alphas)
# shogun data
feat_train = shogun_factory.create_features(train_examples, param)
feat_test = shogun_factory.create_features(examples, param)
# create kernel
kernel = shogun_factory.create_empty_kernel(param)
kernel.init(feat_train, feat_test)
# all examples belong to same task (called individually per task)
task_vector_rhs = [task_num]*len(examples)
# re-weight kernel matrix
km = kernel.get_kernel_matrix()
km = reweight_kernel_matrix(km, gammas, task_vector_lhs, task_vector_rhs)
# compute output
out = numpy.zeros(len(examples))
for test_idx in xrange(len(examples)):
for train_idx in xrange(len(train_examples)):
out[test_idx] += alphas[train_idx] * train_labels[train_idx] * km[train_idx, test_idx]
return out
def reweight_kernel_matrix(km, gammas, task_vector_lhs, task_vector_rhs=None):
"""
method that computes explicit reweighting of kernel matrix
"""
if task_vector_rhs==None:
task_vector_rhs = task_vector_lhs
# basic sanity checks
assert(km.shape[0]==len(task_vector_lhs))
assert(km.shape[1]==len(task_vector_rhs))
assert(len(set(task_vector_lhs))==len(gammas))
N_lhs = len(task_vector_lhs)
N_rhs = len(task_vector_rhs)
# weight km entries according to gammas
for i in xrange(N_lhs):
task_i = task_vector_lhs[i]
for j in xrange(N_rhs):
task_j = task_vector_rhs[j]
weight = gammas[task_i][task_j]
km[i][j] = km[i][j] * weight
return km
def main():
print "starting debugging:"
SPLIT_POINTER = 1
from expenv import MultiSplitSet
# select dataset
multi_split_set = MultiSplitSet.get(384)
# flags
flags = {}
flags["normalize_cost"] = False
#flags["epsilon"] = 0.005
flags["kernel_cache"] = 200
flags["use_bias"] = False
# arts params
#flags["svm_type"] = "liblineardual"
flags["degree"] = 24
flags["local"] = False
flags["mem"] = "6G"
flags["maxNumThreads"] = 1
#create mock param object by freezable struct
param = Options()
#param.kernel = "GaussianKernel"
param.kernel = "PolyKernel"
param.sigma = 3.0
param.cost = 10.0
param.transform = 1.0
param.id = 666
param.flags = flags
param.taxonomy = multi_split_set.taxonomy.data
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()
class TestAugmentedTraining(unittest.TestCase):
def setUp(self):
import expenv
run = expenv.Run.get(13490)
self.instances = run.get_train_data()
self.test_data = run.get_eval_data()
self.param = run.method.param
flags = {}
flags["kernel_cache"] = 200
#create mock param object by freezable struct
param = Options()
param.kernel = "GaussianKernel"
param.sigma = 3.0
param.cost = 10.0
param.flags = flags
self.param = param
def testtrainsimple(self):
method_internal = Method(self.param)
preds_internal = method_internal.train(self.instances)
assessment = method_internal.evaluate(self.test_data)
assessment.clean_up()
if __name__ == '__main__':
#unittest.main()
main()