Ejemplo n.º 1
0
def test_averaged():
    # Under a lot of noise, averaging helps.  This fails with less noise.
    X, Y = generate_blocks_multinomial(n_samples=15, noise=3, seed=0)
    X_train, Y_train = X[:10], Y[:10]
    X_test, Y_test = X[10:], Y[10:]
    crf = GridCRF()
    clf = StructuredPerceptron(model=crf, max_iter=3)
    clf.fit(X_train, Y_train)
    no_avg_test = clf.score(X_test, Y_test)
    clf.set_params(average=True)
    clf.fit(X_train, Y_train)
    avg_test = clf.score(X_test, Y_test)
    assert_greater(avg_test, no_avg_test)
Ejemplo n.º 2
0
def test_averaged():
    # Under a lot of noise, averaging helps.  This fails with less noise.
    X, Y = generate_blocks_multinomial(n_samples=15, noise=3, seed=0)
    X_train, Y_train = X[:10], Y[:10]
    X_test, Y_test = X[10:], Y[10:]
    crf = GridCRF()
    clf = StructuredPerceptron(model=crf, max_iter=3)
    clf.fit(X_train, Y_train)
    no_avg_test = clf.score(X_test, Y_test)
    clf.set_params(average=True)
    clf.fit(X_train, Y_train)
    avg_test = clf.score(X_test, Y_test)
    assert_greater(avg_test, no_avg_test)
Ejemplo n.º 3
0
def test_averaging_early_stopping():
    """Test averaging over final epoch when early stopping"""
    # we use logical OR, an easy problem solved after the second epoch
    X = np.array([[a, b, 1] for a in (-1, 1) for b in (-1, 1)], dtype=np.float)
    Y = np.array([-1, 1, 1, 1])
    pcp = StructuredPerceptron(model=BinarySVMModel(n_features=3), max_iter=3, average=-1)
    pcp.fit(X, Y)
    # The exact weight is used without the influence of the early iterations
    assert_array_equal(pcp.w, [1, 1, 1])

    # If we were expecting 3 iterations, we would end up with a zero vector
    pcp.set_params(average=2)
    pcp.fit(X, Y)
    assert_array_equal(pcp.w, [0, 0, 0])
Ejemplo n.º 4
0
def test_partial_averaging():
    """Use XOR weight cycling to test partial averaging"""
    X = np.array([[a, b, 1] for a in (-1, 1) for b in (-1, 1)], dtype=np.float)
    Y = np.array([-1, 1, 1, -1])
    pcp = StructuredPerceptron(model=BinarySVMModel(n_features=3), max_iter=5, decay_exponent=1, decay_t0=1)
    weight = {}
    for average in (0, 1, 4, -1):
        pcp.set_params(average=average)
        pcp.fit(X, Y)
        weight[average] = pcp.w
    assert_array_equal(weight[4], weight[-1])
    assert_array_almost_equal(weight[0], [1.5, 3, 0])
    assert_array_almost_equal(weight[1], [1.75, 3.5, 0])
    assert_array_almost_equal(weight[4], [2.5, 5, 0])
Ejemplo n.º 5
0
def test_averaging_early_stopping():
    """Test averaging over final epoch when early stopping"""
    # we use logical OR, an easy problem solved after the second epoch
    X = np.array([[a, b, 1] for a in (-1, 1) for b in (-1, 1)], dtype=np.float)
    Y = np.array([-1, 1, 1, 1])
    pcp = StructuredPerceptron(model=BinaryClf(n_features=3), max_iter=3,
                               average=-1)
    pcp.fit(X, Y)
    # The exact weight is used without the influence of the early iterations
    assert_array_equal(pcp.w, [1, 1, 1])

    # If we were expecting 3 iterations, we would end up with a zero vector
    pcp.set_params(average=2)
    pcp.fit(X, Y)
    assert_array_equal(pcp.w, [0, 0, 0])
Ejemplo n.º 6
0
def test_partial_averaging():
    """Use XOR weight cycling to test partial averaging"""
    X = np.array([[a, b, 1] for a in (-1, 1) for b in (-1, 1)], dtype=np.float)
    Y = np.array([-1, 1, 1, -1])
    pcp = StructuredPerceptron(model=BinaryClf(n_features=3), max_iter=5,
                               decay_exponent=1, decay_t0=1)
    weight = {}
    for average in (0, 1, 4, -1):
        pcp.set_params(average=average)
        pcp.fit(X, Y)
        weight[average] = pcp.w
    assert_array_equal(weight[4], weight[-1])
    assert_array_almost_equal(weight[0], [1.5, 3, 0])
    assert_array_almost_equal(weight[1], [1.75, 3.5, 0])
    assert_array_almost_equal(weight[4], [2.5, 5, 0])
Ejemplo n.º 7
0
def test_xor():
    """Test perceptron behaviour against hand-computed values for XOR"""
    X = np.array([[a, b, 1] for a in (-1, 1) for b in (-1, 1)], dtype=np.float)
    Y = np.array([-1, 1, 1, -1])
    # Should cycle weight vectors (1, 1, -1), (0, 2, 0), (1, 1, 1), (0, 0, 0)
    # but this depends on how ties are settled.  Maybe the test can be
    # made robust to this
    # Batch version should cycle (0, 0, -2), (0, 0, 0)

    expected_predictions = [
        np.array([1, 1, 1, 1]),  # online, no average, w = (0, 0, 0, 0)
        np.array([-1, 1, -1, 1]),  # online, average, w ~= (0.5, 1, 0)
        np.array([1, 1, 1, 1]),  # batch, no average, w = (0, 0, 0)
        np.array([-1, -1, -1, -1]),  # batch, average, w ~= (0, 0, -2)
    ]
    pcp = StructuredPerceptron(model=BinarySVMModel(n_features=3), max_iter=2)
    for pred, (batch, average) in zip(expected_predictions, product((False, True), (False, True))):
        pcp.set_params(batch=batch, average=average)
        pcp.fit(X, Y)
        # We don't compare w explicitly but its prediction.  As the perceptron
        # is invariant to the scaling of w, this will allow the optimization of
        # the underlying implementation
        assert_array_equal(pcp.predict(X), pred)
Ejemplo n.º 8
0
def test_xor():
    """Test perceptron behaviour against hand-computed values for XOR"""
    X = np.array([[a, b, 1] for a in (-1, 1) for b in (-1, 1)], dtype=np.float)
    Y = np.array([-1, 1, 1, -1])
    # Should cycle weight vectors (1, 1, -1), (0, 2, 0), (1, 1, 1), (0, 0, 0)
    # but this depends on how ties are settled.  Maybe the test can be
    # made robust to this
    # Batch version should cycle (0, 0, -2), (0, 0, 0)

    expected_predictions = [
        np.array([1, 1, 1, 1]),  # online, no average, w = (0, 0, 0, 0)
        np.array([-1, 1, -1, 1]),  # online, average, w ~= (0.5, 1, 0)
        np.array([1, 1, 1, 1]),  # batch, no average, w = (0, 0, 0)
        np.array([-1, -1, -1, -1])  # batch, average, w ~= (0, 0, -2)
    ]
    pcp = StructuredPerceptron(model=BinaryClf(n_features=3), max_iter=2)
    for pred, (batch, average) in zip(expected_predictions,
                                      product((False, True), (False, True))):
        pcp.set_params(batch=batch, average=average)
        pcp.fit(X, Y)
        # We don't compare w explicitly but its prediction.  As the perceptron
        # is invariant to the scaling of w, this will allow the optimization of
        # the underlying implementation
        assert_array_equal(pcp.predict(X), pred)