forked from gatapia/py_ml_utils
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathVowpalWabbit.py
460 lines (416 loc) · 14.3 KB
/
VowpalWabbit.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
from __future__ import absolute_import
import os, sys, subprocess, shlex, tempfile, time, sklearn.base, math
import numpy as np
import pandas as pd
from pandas_extensions import *
_vw_default_path = 'utils/lib/vw'
class _VW(sklearn.base.BaseEstimator):
def __init__(self,
logger=None,
vw=_vw_default_path,
moniker='vowpal_wabbit',
name=None,
bits=None,
loss=None,
passes=10,
silent=False,
l1=None,
l2=None,
learning_rate=None,
quadratic=None,
audit=None,
power_t=None,
adaptive=False,
working_dir=None,
decay_learning_rate=None,
initial_t=None,
minibatch=None,
total=None,
node=None,
unique_id=None,
span_server=None,
bfgs=None,
oaa=None,
ect=None,
csoaa=None,
wap=None,
cb=None,
cb_type=None,
old_model=None,
incremental=False,
mem=None,
nn=None,
invariant=False,
normalized=False,
sgd=False,
ignore=None,
columns=None
):
self.logger = logger
self.vw = vw
self.moniker = moniker
self.name = name
self.bits = bits
self.loss = loss
self.passes = passes
self.silent = silent
self.l1 = l1
self.l2 = l2
self.learning_rate = learning_rate
self.quadratic = quadratic
self.audit = audit
self.power_t = power_t
self.adaptive = adaptive
self.working_dir = working_dir
self.decay_learning_rate = decay_learning_rate
self.initial_t = initial_t
self.minibatch = minibatch
self.total = total
self.node = node
self.unique_id = unique_id
self.span_server = span_server
self.bfgs = bfgs
self.oaa = oaa
self.ect = ect
self.csoaa=csoaa
self.wap = wap
self.cb = cb
self.cb_type = cb_type
self.old_model = old_model
self.incremental = incremental
self.mem = mem
self.nn = nn
self.invariant = invariant
self.normalized = normalized
self.sgd = sgd
self.ignore = ignore
self.columns = columns
if hasattr(self.columns, 'tolist'): self.columns = self.columns.tolist()
def fit(self, X, y=None):
if type(X) is np.ndarray:
if self.columns is None: raise Exception('VowpalWabbit requires columns be set')
X = pd.DataFrame(X, columns=self.columns)
if type(X) is pd.DataFrame: X = X.to_vw(y)
self.vw_ = VW(
logger=self.logger,
vw=self.vw,
moniker=self.moniker,
name=self.name,
bits=self.bits,
loss=self.loss,
passes=self.passes,
silent=self.silent,
l1=self.l1,
l2=self.l2,
learning_rate=self.learning_rate,
quadratic=self.quadratic,
audit=self.audit,
power_t=self.power_t,
adaptive=self.adaptive,
working_dir=self.working_dir,
decay_learning_rate=self.decay_learning_rate,
initial_t=self.initial_t,
minibatch=self.minibatch,
total=self.total,
node=self.node,
unique_id=self.unique_id,
span_server=self.span_server,
bfgs=self.bfgs,
oaa=self.oaa,
ect=self.ect,
csoaa=self.csoaa,
wap=self.wap,
cb=self.cb,
cb_type=self.cb_type,
old_model=self.old_model,
incremental=self.incremental,
mem=self.mem,
nn=self.nn,
invariant=self.invariant,
normalized=self.normalized,
sgd=self.sgd,
ignore=self.ignore
)
self.vw_.training(X)
return self
def predict(self, X):
if type(X) is np.ndarray:
if self.columns is None: raise Exception('VowpalWabbit requires columns be set')
X = pd.DataFrame(X, columns=self.columns)
if type(X) is pd.DataFrame: X = X.to_vw()
self.vw_.predicting(X)
raw = self.vw_.read_predictions_()
return np.asarray(list(raw))
def predict_proba(self, X):
if type(X) is np.ndarray:
if self.columns is None: raise Exception('VowpalWabbit requires columns be set')
X = pd.DataFrame(X, columns=self.columns)
if type(X) is pd.DataFrame: X = X.to_vw()
self.vw_.predicting(X)
preds = list(self.vw_.read_predictions_())
predictions = np.asarray(map(lambda p: 1 / (1 + math.exp(-p)), preds))
return np.vstack([1 - predictions, predictions]).T
class VowpalWabbitRegressor(sklearn.base.RegressorMixin, _VW):
pass
class VowpalWabbitClassifier(sklearn.base.ClassifierMixin, _VW):
pass
class VW:
def __init__(self,
logger=None,
vw=_vw_default_path,
moniker=None,
name=None,
bits=None,
loss=None,
passes=None,
silent=False,
l1=None,
l2=None,
learning_rate=None,
quadratic=None,
cubic=None,
audit=None,
power_t=None,
adaptive=False,
working_dir=None,
decay_learning_rate=None,
initial_t=None,
lda=None,
lda_D=None,
lda_rho=None,
lda_alpha=None,
minibatch=None,
total=None,
node=None,
unique_id=None,
span_server=None,
bfgs=None,
oaa=None,
ect=None,
csoaa=None,
wap=None,
cb=None,
cb_type=None,
old_model=None,
incremental=False,
mem=None,
nn=None,
holdout_off=None,
no_model=None,
invariant=False,
normalized=False,
sgd=False,
ignore=None,
**kwargs):
assert moniker and passes
self.node = node
self.total = total
self.unique_id = unique_id
self.span_server = span_server
if self.node is not None:
assert self.total is not None
assert self.unique_id is not None
assert self.span_server is not None
if name is None:
self.handle = '%s' % moniker
else:
self.handle = '%s.%s' % (moniker, name)
if self.node is not None:
self.handle = "%s.%d" % (self.handle, self.node)
if old_model is None:
self.filename = '%s.model' % self.handle
self.incremental = False
else:
self.filename = old_model
self.incremental = True
self.name = name
self.bits = bits
self.loss = loss
self.vw = vw
self.l1 = l1
self.l2 = l2
self.learning_rate = learning_rate
self.silent = silent
self.passes = passes
self.quadratic = quadratic
self.cubic = cubic
self.power_t = power_t
self.adaptive = adaptive
self.decay_learning_rate = decay_learning_rate
self.audit = audit
self.initial_t = initial_t
self.sgd = sgd
self.lda = lda
self.lda_D = lda_D
self.lda_rho = lda_rho
self.lda_alpha = lda_alpha
self.minibatch = minibatch
self.oaa = oaa
self.ect=ect
self.csoaa=csoaa
self.wap=wap
self.cb=cb
self.cb_type=cb_type
self.bfgs = bfgs
self.mem = mem
self.nn = nn
self.holdout_off = holdout_off
self.no_model = no_model
self.invariant = invariant
self.normalized = normalized
self.sgd = sgd
self.ignore = ignore
self.tmpdir = 'tmpfiles'
if not os.path.isdir(self.tmpdir): os.mkdir(self.tmpdir)
# Do some sanity checking for compatability between models
if self.lda:
assert not self.l1
assert not self.l1
assert not self.l2
assert not self.loss
assert not self.adaptive
assert not self.oaa
assert not self.csoaa
assert not self.wap
assert not self.cb
assert not self.cb_type
assert not self.ect
assert not self.bfgs
else:
assert not self.lda_D
assert not self.lda_rho
assert not self.lda_alpha
assert not self.minibatch
if self.sgd:
assert not self.adaptive
assert not self.invariant
assert not self.normalized
self.working_directory = working_dir or os.getcwd()
def vw_base_command(self, base, is_train):
l = base
if self.no_model is None: l.append('-f %s' % self.get_model_file())
if self.bits is not None: l.append('-b %d' % self.bits)
if self.learning_rate is not None: l.append('--learning_rate=%f' % self.learning_rate)
if self.l1 is not None: l.append('--l1=%f' % self.l1)
if self.l2 is not None: l.append('--l2=%f' % self.l2)
if self.initial_t is not None: l.append('--initial_t=%f' % self.initial_t)
if self.quadratic is not None: l.append('-q %s' % self.quadratic)
if self.cubic is not None: l.append('--cubic %s' % self.cubic)
if self.power_t is not None: l.append('--power_t=%f' % self.power_t)
if self.loss is not None: l.append('--loss_function=%s' % self.loss)
if self.decay_learning_rate is not None: l.append('--decay_learning_rate=%f' % self.decay_learning_rate)
if self.lda is not None: l.append('--lda=%d' % self.lda)
if self.lda_D is not None: l.append('--lda_D=%d' % self.lda_D)
if self.lda_rho is not None: l.append('--lda_rho=%f' % self.lda_rho)
if self.lda_alpha is not None: l.append('--lda_alpha=%f' % self.lda_alpha)
if self.minibatch is not None: l.append('--minibatch=%d' % self.minibatch)
if is_train:
if self.oaa is not None: l.append('--oaa=%d' % self.oaa)
if self.ect is not None: l.append('--ect=%d' % self.ect)
if self.csoaa is not None: l.append('--csoaa=%d' % self.csoaa)
if self.wap is not None: l.append('--wap=%d' % self.wap)
if self.cb is not None: l.append('--cb=%d' % self.cb)
if self.cb_type is not None: l.append('--cb_type %s' % self.cb_type)
if self.unique_id is not None: l.append('--unique_id=%d' % self.unique_id)
if self.total is not None: l.append('--total=%d' % self.total)
if self.node is not None: l.append('--node=%d' % self.node)
if self.span_server is not None: l.append('--span_server=%s' % self.span_server)
if self.mem is not None: l.append('--mem=%d' % self.mem)
if self.audit: l.append('--audit')
if self.bfgs: l.append('--bfgs')
if self.adaptive: l.append('--adaptive')
if self.invariant: l.append('--invariant')
if self.normalized: l.append('--normalized')
if self.sgd: l.append('--sgd')
if self.ignore is not None: l.append('--ignore=%d' % self.ignore)
if self.nn is not None: l.append('--nn=%d' % self.nn)
if self.holdout_off is not None: l.append('--holdout_off')
return ' '.join(l)
def vw_train_command(self, cache_file):
if os.path.exists(self.get_model_file()) and self.incremental:
return self.vw_base_command([self.vw], True) + ' --passes %d -c -i %s' \
% (self.passes, self.get_model_file())
else:
print 'No existing model file or not options.incremental'
return self.vw_base_command([self.vw], True) + ' --passes %d -c' \
% (self.passes)
def vw_test_command(self, model_file, prediction_file):
return self.vw_base_command([self.vw], False) + ' -t -i %s -r %s' % (model_file, prediction_file)
def training(self, instances):
if type(instances) is str:
self.start_training(instances)
self.close_process()
return
f = self.save_tmp_file(instances, True)
self.start_training(f)
self.close_process()
self.del_file(f)
def predicting(self, instances):
if type(instances) is str:
self.start_predicting(instances)
self.close_process()
return
f = self.save_tmp_file(instances, False)
self.start_predicting(f)
self.close_process()
self.del_file(f)
def save_tmp_file(self, instances, training=True):
f = self.tmpfile('_tmp_' + ('training' if training else 'testing') + '_file.vw.')
with open(f, 'wb') as fs: fs.write('\n'.join(instances))
return f
def tmpfile(self, suffix):
_, f = tempfile.mkstemp(dir=self.tmpdir, suffix=suffix)
os.close(_)
return self.tmpdir + '/' + f.split('\\')[-1]
def start_training(self, training_file):
cache_file = self.tmpdir + '/' + self.handle + '.cache'
model_file = self.get_model_file()
# Remove the old cache and model files
if not self.incremental:
self.del_file(cache_file)
self.del_file(model_file)
# Run the actual training
cmd = self.vw_train_command(cache_file)
self.vw_process = self.make_subprocess(cmd, training_file)
def close_process(self):
# Close the process
assert self.vw_process
if self.vw_process.wait() != 0:
raise Exception("vw_process %d (%s) exited abnormally with return code %d" % \
(self.vw_process.pid, self.vw_process.command, self.vw_process.returncode))
def start_predicting(self, testing_file):
model_file = self.get_model_file()
# Be sure that the prediction file has a unique filename, since many processes may try to
# make predictions using the same model at the same time
pred_file = self.handle + '.prediction'
prediction_file = self.tmpfile(pred_file)
self.vw_process = self.make_subprocess(
self.vw_test_command(model_file, prediction_file), testing_file)
self.prediction_file = prediction_file
def parse_prediction(self, p):
return map(float, p.split()) if self.lda else float(p.split()[0])
def read_predictions_(self):
for x in open(self.prediction_file):
yield self.parse_prediction(x)
self.del_file(self.prediction_file)
def del_file(self, file):
#try: os.remove(file)
#except OSError: pass
pass
def make_subprocess(self, command, file):
stdout = open('nul', 'w')
stderr = open('nul', 'w') if self.silent else sys.stderr
commands = shlex.split(str(command))
commands += ['-d', file]
print 'Running command: "%s"' % str(commands)
result = subprocess.Popen(commands,
stdout=stdout, stderr=stderr,
close_fds=sys.platform != "win32",
universal_newlines=True, cwd='.')
result.command = command
return result
def get_model_file(self):
if self.incremental:
return self.filename
else:
return self.tmpdir + '/' + self.filename