def test_binary_blocks(): X, Y = toy.generate_blocks(n_samples=10) crf = GridCRF() clf = StructuredPerceptron(problem=crf, max_iter=40) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred)
def test_multinomial_blocks(): X, Y = generate_blocks_multinomial(n_samples=10, noise=0.3, seed=0) crf = GridCRF(n_states=X.shape[-1]) clf = StructuredPerceptron(model=crf, max_iter=10) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred)
def test_multinomial_blocks(): X, Y = toy.generate_blocks_multinomial(n_samples=10, noise=0.3, seed=0) crf = GridCRF(n_states=X.shape[-1]) clf = StructuredPerceptron(problem=crf, max_iter=10) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred)
def test_binary_blocks(): X, Y = generate_blocks(n_samples=10) crf = GridCRF() clf = StructuredPerceptron(model=crf, max_iter=40) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred)
def test_binary_blocks_perceptron_online(): #testing subgradient ssvm on easy binary dataset X, Y = toy.generate_blocks(n_samples=10) crf = GridCRF() clf = StructuredPerceptron(model=crf, max_iter=20) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred)
def test_binary_blocks_perceptron_online(): #testing subgradient ssvm on easy binary dataset X, Y = generate_blocks(n_samples=10) inference_method = get_installed(['qpbo', 'ad3', 'lp'])[0] crf = GridCRF(inference_method=inference_method) clf = StructuredPerceptron(model=crf, max_iter=20) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred)
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)
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)
class PerceptronTrainer: def __init__(self, max_iter=25, verbose=False): self.dsm = DiscourseSequenceModel(True) self.sp = StructuredPerceptron(self.dsm, verbose=(1 if verbose else 0), max_iter=max_iter, average=True) def fit(self, trainX, trainY): import warnings with warnings.catch_warnings(): warnings.simplefilter('ignore') self.sp.fit(trainX, trainY) def predict(self, testX): predY = self.sp.predict(testX) return predY def score(self, testX, testY): return self.sp.score(testX, testY) def weights(self): return self.dsm._vec.inverse_transform(self.sp.w)