Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(VERB, 'ate'),
(DET , 'a'),
(ADJ , 'big'),
(NOUN, 'sandwich')],
[(DET , 'the'),
(NOUN, 'sandwich'),
(VERB, 'was'),
(ADJ , 'tasty')],
[(NOUN, 'it'),
(VERB, 'ate'),
(NOUN, 'it'),
(ADJ , 'all')] ]
# initialize VW as usual, but use 'hook' as the search_task
vw = pyvw.vw("--search 4 --quiet --search_task hook --ring_size 1024")
# tell VW to construct your search task object
sequenceLabeler = vw.init_search_task(SequenceLabeler)
# train it on the above dataset ten times; the my_dataset.__iter__ feeds into _run above
print('training!', file=sys.stderr)
for i in range(10):
sequenceLabeler.learn(my_dataset)
# now see the predictions on a test sentence
print('predicting!', file=sys.stderr)
print(sequenceLabeler.predict( [(1,w) for w in "the sandwich ate a monster".split()] ))
print('should have printed: [1, 2, 3, 1, 2]')
parser = argparse.ArgumentParser()
parser.add_argument('--method', type=str, choices=['reslope', 'prep', 'mc', 'bootstrap'],
default='prep')
parser.add_argument('--env', type=str, choices=[
'gridworld', 'gridworld_stoch', 'gridworld_ep', 'cartpole', 'hex', 'blackjack', 'sl', 'dep'],
help='Environment to run on', default='gridworld')
parser.add_argument('--alr', type=float, help='Actor learning rate', default=0.0005)
parser.add_argument('--vdlr', type=float, help='Value difference learning rate', default=0.005)
parser.add_argument('--clr', type=float, help='Critic learning rate', default=0.005)
parser.add_argument('--clip', type=float, help='Gradient clipping argument', default=10)
parser.add_argument('--exp', type=str, help='Exploration method', default='eps',
choices=['eps', 'softmax', 'bagging'])
parser.add_argument('--exp_param', type=float, help='Parameter for exp. method', default=0.4)
args = parser.parse_args()
# policy = VWPolicy(actor, n_labels, lr=args.alr, exp_type=args.exp, exp_param=args.exp_param)
vd_regressor = pyvw.vw('-l ' + str(args.vdlr), quiet=True)
ref_critic = pyvw.vw('-l ' + str(args.clr), quiet=True)
learner_type = 'prep'
# learner = VwPrep(policy, actor, vd_regressor, ref_critic, learner_type)
loss_fn = sl.HammingLoss
# TODO what is the best value for n_epochs?
n_epochs = 1
warm = True
if warm:
macarico.util.TrainLoop(mk_env, policy, learner, optimizer, losses=[loss_fn, loss_fn, loss_fn], progress_bar=False,
minibatch_size=np.random.choice([1]),).train(training_data=tr, dev_data=de,
n_epochs=n_epochs)
# Load wsj again
data_dir = 'bandit_data/pos/pos_wsj.mac'
n_tr = 42000
n_de = 0
from vowpalwabbit import pyvw
vw = pyvw.vw('--audit')
full = vw.example( { 'a': ['b'], 'x': ['y'] } )
full.learn()
part = vw.example( {'a': ['b'] } )
part.learn()
part.push_features('x', ['y'])
part.learn()
part.erase_namespace(ord('x'))
part.push_features('x', ['z'])
part.learn()
pred = self.sch.predict(examples = examples,
my_tag = i+1,
oracle = oracle,
condition = [ (i, 'p'), (i-1, 'q') ] )
for ex in examples: ex.finish()
output.append( spans[pred][2] )
for j in spans[pred][2]:
covered[j] = True
return output
print('training LDF')
vw = pyvw.vw("--search 0 --csoaa_ldf m --search_task hook --ring_size 1024 --quiet -q ef -q ep")
task = vw.init_search_task(WordAligner)
for p in range(10):
task.learn(my_dataset)
print('====== test ======')
print(task.predict( ("the blue flower".split(), ([],[],[]), "la fleur bleue".split()) ))
print('should have printed [[0], [2], [1]]')
my_dataset2 = [
( "mary did not slap the green witch".split(),
([0], [], [1],[2,3,4],[6],[8], [7]),
"maria no dio una bofetada a la bruja verde".split() ) ]
# 0 1 2 3 4 5 6 7 8
def alignmentError(true, sys):
t = set(true)
s = set(sys)
if len(t | s) == 0: return 0.
return 1. - float(len(t & s)) / float(len(t | s))
class WordAligner(pyvw.SearchTask):
def __init__(self, vw, sch, num_actions):
pyvw.SearchTask.__init__(self, vw, sch, num_actions)
sch.set_options( sch.AUTO_HAMMING_LOSS | sch.IS_LDF | sch.AUTO_CONDITION_FEATURES )
def makeExample(self, E, F, i, j0, l):
f = 'Null' if j0 is None else [ F[j0+k] for k in range(l+1) ]
ex = self.vw.example( { 'e': E[i],
'f': f,
'p': '_'.join(f),
'l': str(l),
'o': [str(i-j0), str(i-j0-l)] if j0 is not None else [] },
labelType = self.vw.lCostSensitive )
lab = 'Null' if j0 is None else str(j0+l)
ex.set_label_string(lab + ':0')
return ex
def mini_vw(inputFile, numPasses, otherArgs):
vw = pyvw.vw(otherArgs)
for p in range(numPasses):
print('pass', (p+1))
h = open(inputFile, 'r')
for l in h.readlines():
if learnFromStrings:
vw.learn(l)
else:
ex = vw.example(l)
vw.learn(ex)
ex.finish()
h.close()
vw.finish()
def get_vw(self):
"""Factory to create a vw instance on demand
Returns
-------
pyvw.vw instance
"""
if self.vw_ is None:
self.vw_ = pyvw.vw(**self.params)
return self.vw_
def __init__(self, **kwargs):
kwargs['passes'] = 1
self.model = pyvw.vw('--quiet', **kwargs)