Exemplo n.º 1
0
def test_keys_with_list_of_values():
    # No exception in creating and executing model with a key/list pair
    model = Workspace(quiet=True, q=["fa", "fb"])
    model.learn("1 | a b c")
    prediction = model.predict(" | a b c")
    assert isinstance(prediction, float)
    del model
Exemplo n.º 2
0
def test_example_learn():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    ex.learn()
    assert ex.setup_done is True
    ex.unsetup_example()  # unsetup an example as it is already setup
    assert ex.setup_done is False
Exemplo n.º 3
0
def test_slates_label():
    model = Workspace(slates=True, quiet=True)
    slates_shared_label = vowpalwabbit.SlatesLabel.from_example(
        model.example("slates shared 0.8 | shared_0 shared_1"))
    slates_action_label = vowpalwabbit.SlatesLabel.from_example(
        model.example("slates action 1 | action_3"))
    slates_slot_label = vowpalwabbit.SlatesLabel.from_example(
        model.example("slates slot 1:0.8,0:0.1,2:0.1 | slot_0"))
    assert slates_shared_label.type == vowpalwabbit.SlatesLabelType.SHARED
    assert slates_shared_label.labeled == True
    assert isclose(slates_shared_label.cost, 0.8)
    assert str(slates_shared_label) == "slates shared 0.8"
    assert slates_action_label.type == vowpalwabbit.SlatesLabelType.ACTION
    assert slates_action_label.labeled == False
    assert slates_action_label.weight == 1.0
    assert slates_action_label.slot_id == 1
    assert str(slates_action_label) == "slates action 1"
    assert slates_slot_label.type == vowpalwabbit.SlatesLabelType.SLOT
    assert slates_slot_label.labeled == True
    assert slates_slot_label.probabilities[0].action == 1
    assert isclose(slates_slot_label.probabilities[0].score, 0.8)
    assert slates_slot_label.probabilities[1].action == 0
    assert isclose(slates_slot_label.probabilities[1].score, 0.1)
    assert slates_slot_label.probabilities[2].action == 2
    assert isclose(slates_slot_label.probabilities[2].score, 0.1)
    assert str(slates_slot_label) == "slates slot 1:0.8,0:0.1,2:0.1"
    del model
Exemplo n.º 4
0
def test_example_namespace_id():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    ns = vowpalwabbit.NamespaceId(ex, 1)
    assert isinstance(ex.get_ns(1), vowpalwabbit.NamespaceId)
    assert isinstance(ex[2], vowpalwabbit.ExampleNamespace)
    assert ex.setup_done is True
    assert ex.num_features_in(ns) == 3
Exemplo n.º 5
0
def test_multilabel_label():
    model = Workspace(multilabel_oaa=5, quiet=True)
    multil = vowpalwabbit.MultilabelLabel.from_example(
        model.example("1,2,3 |"))
    assert len(multil.labels) == 3
    assert multil.labels[0] == 1
    assert multil.labels[1] == 2
    assert multil.labels[2] == 3
    assert str(multil) == "1,2,3"
Exemplo n.º 6
0
def test_SimpleLabel_example():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    sl2 = vowpalwabbit.SimpleLabel.from_example(ex)
    assert sl2.label == 1.0
    assert sl2.weight == 1.0
    assert sl2.prediction == 0.0
    assert sl2.initial == 0.0
    assert str(sl2) == "1.0"
Exemplo n.º 7
0
def test_CostSensitiveLabel():
    model = Workspace(csoaa=4, quiet=True)
    csl = vowpalwabbit.CostSensitiveLabel.from_example(model.example("2:5 |"))
    assert csl.costs[0].label == 2
    assert csl.costs[0].wap_value == 0.0
    assert csl.costs[0].partial_prediction == 0.0
    assert csl.costs[0].cost == 5.0
    assert str(csl) == "2:5.0"
    del model
Exemplo n.º 8
0
def test_action_scores_prediction_type():
    model = Workspace(loss_function="logistic", csoaa_ldf="m", quiet=True)
    multi_ex = [model.example("1:1 | a b c"), model.example("2:-1  | a b c")]
    model.learn(multi_ex)
    assert model.get_prediction_type() == model.pMULTICLASS
    multi_ex = [model.example("1 | a b c"), model.example("2 | a b c")]
    prediction = model.predict(multi_ex)
    assert isinstance(prediction, int)
    del model
Exemplo n.º 9
0
def test_CBLabel():
    model = Workspace(cb=4, quiet=True)
    cbl = vowpalwabbit.CBLabel.from_example(model.example("1:10:0.5 |"))
    assert cbl.weight == 1.0
    assert cbl.costs[0].action == 1
    assert cbl.costs[0].probability == 0.5
    assert cbl.costs[0].partial_prediction == 0
    assert cbl.costs[0].cost == 10.0
    assert str(cbl) == "1:10.0:0.5"
    del model
Exemplo n.º 10
0
def helper_parse(examples):
    model = Workspace(quiet=True, cb_adf=True)
    ex = model.parse(examples)
    assert len(ex) == 2
    model.learn(ex)
    model.finish_example(ex)
    model.finish()
Exemplo n.º 11
0
def test_learn_predict_multiline():
    model = Workspace(quiet=True, cb_adf=True)
    ex = model.parse(["| a:1 b:0.5", "0:0.1:0.75 | a:0.5 b:1 c:2"])
    assert model.predict(ex) == [0.0, 0.0]
    model.finish_example(ex)
    ex = ["| a", "| b"]
    model.learn(ex)
    assert model.predict(ex) == [0.0, 0.0]
Exemplo n.º 12
0
def test_CBContinuousLabel():
    model = Workspace(cats=4,
                      min_value=185,
                      max_value=23959,
                      bandwidth=3000,
                      quiet=True)
    cb_contl = vowpalwabbit.CBContinuousLabel.from_example(
        model.example("ca 1:10:0.5 |"))
    assert cb_contl.costs[0].action == 1
    assert cb_contl.costs[0].pdf_value == 0.5
    assert cb_contl.costs[0].cost == 10.0
    assert str(cb_contl) == "ca 1:10.0:0.5"
    del model
Exemplo n.º 13
0
def test_example_features():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    ns = vowpalwabbit.NamespaceId(ex, 1)
    assert ex.get_feature_id(ns, "a") == 127530
    ex.push_hashed_feature(ns, 1122)
    ex.push_features("x", [("c", 1.0), "d"])
    ex.push_feature(ns, 11000)
    assert ex.num_features_in("x") == 2
    assert ex.sum_feat_sq(ns) == 5.0
    ns2 = vowpalwabbit.NamespaceId(ex, 2)
    ex.push_namespace(ns2)
    assert ex.pop_namespace()
Exemplo n.º 14
0
def test_command_line_with_space_and_escape_kwargs():
    # load and parse external data file
    test_file_dir = Path(__file__).resolve().parent
    data_file = test_file_dir / "resources" / "train file.dat"

    model = Workspace(oaa=3,
                      data=str(data_file),
                      final_regressor="test model.vw")
    assert model.predict("| feature1:2.5") == 1
    del model

    model_file = Path("test model.vw")
    assert model_file.is_file()
    model_file.unlink()
Exemplo n.º 15
0
def test_save_to_Path():
    model = Workspace(quiet=True)
    model.learn("1 | a b c")
    model.save(Path("tmp1.model"))
    model.save("tmp2.model")
    assert filecmp.cmp("tmp1.model", "tmp2.model")
    os.remove("tmp1.model")
    os.remove("tmp2.model")
Exemplo n.º 16
0
def test_namespace_id():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    nm1 = vowpalwabbit.NamespaceId(ex, 0)
    nm2 = vowpalwabbit.NamespaceId(ex, 1)
    nm3 = vowpalwabbit.NamespaceId(ex, 2)
    assert nm1.id == 0
    assert nm1.ord_ns == 97
    assert nm1.ns == "a"
    assert nm2.id == 1
    assert nm2.ord_ns == 98
    assert nm2.ns == "b"
    assert nm3.id == 2
    assert nm3.ord_ns == 128
    assert nm3.ns == "\x80"  # Represents string of ord_ns
Exemplo n.º 17
0
def test_command_line_using_arg_list():
    # load and parse external data file
    test_file_dir = Path(__file__).resolve().parent
    data_file = test_file_dir / "resources" / "train file.dat"

    args = [
        "--oaa", "3", "--data",
        str(data_file), "--final_regressor", "test model2.vw"
    ]
    model = Workspace(arg_list=args)
    assert model.predict("| feature1:2.5") == 1
    del model

    model_file = Path("test model2.vw")
    assert model_file.is_file()
    model_file.unlink()
Exemplo n.º 18
0
class TestVW:

    model = Workspace(quiet=True, b=BIT_SIZE)

    def test_constructor(self):
        assert isinstance(self.model, Workspace)

    def test_learn_predict(self):
        ex = self.model.example("1 | a b c")
        init = self.model.predict(ex)
        assert init == 0
        self.model.learn(ex)
        assert self.model.predict(ex) > init
        ex = ["| a", "| b"]
        check_error_raises(TypeError, lambda: self.model.predict(ex))
        check_error_raises(TypeError, lambda: self.model.learn(ex))

    def test_get_tag(self):
        ex = self.model.example("1 foo| a b c")
        assert ex.get_tag() == "foo"
        ex = self.model.example("1 1.0 bar| a b c")
        assert ex.get_tag() == "bar"
        ex = self.model.example("1 'baz | a b c")
        assert ex.get_tag() == "baz"

    def test_num_weights(self):
        assert self.model.num_weights() == 2**BIT_SIZE

    def test_get_weight(self):
        assert self.model.get_weight(0, 0) == 0

    def test_finish(self):
        assert not self.model.finished
        self.model.finish()
        assert self.model.finished
Exemplo n.º 19
0
def test_prob_prediction_type():
    model = Workspace(
        loss_function="logistic",
        csoaa_ldf="mc",
        probabilities=True,
        quiet=True,
    )
    multi_ex = [
        model.example("1:0.2 | a b c"),
        model.example("2:0.8  | a b c"),
    ]
    model.learn(multi_ex)
    assert model.get_prediction_type() == vowpalwabbit.PredictionType.PROB
    multi_ex = [model.example("1 | a b c"), model.example("2 | a b c")]
    prediction = model.predict(multi_ex)
    assert isinstance(prediction, float)
    del model
Exemplo n.º 20
0
def test_example_namespace():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    ns_id = vowpalwabbit.NamespaceId(ex, 1)
    ex_nm = vowpalwabbit.ExampleNamespace(ex,
                                          ns_id,
                                          ns_hash=vw_ex.hash_space(ns_id.ns))
    assert isinstance(ex_nm.ex, vowpalwabbit.Example)
    assert isinstance(ex_nm.ns, vowpalwabbit.NamespaceId)
    assert ex_nm.ns_hash == 2514386435
    assert ex_nm.num_features_in() == 3
    assert ex_nm[2] == (11617, 1.0)  # represents (feature, value)
    iter_obj = ex_nm.iter_features()
    for i in range(ex_nm.num_features_in()):
        assert ex_nm[i] == next(iter_obj)
    assert ex_nm.pop_feature()
    ex_nm.push_features(ns_id, ["c", "d"])
    assert ex_nm.num_features_in() == 4
Exemplo n.º 21
0
def test_scalar_prediction_type():
    model = Workspace(quiet=True)
    model.learn("1 | a b c")
    assert model.get_prediction_type() == model.pSCALAR
    prediction = model.predict(" | a b c")
    assert isinstance(prediction, float)
    del model
Exemplo n.º 22
0
def test_action_probs_prediction_type():
    model = Workspace(cb_explore=2, ngram=2, quiet=True)
    model.learn("1 | a b c")
    assert model.get_prediction_type() == model.pACTION_PROBS
    prediction = model.predict(" | a b c")
    assert isinstance(prediction, list)
    del model
Exemplo n.º 23
0
def test_multilabel_prediction_type():
    model = Workspace(multilabel_oaa=4, quiet=True)
    model.learn("1 | a b c")
    assert model.get_prediction_type() == model.pMULTILABELS
    prediction = model.predict(" | a b c")
    assert isinstance(prediction, list)
    del model
Exemplo n.º 24
0
def test_ccb_label():
    model = Workspace(ccb_explore_adf=True, quiet=True)
    ccb_shared_label = vowpalwabbit.CCBLabel.from_example(
        model.example("ccb shared | shared_0 shared_1"))
    ccb_action_label = vowpalwabbit.CCBLabel.from_example(
        model.example("ccb action | action_1 action_3"))
    ccb_slot_label = vowpalwabbit.CCBLabel.from_example(
        model.example("ccb slot 0:0.8:1.0 0 | slot_0"))
    ccb_slot_pred_label = vowpalwabbit.CCBLabel.from_example(
        model.example("ccb slot |"))
    assert ccb_shared_label.type == vowpalwabbit.CCBLabelType.SHARED
    assert len(ccb_shared_label.explicit_included_actions) == 0
    assert ccb_shared_label.outcome is None
    assert str(ccb_shared_label) == "ccb shared"
    assert ccb_action_label.type == vowpalwabbit.CCBLabelType.ACTION
    assert len(ccb_action_label.explicit_included_actions) == 0
    assert ccb_action_label.weight == 1.0
    assert ccb_action_label.outcome is None
    assert str(ccb_action_label) == "ccb action"
    assert ccb_slot_label.type == vowpalwabbit.CCBLabelType.SLOT
    assert ccb_slot_label.explicit_included_actions[0] == 0
    assert ccb_slot_label.outcome.action_probs[0].action == 0
    assert isclose(ccb_slot_label.outcome.action_probs[0].score, 1.0)
    assert isclose(ccb_slot_label.outcome.cost, 0.8)
    assert str(ccb_slot_label) == "ccb slot 0:0.8:1.0 0"
    assert ccb_slot_pred_label.type == vowpalwabbit.CCBLabelType.SLOT
    assert len(ccb_slot_pred_label.explicit_included_actions) == 0
    assert ccb_slot_pred_label.outcome is None
    assert str(ccb_slot_pred_label) == "ccb slot"
    del model
Exemplo n.º 25
0
def test_multiclass_prediction_type():
    n = 3
    model = Workspace(loss_function="logistic", oaa=n, quiet=True)
    model.learn("1 | a b c")
    assert model.get_prediction_type(
    ) == vowpalwabbit.PredictionType.MULTICLASS
    prediction = model.predict(" | a b c")
    assert isinstance(prediction, int)
    del model
Exemplo n.º 26
0
def test_scalars_prediction_type():
    n = 3
    model = Workspace(loss_function="logistic",
                      oaa=n,
                      probabilities=True,
                      quiet=True)
    model.learn("1 | a b c")
    assert model.get_prediction_type() == vowpalwabbit.PredictionType.SCALARS
    prediction = model.predict(" | a b c")
    assert isinstance(prediction, list)
    assert len(prediction) == n
    del model
Exemplo n.º 27
0
def test_parse_2():
    model = Workspace(quiet=True, cb_adf=True)
    ex = model.parse("| a:1 b:0.5\n0:0.1:0.75 | a:0.5 b:1 c:2")
    assert len(ex) == 2
    model.learn(ex)
    model.finish_example(ex)
    model.finish()

    model = Workspace(quiet=True, cb_adf=True)
    ex = model.parse(["| a:1 b:0.5", "0:0.1:0.75 | a:0.5 b:1 c:2"])
    assert len(ex) == 2
    model.learn(ex)
    model.finish_example(ex)
    model.finish()
Exemplo n.º 28
0
def test_delete():
    model = Workspace(quiet=True, b=BIT_SIZE)
    assert "model" in locals()
    del model
    assert "model" not in locals()
Exemplo n.º 29
0
def test_get_weight_name():
    model = Workspace(quiet=True)
    model.learn("1 | a a b c |ns x")
    assert model.get_weight_from_name("a") != 0.0
    assert model.get_weight_from_name("b") != 0.0
    assert model.get_weight_from_name("b") == model.get_weight_from_name("c")
    assert model.get_weight_from_name("a") != model.get_weight_from_name("b")
    assert model.get_weight_from_name("x") == 0.0
    assert model.get_weight_from_name("x", "ns") != 0.0
    assert model.get_weight_from_name("x",
                                      "ns") == model.get_weight_from_name("b")
Exemplo n.º 30
0
def test_example_label():
    vw_ex = Workspace(quiet=True)
    ex = vw_ex.example("1 |a two features |b more features here")
    ex.set_label_string("1.0")
    assert isinstance(ex.get_label(), vowpalwabbit.SimpleLabel)