1import pyvw 2 3 4def my_predict(vw, ex): 5 pp = 0. 6 for f,v in ex.iter_features(): 7 pp += vw.get_weight(f) * v 8 return pp 9 10def ensure_close(a,b,eps=1e-6): 11 if abs(a-b) > eps: 12 raise Exception("test failed: expected " + str(a) + " and " + str(b) + " to be " + str(eps) + "-close, but they differ by " + str(abs(a-b))) 13 14############################################################################### 15vw = pyvw.vw("--quiet") 16 17 18############################################################################### 19vw.learn("1 |x a b") 20 21 22############################################################################### 23print '# do some stuff with a read example:' 24ex = vw.example("1 |x a b |y c") 25ex.learn() ; ex.learn() ; ex.learn() ; ex.learn() 26updated_pred = ex.get_updated_prediction() 27print 'current partial prediction =', updated_pred 28 29# compute our own prediction 30print ' my view of example =', str(list(ex.iter_features())) 31my_pred = my_predict(vw, ex) 32print ' my partial prediction =', my_pred 33ensure_close(updated_pred, my_pred) 34print '' 35ex.finish() 36 37############################################################################### 38print '# make our own example from scratch' 39ex = vw.example() 40ex.set_label_string("0") 41ex.push_features('x', ['a', 'b']) 42ex.push_features('y', [('c', 1.)]) 43ex.setup_example() 44 45print ' my view of example =', str(list(ex.iter_features())) 46my_pred2 = my_predict(vw, ex) 47print ' my partial prediction =', my_pred2 48ensure_close(my_pred, my_pred2) 49 50ex.learn() ; ex.learn() ; ex.learn() ; ex.learn() 51print ' final partial prediction =', ex.get_updated_prediction() 52ensure_close(ex.get_updated_prediction(), my_predict(vw,ex)) 53print '' 54ex.finish() 55 56############################################################################### 57exList = [] 58for i in range(120): # note: if this is >=129, we hang!!! 59 ex = vw.example() 60 exList.append(ex) 61 62# this is the safe way to delete the examples for VW to reuse: 63for ex in exList: 64 ex.finish() 65 66exList = [] # this should __del__ the examples, we hope :) 67for i in range(120): # note: if this is >=129, we hang!!! 68 ex = vw.example() 69 exList.append(ex) 70 71for ex in exList: 72 ex.finish() 73 74############################################################################### 75 76for i in range(2): 77 ex = vw.example("1 foo| a b") 78 ex.learn() 79 print 'tag =', ex.get_tag() 80 print 'counter =', ex.get_example_counter() 81 print 'partial pred =', ex.get_partial_prediction() 82 print 'loss =', ex.get_loss() 83 84 print 'label =', ex.get_label() 85 ex.finish() 86 87 88# to be safe, finish explicity (should happen by default anyway) 89vw.finish() 90 91 92############################################################################### 93print '# test some save/load behavior' 94vw = pyvw.vw("--quiet -f test.model") 95ex = vw.example("1 |x a b |y c") 96ex.learn() ; ex.learn() ; ex.learn() ; ex.learn() 97before_save = ex.get_updated_prediction() 98print 'before saving, prediction =', before_save 99ex.finish() 100vw.finish() # this should create the file 101 102# now re-start vw by loading that model 103vw = pyvw.vw("--quiet -i test.model") 104ex = vw.example("1 |x a b |y c") # test example 105ex.learn() 106after_save = ex.get_partial_prediction() 107print ' after saving, prediction =', after_save 108ex.finish() 109ensure_close(before_save, after_save) 110vw.finish() # this should create the file 111 112print 'done!' 113