def test_make_example_setup_done(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --noconstant --quiet", 4) ex = vw.make_example({'x': 'a'}, None) self.assertTrue(hasattr(ex, "setup_done"))
def test_make_examples_setup_done(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --noconstant --quiet", 4) exs = vw.make_examples({'x': 1}, [{'a': 2}, {'a': 3}], None) for ex in exs: self.assertTrue(hasattr(ex, "setup_done"))
def test_make_example_list_string_value(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --noconstant --quiet", 4) ex = vw.make_example({'x': ['a']}, None) self.assertEqual([(ex.get_feature_id("x", "0=a"), 1)], list(ex.iter_features()))
def test_make_example_empty_list(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --noconstant --quiet", 4) ex = vw.make_example({'x': [2], 'y': []}, None) self.assertEqual([(ex.get_feature_id("x", 0), 2)], list(ex.iter_features()))
def test_make_example_dict_numeric_value(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --noconstant --quiet", 4) ex = vw.make_example({'x': {'a': 5}}, None) self.assertEqual([(ex.get_feature_id("x", "a"), 5)], list(ex.iter_features()))
def test_vowpal_mediator_make_example_sequence_dict_performance(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --quiet", 4) ns = {'x': dict(zip(map(str, range(1000)), range(1000)))} time = statistics.mean( timeit.repeat(lambda: vw.make_example(ns, None), repeat=10, number=100)) #.024 was my final average time self.assertLess(time, .24)
def test_vowpal_mediator_make_example_highly_sparse_performance(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --quiet", 4) ns = {'x': [1] + [0] * 1000} time = statistics.mean( timeit.repeat(lambda: vw.make_example(ns, None), repeat=10, number=100)) #.0044 was my final average time self.assertLess(time, .05)
def test_vowpal_mediator_make_examples_sequence_int_performance(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --quiet", 4) shared = {'a': list(range(500))} distincts = [{'x': list(range(500))}, {'x': list(range(500))}] time = statistics.mean( timeit.repeat(lambda: vw.make_examples(shared, distincts, None), repeat=10, number=100)) #.042 was my final average time self.assertLess(time, .42)
def test_make_example_list_mixed_value(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --noconstant --quiet", 4) ex = vw.make_example({'x': ['a', 3.2, 'c']}, None) expected = [(ex.get_feature_id("x", "0=a"), 1), (ex.get_feature_id("x", 1), 3.2), (ex.get_feature_id("x", "2=c"), 1)] actual = list(ex.iter_features()) self.assertEqual(expected[0], actual[0]) self.assertEqual(expected[1], (actual[1][0], round(actual[1][1], 4))) self.assertEqual(expected[2], actual[2])
def test_make_example_label(self): from vowpalwabbit import pyvw, __version__ vw = VowpalMediator() vw.init_learner("--cb_explore 2 --noconstant --quiet", 4) ex = vw.make_example({'x': 1}, "0:.5:1") self.assertEqual(4, ex.labelType) if __version__[0] != '8': self.assertEqual( "0:0.5:1.0", str(ex.get_label(pyvw.LabelType.CONTEXTUAL_BANDIT))) else: self.assertEqual("0:0.5:1.0", str(pyvw.cbandits_label(ex)))
def test_make_examples_labels(self): from vowpalwabbit import __version__, pyvw vw = VowpalMediator() vw.init_learner("--cb_explore_adf --noconstant --quiet", 4) exs = vw.make_examples({'x': 1}, [{'a': 1}, {'a': 2}], ["0:.5:1", ""]) self.assertEqual(4, exs[0].labelType) self.assertEqual(4, exs[1].labelType) if __version__[0] == '9': self.assertEqual( "0:0.5:1.0", str(exs[0].get_label(pyvw.LabelType.CONTEXTUAL_BANDIT))) self.assertEqual( "", str(exs[1].get_label(pyvw.LabelType.CONTEXTUAL_BANDIT))) else: self.assertEqual("0:0.5:1.0", str(pyvw.cbandits_label(exs[0]))) self.assertEqual("", str(pyvw.cbandits_label(exs[1])))
def test_predict(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --epsilon .1 --noconstant --quiet", 4) ex = vw.make_examples({}, [{'a': 1}, {'a': 2}], None) self.assertEqual(2, len(vw.predict(ex)))
def test_make_examples_namespaces(self): from vowpalwabbit.pyvw import example vw = VowpalMediator() vw.init_learner("--cb_explore_adf --noconstant --quiet", 4) ex = cast(Sequence[example], vw.make_examples({'x': 3}, [{ 'a': 1 }, { 'a': 2 }], None)) self.assertEqual(2, len(ex)) expected_0 = [(ex[0].get_feature_id("x", 0), 3), (ex[0].get_feature_id("a", 1), 1)] expected_1 = [(ex[1].get_feature_id("x", 0), 3), (ex[1].get_feature_id("a", 1), 2)] self.assertEqual(expected_0, list(ex[0].iter_features())) self.assertEqual(expected_1, list(ex[1].iter_features()))
def test_learn(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --epsilon .1 --noconstant --quiet", 4) ex = vw.make_examples({'x': 2}, [{'a': 1}, {'a': 2}], ["0:.5:1", ""]) self.assertEqual(0, vw._vw.get_weight(0)) self.assertEqual(0, vw._vw.get_weight(1)) vw.learn(ex) self.assertNotEqual(0, vw._vw.get_weight(0)) self.assertNotEqual(0, vw._vw.get_weight(1)) self.assertEqual( 2, len([ vw._vw.get_weight(i) for i in range(vw._vw.num_weights()) if vw._vw.get_weight(i) != 0 ]))
def test_regression_learning(self): vw = VowpalMediator().init_learner("--quiet", 1) n_features = 10 n_examples = 1000 rng = CobaRandom(1) weights = rng.randoms(n_features) rows = [rng.randoms(n_features) for _ in range(n_examples)] labels = [sum([w * r for w, r in zip(weights, row)]) for row in rows] examples = list(zip(rows, labels)) self.assertEqual(0, vw.predict(vw.make_example({'x': rows[0]}, None))) pred_errs = [] for row, label in examples[int(.9 * n_examples):]: pred_errs.append( vw.predict(vw.make_example({"x": row}, None)) - label) pre_learn_mse = sum([e**2 for e in pred_errs]) // len(pred_errs) for row, label in examples[0:int(.9 * n_examples)]: vw.learn(vw.make_example({"x": row}, str(label))) pred_errs = [] for row, label in examples[int(.9 * n_examples):]: pred_errs.append( vw.predict(vw.make_example({"x": row}, None)) - label) post_learn_mse = sum([e**2 for e in pred_errs]) / len(pred_errs) self.assertNotAlmostEqual(0, pre_learn_mse, places=2) self.assertAlmostEqual(0, post_learn_mse, places=2)
def test_init_twice_exception(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --epsilon .1 --quiet", 4) with self.assertRaises(CobaException) as ex: vw.init_learner("--cb_explore_adf --epsilon .1 --quiet", 4)
def test_init_learner(self): vw = VowpalMediator() vw.init_learner("--cb_explore_adf --epsilon .1 --quiet", 4) self.assertIn("--cb_explore_adf", vw._vw.get_arguments()) self.assertIn("--epsilon", vw._vw.get_arguments())