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_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_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_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_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])