-
Notifications
You must be signed in to change notification settings - Fork 3
/
solver_dcd_shogun.py
267 lines (183 loc) · 6.56 KB
/
solver_dcd_shogun.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
import pprint
import time
import numpy
from shogun.Classifier import LibLinearMTL, MSG_DEBUG
from shogun.Classifier import LibLinear, SVMLight, L2R_L1LOSS_SVC_DUAL, LibSVM
from shogun.Features import RealFeatures, Labels, StringCharFeatures, DNA
from shogun.Kernel import LinearKernel, MultitaskKernelNormalizer, WeightedDegreeStringKernel
from base import alphas_to_w, compute_primal_objective, compute_dual_objective #, v_to_w
from dcd_shogun_factory import create_hashed_features_wdk
def solver_dcd_shogun_debug(C, all_xt, all_lt, task_indicator, M, L):
"""
use standard LibLinear for debugging purposes
"""
xt = numpy.array(all_xt)
lt = numpy.array(all_lt)
tt = numpy.array(task_indicator, dtype=numpy.int32)
tsm = numpy.array(M)
num_tasks = L.shape[0]
# sanity checks
assert len(xt) == len(lt) == len(tt)
assert M.shape == L.shape
assert num_tasks == len(set(tt))
# set up shogun objects
if type(xt[0]) == str:
feat = create_hashed_features_wdk(xt, 8)
else:
feat = RealFeatures(xt.T)
lab = Labels(lt)
# set up machinery
svm = LibLinear()
svm.set_liblinear_solver_type(L2R_L1LOSS_SVC_DUAL)
svm.io.set_loglevel(MSG_DEBUG)
svm.set_C(C,C)
svm.set_bias_enabled(False)
# invoke training
svm.set_labels(lab)
svm.train(feat)
# get model parameters
W = [svm.get_w()]
return W, 42, 42
def solver_dcd_shogun(C, all_xt, all_lt, task_indicator, M, L, eps, target_obj):
"""
wrap shogun solver with same interface as others
"""
xt = numpy.array(all_xt)
lt = numpy.array(all_lt)
tt = numpy.array(task_indicator, dtype=numpy.int32)
tsm = numpy.array(M)
laplacian = numpy.array(L)
print "task_sim:", tsm
num_tasks = L.shape[0]
# sanity checks
assert len(xt) == len(lt) == len(tt)
assert M.shape == L.shape
assert num_tasks == len(set(tt))
# set up shogun objects
# set up shogun objects
if type(xt[0]) == str or type(xt[0]) == numpy.string_:
feat = create_hashed_features_wdk(xt, 8)
else:
feat = RealFeatures(xt.T)
lab = Labels(lt)
# set up machinery
svm = LibLinearMTL()
svm.set_epsilon(eps)
svm.io.set_loglevel(MSG_DEBUG)
svm.set_C(C,C)
svm.set_bias_enabled(False)
# set MTL stuff
svm.set_task_indicator_lhs(tt)
svm.set_task_indicator_rhs(tt)
svm.set_num_tasks(num_tasks)
svm.set_task_similarity_matrix(tsm)
svm.set_graph_laplacian(laplacian)
# invoke training
svm.set_labels(lab)
# how often do we like to compute objective etc
svm.set_record_interval(0)
svm.set_target_objective(target_obj)
svm.set_max_iterations(10000000)
# start training
start_time = time.time()
svm.train(feat)
train_time = time.time() - start_time
print "training time:", train_time, "seconds"
objectives = svm.get_primal_objectives()
print "computing objectives one last time"
#obj_dual = svm.compute_dual_obj()
obj_primal = svm.compute_primal_obj()
print "obj primal", obj_primal
#print "obj dual", obj_dual
#print "actual duality gap:", obj_primal - obj_dual
#rd = [obj - svm.get_primal_objectives()[-1] for obj in svm.get_primal_objectives()]
train_times = svm.get_training_times()
# get model parameters
#V = svm.get_W().T
#alphas = svm.get_alphas()
#dual_obj_python = compute_dual_objective(alphas, xt, lt, task_indicator, M)
#print "dual obj python", dual_obj_python
#print "dual obj C++", dual_obj
#print alphas
#W = alphas_to_w(alphas, xt, lt, task_indicator, M)
#print W
#primal_obj = compute_primal_objective(W.reshape(W.shape[0] * W.shape[1]), C, xt, lt, task_indicator, L)
#print "python primal", primal_obj
#W = [svm.get_w()]
# compare dual obj
return objectives, train_times
def solver_mtk_shogun(C, all_xt, all_lt, task_indicator, M, L, eps, target_obj):
"""
implementation using multitask kernel
"""
xt = numpy.array(all_xt)
lt = numpy.array(all_lt)
tt = numpy.array(task_indicator, dtype=numpy.int32)
tsm = numpy.array(M)
print "task_sim:", tsm
num_tasks = L.shape[0]
# sanity checks
assert len(xt) == len(lt) == len(tt)
assert M.shape == L.shape
assert num_tasks == len(set(tt))
# set up shogun objects
if type(xt[0]) == numpy.string_:
feat = StringCharFeatures(DNA)
xt = [str(a) for a in xt]
feat.set_features(xt)
base_kernel = WeightedDegreeStringKernel(feat, feat, 8)
else:
feat = RealFeatures(xt.T)
base_kernel = LinearKernel(feat, feat)
lab = Labels(lt)
# set up normalizer
normalizer = MultitaskKernelNormalizer(tt.tolist())
for i in xrange(num_tasks):
for j in xrange(num_tasks):
normalizer.set_task_similarity(i, j, M[i,j])
print "num of unique tasks: ", normalizer.get_num_unique_tasks(task_indicator)
# set up kernel
base_kernel.set_cache_size(2000)
base_kernel.set_normalizer(normalizer)
base_kernel.init_normalizer()
# set up svm
svm = SVMLight() #LibSVM()
svm.set_epsilon(eps)
#print "reducing num threads to one"
#svm.parallel.set_num_threads(1)
#print "using one thread"
# how often do we like to compute objective etc
svm.set_record_interval(0)
svm.set_target_objective(target_obj)
svm.set_linadd_enabled(False)
svm.set_batch_computation_enabled(False)
svm.io.set_loglevel(MSG_DEBUG)
#SET THREADS TO 1
svm.set_C(C,C)
svm.set_bias_enabled(False)
# prepare for training
svm.set_labels(lab)
svm.set_kernel(base_kernel)
# train svm
svm.train()
train_times = svm.get_training_times()
objectives = [-obj for obj in svm.get_dual_objectives()]
if False:
# get model parameters
sv_idx = svm.get_support_vectors()
sparse_alphas = svm.get_alphas()
assert len(sv_idx) == len(sparse_alphas)
# compute dense alpha (remove label)
alphas = numpy.zeros(len(xt))
for id_sparse, id_dense in enumerate(sv_idx):
alphas[id_dense] = sparse_alphas[id_sparse] * lt[id_dense]
# print alphas
W = alphas_to_w(alphas, xt, lt, task_indicator, M)
primal_obj = compute_primal_objective(W.reshape(W.shape[0] * W.shape[1]), C, all_xt, all_lt, task_indicator, L)
objectives.append(primal_obj)
train_times.append(train_times[-1] + 100)
return objectives, train_times
def main():
print "implement me"
if __name__ == "__main__":
main()