-
Notifications
You must be signed in to change notification settings - Fork 2
/
method_multitask_cvxopt.py
214 lines (136 loc) · 5.55 KB
/
method_multitask_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
#!/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 02.06.2009
@author: Christian Widmer
@summary: Implementation of the augmented SVM multitask method5
This methods uses a modified kernel such that tasks,
which are close to each other are more similar by default.
"""
import unittest
import numpy
import helper
from shogun.Shogun import LibSVM, SVMLight, StringCharFeatures, Labels
from shogun.Shogun import DNA, WeightedDegreeStringKernel
from base_method import MultiMethod
from expenv_runner import fetch_gammas
from openopt import QP
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 parameters
M = len(train_data)
# init containers
examples = []
labels = []
# vector to indicate to which task each example belongs
task_vector = []
task_num = 0
# 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])
labels.extend([inst.label for inst in instance_set])
task_vector.extend([task_num]*len(instance_set))
task_num += 1
# shogun data
feat = StringCharFeatures(DNA)
feat.set_string_features(examples)
lab = Labels(numpy.double(labels))
# fetch gammas from parameter object
gammas = param.taxonomy.data
from expenv_runner import TaskMap
tm = TaskMap(param.taxonomy.data)
# fetch gammas from parameter object
gammas = tm.taskmap2matrix(train_data.keys())
print gammas
assert(gammas.shape[0] == len(train_data))
# create kernel
k = WeightedDegreeStringKernel(feat, feat, param.wdk_degree, 0)
y = numpy.array(labels)
km = k.get_kernel_matrix()
km = reweight_kernel_matrix(km, gammas, task_vector)
km = numpy.transpose(y.flatten() * (km*y.flatten()).transpose())
N = len(labels)
f = -numpy.ones(N)
# set up QP
p = QP(km, f, Aeq=y, beq=0, lb=numpy.zeros(N), ub=param.cost*numpy.ones(N))
# run solver
r = p.solve('cvxopt_qp', iprint = 0)
alphas = r.xf
objective = r.ff
predictors = {}
for (k, task_id) in enumerate(train_data.keys()):
# pack all relevant information in predictor
predictors[task_id] = (alphas, param.wdk_degree, 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, wdk_degree, task_vector_lhs, task_num, gammas, train_examples, train_labels) = predictor
print "length alphas:", len(alphas), ", length train_examples:", len(train_examples), ", length train_labels:", len(train_labels)
# shogun data
feat_train = StringCharFeatures(DNA)
feat_train.set_string_features(list(train_examples))
feat_test = StringCharFeatures(DNA)
feat_test.set_string_features(list(examples))
k = WeightedDegreeStringKernel(feat_train, feat_test, wdk_degree, 0)
task_vector_rhs = [task_num]*len(examples)
km = k.get_kernel_matrix()
km = reweight_kernel_matrix(km, gammas, task_vector_lhs, task_vector_rhs)
alphas = numpy.array(alphas)
out = numpy.dot(alphas, km)
####################
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
class TestAugmentedTraining(unittest.TestCase):
def setUp(self):
import expenv
run = expenv.Run.get(13490)
self.instances = run.get_train_data()
self.param = run.method.param
def testtrainsimple(self):
method_internal = Method(self.param)
preds_internal = method_internal.train(self.instances)
if __name__ == '__main__':
unittest.main()