-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexpenv_runner.py
404 lines (276 loc) · 10.8 KB
/
expenv_runner.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/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 12.03.2009
@author: Christian Widmer
@summary: Runs experiments using the SQLObject-based expenv framework
usage:
expenv_runner new <mss_id> <method> <comment>
expenv_runner run <run_id>
expenv_runner complete <experiment_id>
expenv_runner clean <experiment_id>
"""
#import std packages
import sys
import getopt
import time
import os
import numpy
#import extra packages
from pythongrid import KybJob, process_jobs
#import custom packages
import expenv
from expenv import Method, MultiSplitSet, MultiSourceExperiment, Taxonomy
from expenv import ParameterSvm, ParameterMultiSvm #, ParameterLog
from expenv import execute_run, create_tables
from task_similarities import fetch_gammas, dataset_to_hierarchy
#some global variables
target = "auROC" #"auPRC"
def run_all_for_mss(mss_id, comment="", methods=None, cluster=True):
cluster_flag = ""
if cluster:
cluster_flag = "-c"
if methods == None:
#methods = ["method_plain_svm", "method_union_svm", "method_hierarchy_svm", "method_xval_hierarchy_svm", "method_augmented_svm"]
#methods = ["method_plain_svm", "method_union_svm", "method_hierarchy_svm", "method_augmented_svm", "method_pairwise_multitask"]
methods = ["method_plain_svm"]
for method in methods:
prefix = "nohup python expenv_runner.py " + cluster_flag + " new "
suffix = ' "' + comment + '" 2> /dev/null &'
command = prefix + str(mss_id) + " " + method + suffix
print command
os.system(command)
time.sleep(4)
def run_all(mss_ids, comment="", methods=None, cluster=True):
for mss_id in mss_ids:
run_all_for_mss(mss_id, comment, methods, cluster)
def run_multi_example(dataset_idx, mymethod, comment):
"""
sets up and runs experiment
"""
#######################################
# fix parameters
#######################################
flags= {}
# general
flags["normalize_cost"] = True #False
flags["epsilon"] = 0.03
flags["cache_size"] = 500
# Boosting
#flags["boosting"] = "ones"
#flags["boosting"] = "L1"
#flags["boosting"] = "L2"
flags["boosting"] = "L2_reg"
#flags["use_all_nodes"] = False
flags["signum"] = False
#flags["all_positions"] = True
# MKL
#flags["solver_type"] = "ST_DIRECT" #ST_CPLEX #ST_GLPK) #ST_DIRECT) #ST_NEWTON)
#flags["normalize_trace"] = True
#flags["interleaved"] = True
#flags["mkl_q"] = 0
#WDK_RBF
flags["wdk_rbf_on"] = False
# define parameter search space [float(numpy.power(10, 3.58))] #
costs = [float(c) for c in numpy.exp(numpy.linspace(numpy.log(1000), numpy.log(100000), 8))]
#costs = [float(c) for c in numpy.exp(numpy.linspace(numpy.log(float(numpy.power(10, 3))), numpy.log(10000), 4))]
#costs = [float(c) for c in numpy.exp(numpy.linspace(numpy.log(0.01), numpy.log(1000), 8))]
#[float(c) for c in numpy.exp(numpy.linspace(numpy.log(10), numpy.log(2000), 10))]
costs.reverse()
degrees = [1,2,3,4,5] #[1, 5, 10, 15, 20, 22]
#print "WARNING: Degree is ONE"
base_similarities = [200] #[float(c) for c in numpy.exp(numpy.linspace(numpy.log(1), numpy.log(1000), 8))]
#base_similarities = [float(c) for c in numpy.linspace(1, 5000, 6)] #[1]
#transform_params = [float(c) for c in numpy.linspace(1, 10000, 6)] #[1] #1.5, 2.0, 2.5, 3.0] #, 3.5, 4.0, 4.5, 5.0]
#transform_params = [float(c) for c in numpy.linspace(0.01, 0.99, 6)]
transform_params = [0.99]
generation_parameters = locals()
#######################################
# create experiment
#######################################
# select dataset
multi_split_set = MultiSplitSet.get(dataset_idx)
dataset_name = multi_split_set.description
print "method:", mymethod
print "dataset:", dataset_name
print "multi split set id:", dataset_idx
experiment_description = dataset_name + " (" + mymethod + ") " + comment
# allow different features/kernel types
feature_type = multi_split_set.feature_type
if feature_type == "string":
kernel_type = "WeightedDegreeStringKernel"
else:
kernel_type = "PolyKernel"
kernel_type = "WeightedDegreeRBFKernel"
# create experiment
experiment = MultiSourceExperiment(split_set = multi_split_set,
description = experiment_description,
method_name = mymethod,
meta_data = generation_parameters)
print "experiment id:", experiment.id
#######################################
# create runs
#######################################
if multi_split_set.taxonomy==None:
print "WARNING: NO taxonomy set, generating one for dataset " + dataset_name
taxonomy = dataset_to_hierarchy(dataset_name)
else:
taxonomy = multi_split_set.taxonomy
for cost in costs:
for degree in degrees:
for base in base_similarities:
for transform in transform_params:
param = ParameterMultiSvm(cost=cost,
wdk_degree=degree,
base_similarity=base,
transform=transform,
taxonomy=taxonomy,
kernel=kernel_type,
flags=flags)
print param
Method(module_name=mymethod, param=param, experiment=experiment)
# skip model selection if we only have one model
if len(experiment.methods) > 1:
# create evaluation runs based on splits and methods
run_ids = [run.id for run in experiment.create_eval_runs()]
# execute runs
execute_runs(run_ids)
# finally perform model selection and retrain
select_best_and_test(experiment, target)
#experiment.select_best_method(target)
return experiment.id
def complete_experiment(experiment_id, mem, local, threads, force_rerun=False):
"""
completes runs for experiment with id experiment_id
"""
print "Warning: Overwriting assessment for experiment_id", experiment_id
experiment = expenv.Experiment.get(experiment_id)
# create evaluation runs based on splits and methods
# run_ids = [run.id for run in experiment.eval_runs]
if force_rerun:
run_ids = [run.id for run in experiment.eval_runs]
else:
run_ids = [run.id for run in experiment.eval_runs if not run.assessment or not run.assessment_test]
# execute runs
execute_runs(run_ids, mem, local, threads)
# finally perform model selection and retrain
#experiment.select_best_method(target)
select_best_and_test(experiment, target, mem, local, threads)
def select_best_retrain_test(experiment, target):
"""
select best set of parameters from evaluation runs
"""
# we select best method
experiment.select_best_method(target)
# create test run
test_run = experiment.create_test_run()
print "test run id:", test_run.id
execute_runs([test_run.id])
print "##############################"
print ""
print "final assessment:"
print test_run.assessment
print ""
print "Experiment", experiment.id, "done."
def select_best_and_test(experiment, target, mem, local, threads):
"""
select best set of parameters from evaluation runs
"""
# we select best method
experiment.select_best_method(target)
# create test run
test_run = experiment.create_test_run()
# set flag indicating that this is test run
flags = test_run.method.param.flags
flags["is_test_run"] = True
test_run.method.param.flags = flags
# run
print "test run id:", test_run.id
execute_runs([test_run.id], mem, local, threads)
print "##############################"
print ""
print "final assessment:"
print test_run.assessment
print ""
print "Experiment", experiment.id, "done."
def re_run_run(run_id):
"""
executes individual run
"""
print "Warning: Overwriting assessment for run_id", run_id
execute_run(run_id)
def execute_runs(run_ids, mem, local, threads):
"""
takes a list of run ids and computes them
"""
print "created", len(run_ids), " runs: ", run_ids
# use pythongrid
jobs = []
for run_id in run_ids:
job = KybJob(expenv.execute_run, [run_id])
job.h_vmem = mem
jobs.append(job)
#global local
print "local", local
print "maxNumThreads", threads
finished_jobs = process_jobs(jobs, local=local, maxNumThreads=threads)
MultiSourceExperiment._connection.expireAll()
return finished_jobs
def main():
"""
delegates work
"""
# parse command line options
try:
opts, args = getopt.getopt(sys.argv[1:], "hct:m:f", ["help", "cluster", "threads=", "mem=", "force"])
except getopt.error, msg:
print msg
print "for help use --help"
sys.exit(2)
# set default values
local = True
threads = 1
mem = "1G"
force_rerun = False
print opts
# process options
for o, a in opts:
if o in ("-h", "--help"):
print __doc__
sys.exit(0)
if o in ("-c", "--cluster"):
print "cluster flag set"
local = False
if o in ("-t", "--threads"):
threads = int(a)
if o in ("-m", "--mem"):
mem = a
if o in ("-f", "--force"):
force_rerun = True
if len(args) == 0:
print "need at least one argument"
elif args[0]=="init":
create_tables()
elif args[0]=="new" and len(args)==4:
dataset_idx = int(args[1])
mymethod = args[2]
comment = args[3]
run_multi_example(dataset_idx, mymethod, comment)
elif args[0]=="run" and len(args)==2:
run_id = int(args[1])
re_run_run(run_id)
elif args[0]=="complete" and len(args)==2:
experiment_id = int(args[1])
complete_experiment(experiment_id, mem, local, threads, force_rerun)
elif args[0]=="clean" and len(args)==2:
experiment_id = int(args[1])
experiment = expenv.Experiment.get(experiment_id)
experiment.clean_up()
if __name__ == "__main__":
main()