Esempio n. 1
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]
Esempio n. 2
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
Esempio n. 3
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
Esempio n. 4
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
Esempio n. 5
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
Esempio n. 6
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
Esempio n. 7
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
Esempio n. 8
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
Esempio n. 9
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()
Esempio n. 10
0
def test_regressor_args():
    # load and parse external data file
    data_file = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                             "resources", "train.dat")
    model = Workspace(oaa=3, data=data_file, passes=30, c=True, k=True)
    assert model.predict("| feature1:2.5") == 1

    # update model in memory
    for _ in range(10):
        model.learn("3 | feature1:2.5")
    assert model.predict("| feature1:2.5") == 3

    # save model
    model.save("tmp.model")
    del model

    # load initial regressor and confirm updated prediction
    new_model = Workspace(i="tmp.model", quiet=True)
    assert new_model.predict("| feature1:2.5") == 3
    del new_model

    # clean up
    os.remove("{}.cache".format(data_file))
    os.remove("tmp.model")
Esempio n. 11
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()
Esempio n. 12
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