def test_graph_crf_inference(): # create two samples with different graphs # two states only, pairwise smoothing for inference_method in ["qpbo", "lp", "ad3", "dai"]: crf = GraphCRF(n_states=2, inference_method=inference_method) assert_array_equal(crf.inference((x_1, g_1), w), y_1) assert_array_equal(crf.inference((x_2, g_2), w), y_2)
def test_standard_svm_blobs_2d_class_weight(): # no edges, reduce to crammer-singer svm X, Y = make_blobs(n_samples=210, centers=3, random_state=1, cluster_std=3, shuffle=False) X = np.hstack([X, np.ones((X.shape[0], 1))]) X, Y = X[:170], Y[:170] X_graphs = [(x[np.newaxis, :], np.empty((0, 2), dtype=np.int)) for x in X] pbl = GraphCRF(n_features=3, n_states=3, inference_method='unary') svm = OneSlackSSVM(pbl, check_constraints=False, C=1000) svm.fit(X_graphs, Y[:, np.newaxis]) weights = 1. / np.bincount(Y) weights *= len(weights) / np.sum(weights) pbl_class_weight = GraphCRF(n_features=3, n_states=3, class_weight=weights, inference_method='unary') svm_class_weight = OneSlackSSVM(pbl_class_weight, C=10, check_constraints=False, break_on_bad=False) svm_class_weight.fit(X_graphs, Y[:, np.newaxis]) assert_greater(f1_score(Y, np.hstack(svm_class_weight.predict(X_graphs))), f1_score(Y, np.hstack(svm.predict(X_graphs))))
def test_graph_crf_loss_augment(): x = (x_1, g_1) y = y_1 crf = GraphCRF(n_states=2, inference_method="lp") y_hat, energy = crf.loss_augmented_inference(x, y, w, return_energy=True) # check that y_hat fulfills energy + loss condition assert_almost_equal(np.dot(w, crf.psi(x, y_hat)) + crf.loss(y, y_hat), -energy)
def test_directed_graph_chain(): # check that a directed model actually works differntly in the two # directions. chain of length three, three states 0, 1, 2 which want to be # in this order, evidence only in the middle x = (np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]]), np.array([[0, 1], [1, 2]])) w = np.array([ 1, 0, 0, # unary 0, 1, 0, 0, 0, 1, 0, 1, 0, # pairwise 0, 0, 1, 0, 0, 0 ]) crf = GraphCRF(n_states=3, n_features=3, directed=True) y = crf.inference(x, w) assert_array_equal([0, 1, 2], y)
def test_graph_crf_inference(): # create two samples with different graphs # two states only, pairwise smoothing for inference_method in get_installed(['qpbo', 'lp', 'ad3', 'ogm']): crf = GraphCRF(n_states=2, n_features=2, inference_method=inference_method) assert_array_equal(crf.inference((x_1, g_1), w), y_1) assert_array_equal(crf.inference((x_2, g_2), w), y_2)
def test_graph_crf_energy_lp_integral(): crf = GraphCRF(n_states=2, inference_method="lp") inf_res, energy_lp = crf.inference((x_1, g_1), w, relaxed=True, return_energy=True) # integral solution assert_array_almost_equal(np.max(inf_res[0], axis=-1), 1) y = np.argmax(inf_res[0], axis=-1) # energy and psi check out assert_almost_equal(energy_lp, -np.dot(w, crf.psi((x_1, g_1), y)))
def test_directed_graph_crf_inference(): # create two samples with different graphs # two states only, pairwise smoothing # same as above, only with full symmetric matrix for inference_method in get_installed(['qpbo', 'lp', 'ad3', 'ogm']): crf = GraphCRF(n_states=2, n_features=2, inference_method=inference_method, directed=True) assert_array_equal(crf.inference((x_1, g_1), w_sym), y_1) assert_array_equal(crf.inference((x_2, g_2), w_sym), y_2)
def test_graph_crf_energy_lp_integral(): crf = GraphCRF(n_states=2, inference_method='lp', n_features=2) inf_res, energy_lp = crf.inference((x_1, g_1), w, relaxed=True, return_energy=True) # integral solution assert_array_almost_equal(np.max(inf_res[0], axis=-1), 1) y = np.argmax(inf_res[0], axis=-1) # energy and joint_feature check out assert_almost_equal(energy_lp, -np.dot(w, crf.joint_feature((x_1, g_1), y)), 4)
def test_graph_crf_energy_lp_relaxed(): crf = GraphCRF(n_states=2, inference_method="lp") for i in xrange(10): w_ = np.random.uniform(size=w.shape) inf_res, energy_lp = crf.inference((x_1, g_1), w_, relaxed=True, return_energy=True) assert_almost_equal(energy_lp, -np.dot(w_, crf.psi((x_1, g_1), inf_res))) # now with fractional solution x = np.array([[0, 0], [0, 0], [0, 0]]) inf_res, energy_lp = crf.inference((x, g_1), w, relaxed=True, return_energy=True) assert_almost_equal(energy_lp, -np.dot(w, crf.psi((x, g_1), inf_res)))
def test_graph_crf_class_weights(): # no edges crf = GraphCRF(n_states=3, n_features=3, inference_method="dai") w = np.array([1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]) # unary # pairwise x = (np.array([[1, 1.5, 1.1]]), np.empty((0, 2))) assert_equal(crf.inference(x, w), 1) # loss augmented inference picks last assert_equal(crf.loss_augmented_inference(x, [1], w), 2) # with class-weights, loss for class 1 is smaller, loss-augmented inference # will find it crf = GraphCRF(n_states=3, n_features=3, inference_method="dai", class_weight=[1, 0.1, 1]) assert_equal(crf.loss_augmented_inference(x, [1], w), 1)
def make_random_trees(n_samples=50, n_nodes=100, n_states=7, n_features=10): crf = GraphCRF(inference_method='max-product', n_states=n_states, n_features=n_features) weights = np.random.randn(crf.size_joint_feature) X, y = [], [] for i in range(n_samples): distances = np.random.randn(n_nodes, n_nodes) features = np.random.randn(n_nodes, n_features) tree = minimum_spanning_tree(sparse.csr_matrix(distances)) edges = np.c_[tree.nonzero()] X.append((features, edges)) y.append(crf.inference(X[-1], weights)) return X, y, weights
def test_initialize(): x = (x_1, g_1) y = y_1 crf = GraphCRF(n_states=2, n_features=2) # no-op crf.initialize([x], [y]) #test initialization works crf = GraphCRF() crf.initialize([x], [y]) assert_equal(crf.n_states, 2) assert_equal(crf.n_features, 2) crf = GraphCRF(n_states=3) assert_raises(ValueError, crf.initialize, X=[x], Y=[y])
def test_graph_crf_continuous_inference(): for inference_method in get_installed(['lp', 'ad3']): crf = GraphCRF(n_states=2, inference_method=inference_method) y_hat = crf.inference((x_1, g_1), w, relaxed=True) if isinstance(y_hat, tuple): assert_array_equal(np.argmax(y_hat[0], axis=-1), y_1) else: # ad3 produces integer result if it found the exact solution assert_array_equal(y_hat, y_1) y_hat = crf.inference((x_2, g_2), w, relaxed=True) if isinstance(y_hat, tuple): assert_array_equal(np.argmax(y_hat[0], axis=-1), y_2) else: assert_array_equal(y_hat, y_2)
def test_graph_crf_continuous_inference(): for inference_method in get_installed(['lp', 'ad3']): crf = GraphCRF(n_states=2, n_features=2, inference_method=inference_method) y_hat = crf.inference((x_1, g_1), w, relaxed=True) if isinstance(y_hat, tuple): assert_array_equal(np.argmax(y_hat[0], axis=-1), y_1) else: # ad3 produces integer result if it found the exact solution assert_array_equal(y_hat, y_1) y_hat = crf.inference((x_2, g_2), w, relaxed=True) if isinstance(y_hat, tuple): assert_array_equal(np.argmax(y_hat[0], axis=-1), y_2) else: assert_array_equal(y_hat, y_2)
def test_logging(): iris = load_iris() X, y = iris.data, iris.target X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X] Y = y.reshape(-1, 1) X_train, X_test, y_train, y_test = train_test_split(X_, Y, random_state=1) _, file_name = mkstemp() pbl = GraphCRF(n_features=4, n_states=3, inference_method=inference_method) logger = SaveLogger(file_name) svm = NSlackSSVM(pbl, C=100, n_jobs=1, logger=logger) svm.fit(X_train, y_train) score_current = svm.score(X_test, y_test) score_auto_saved = logger.load().score(X_test, y_test) alt_file_name = file_name + "alt" logger.save(svm, alt_file_name) logger.file_name = alt_file_name logger.load() score_manual_saved = logger.load().score(X_test, y_test) assert_less(.97, score_current) assert_less(.97, score_auto_saved) assert_less(.97, score_manual_saved) assert_almost_equal(score_auto_saved, score_manual_saved)
def define_model(self): if self.models == 'GridCRF': from pystruct.models import GridCRF self.crf = GridCRF( neighborhood=self.models_parameters['neighborhood'], inference_method=self.models_parameters['inference'] ) if self.models == 'GraphCRF': self.prepare_data_to_graph_crf() logging.info('Class weight: ' + str([self.total_label_one_avg,1-self.total_label_one_avg])) from pystruct.models import GraphCRF self.crf = GraphCRF( inference_method=self.models_parameters['inference'], directed = self.models_parameters['directed'], class_weight = [self.total_label_one_avg,1-self.total_label_one_avg] # class_weight=[0.01, 0.99] ) if self.models == 'EdgeFeatureGraphCRF': self.prepare_data_to_edge_feature_graph_crf() logging.info('Class weight: ' + str([self.total_label_one_avg, 1 - self.total_label_one_avg])) from pystruct.models import EdgeFeatureGraphCRF self.crf = EdgeFeatureGraphCRF( inference_method=self.models_parameters['inference'], # directed=self.models_parameters['directed'], class_weight=[self.total_label_one_avg, 1 - self.total_label_one_avg] # class_weight=[0.01, 0.99] ) return
def test_binary_blocks_cutting_plane(): #testing cutting plane ssvm on easy binary dataset # generate graphs explicitly for each example for inference_method in get_installed(["dai", "lp", "qpbo", "ad3", 'ogm']): print("testing %s" % inference_method) X, Y = generate_blocks(n_samples=3) crf = GraphCRF(inference_method=inference_method) clf = NSlackSSVM(model=crf, max_iter=20, C=100, check_constraints=True, break_on_bad=False, n_jobs=1) x1, x2, x3 = X y1, y2, y3 = Y n_states = len(np.unique(Y)) # delete some rows to make it more fun x1, y1 = x1[:, :-1], y1[:, :-1] x2, y2 = x2[:-1], y2[:-1] # generate graphs X_ = [x1, x2, x3] G = [make_grid_edges(x) for x in X_] # reshape / flatten x and y X_ = [x.reshape(-1, n_states) for x in X_] Y = [y.ravel() for y in [y1, y2, y3]] X = zip(X_, G) clf.fit(X, Y) Y_pred = clf.predict(X) for y, y_pred in zip(Y, Y_pred): assert_array_equal(y, y_pred)
def test_binary_blocks_one_slack_graph(): #testing cutting plane ssvm on easy binary dataset # generate graphs explicitly for each example X, Y = generate_blocks(n_samples=3) crf = GraphCRF(inference_method=inference_method) clf = OneSlackSSVM(model=crf, max_iter=100, C=1, check_constraints=True, break_on_bad=True, n_jobs=1, tol=.1) x1, x2, x3 = X y1, y2, y3 = Y n_states = len(np.unique(Y)) # delete some rows to make it more fun x1, y1 = x1[:, :-1], y1[:, :-1] x2, y2 = x2[:-1], y2[:-1] # generate graphs X_ = [x1, x2, x3] G = [make_grid_edges(x) for x in X_] # reshape / flatten x and y X_ = [x.reshape(-1, n_states) for x in X_] Y = [y.ravel() for y in [y1, y2, y3]] X = list(zip(X_, G)) clf.fit(X, Y) Y_pred = clf.predict(X) for y, y_pred in zip(Y, Y_pred): assert_array_equal(y, y_pred)
def test_directed_graph_chain(): # check that a directed model actually works differntly in the two # directions. chain of length three, three states 0, 1, 2 which want to be # in this order, evidence only in the middle x = (np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]]), np.array([[0, 1], [1, 2]])) w = np.array([1, 0, 0, # unary 0, 1, 0, 0, 0, 1, 0, 1, 0, # pairwise 0, 0, 1, 0, 0, 0]) crf = GraphCRF(n_states=3, n_features=3, directed=True) y = crf.inference(x, w) assert_array_equal([0, 1, 2], y)
def graph_crf(): crf = GraphCRF() # X_train # creating features # maximum number of attributes = 2 # variables have only one attribute (assigned value), so other second attribute is set to zero feature_1 = [1, 0] # var_1 feature_2 = [2, 0] # var_2 # function has two attributes, so an indicator variable is used to show those two feature_3 = [1, 1] # function # if has only one condition, which checks for value 1 feature_4 = [1, 0] # if features = np.array([feature_1, feature_2, feature_3, feature_4]) # creating edges # there are four edges: (v1, v2), (v1, func), (v2, func), (v1, if) edge_1 = [0, 1] # (v1,v2) edge_2 = [0, 2] # (v1, func) edge_3 = [1, 2] # (v2, func) edge_4 = [0, 3] # (v1, if) edges = np.array([edge_1, edge_2, edge_3, edge_4]) X_train_sample = (features, edges) # y_train # These are enumerated values for actions # We assume there should be an action for each node(variable, function, if, etc.) y_train_sample = np.array([0, 0, 1, 2]) # creat some full training set by re-sampling above thing n_samples = 100 X_train = [] y_train = [] for i in range(n_samples): X_train.append(X_train_sample) y_train.append(y_train_sample) model = GraphCRF(directed=True, inference_method="max-product") ssvm = FrankWolfeSSVM(model=model, C=.1, max_iter=10) ssvm.fit(X_train, y_train) # predict something output = ssvm.predict(X_train[0:3]) print output
def test_graph_crf_class_weights(): # no edges crf = GraphCRF(n_states=3, n_features=3) w = np.array([ 1, 0, 0, # unary 0, 1, 0, 0, 0, 1, 0, # pairwise 0, 0, 0, 0, 0 ]) x = (np.array([[1, 1.5, 1.1]]), np.empty((0, 2))) assert_equal(crf.inference(x, w), 1) # loss augmented inference picks last assert_equal(crf.loss_augmented_inference(x, [1], w), 2) # with class-weights, loss for class 1 is smaller, loss-augmented inference # will find it crf = GraphCRF(n_states=3, n_features=3, class_weight=[1, .1, 1]) assert_equal(crf.loss_augmented_inference(x, [1], w), 1)
def test_graph_crf_loss_augment(): x = (x_1, g_1) y = y_1 crf = GraphCRF() crf.initialize([x], [y]) y_hat, energy = crf.loss_augmented_inference(x, y, w, return_energy=True) # check that y_hat fulfills energy + loss condition assert_almost_equal(np.dot(w, crf.joint_feature(x, y_hat)) + crf.loss(y, y_hat), -energy)
def structraining(self, bags, mentions, retweets, labels): total_datas = [] total_labels = [] print('num_user', len(bags.keys())) for user_id, bag in bags.items(): if not user_id in labels: continue features = np.empty((0, self.top_seq)) edge_nodes = np.empty((0, 2)) edge_features = np.empty((0, 1)) clique_labels = np.array([labels[user_id]]) features = np.vstack([features, bag]) mentioned_ids = mentions[user_id] cnt = 0 for mentioned_id in enumerate(mentioned_ids): if not mentioned_id in labels: continue clique_labels = np.append(clique_labels, np.array([labels[mentioned_id]])) if mentioned_id in bags: features = np.vstack([features, bags[mentioned_id]]) else: features = np.vstack([features, np.zeros(self.top_seq)]) edge_nodes = np.vstack([edge_nodes, np.array([0, cnt + 1])]) cnt += 1 num_mentioned = edge_nodes.shape[0] retweet_ids = retweets[user_id] cnt = 0 for retweet_id in retweet_ids: if not retweet_id in labels: continue clique_labels = np.append(clique_labels, np.array([labels[retweet_id]])) if retweet_id in bags: features = np.vstack([features, bags[retweet_id]]) else: features = np.vstack([features, np.zeros(self.top_seq)]) edge_nodes = np.vstack([edge_nodes, np.array([0, cnt + 1 + num_mentioned])]) cnt += 1 total_datas.append((features, edge_nodes.astype(int))) total_labels.append(clique_labels) ratio = len(total_datas)*0.7 ratio = int(ratio) print(ratio) X_train, y_train = total_datas[:ratio], total_labels[:ratio] X_test, y_test = total_datas[ratio:], total_labels[ratio:] model = GraphCRF(inference_method="max-product") ssvm = FrankWolfeSSVM(model=model, C=0.1, max_iter=10) ssvm.fit(X_train, y_train) result = ssvm.score(X_test, y_test) print(result)
def test_binary_blocks_cutting_plane_latent_node(): #testing cutting plane ssvm on easy binary dataset # we use the LatentNodeCRF without latent nodes and check that it does the # same as GraphCRF X, Y = generate_blocks(n_samples=3) crf = GraphCRF() clf = NSlackSSVM(model=crf, max_iter=20, C=100, check_constraints=True, break_on_bad=False, n_jobs=1) x1, x2, x3 = X y1, y2, y3 = Y n_states = len(np.unique(Y)) # delete some rows to make it more fun x1, y1 = x1[:, :-1], y1[:, :-1] x2, y2 = x2[:-1], y2[:-1] # generate graphs X_ = [x1, x2, x3] G = [make_grid_edges(x) for x in X_] # reshape / flatten x and y X_ = [x.reshape(-1, n_states) for x in X_] Y = [y.ravel() for y in [y1, y2, y3]] X = zip(X_, G) clf.fit(X, Y) Y_pred = clf.predict(X) for y, y_pred in zip(Y, Y_pred): assert_array_equal(y, y_pred) latent_crf = LatentNodeCRF(n_labels=2, n_hidden_states=0) latent_svm = LatentSSVM(NSlackSSVM(model=latent_crf, max_iter=20, C=100, check_constraints=True, break_on_bad=False, n_jobs=1), latent_iter=3) X_latent = zip(X_, G, np.zeros(len(X_))) latent_svm.fit(X_latent, Y, H_init=Y) Y_pred = latent_svm.predict(X_latent) for y, y_pred in zip(Y, Y_pred): assert_array_equal(y, y_pred) assert_array_almost_equal(latent_svm.w, clf.w)
def test_n_slack_svm_as_crf_pickling(): iris = load_iris() X, y = iris.data, iris.target X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X] Y = y.reshape(-1, 1) X_train, X_test, y_train, y_test = train_test_split(X_, Y, random_state=1) _, file_name = mkstemp() pbl = GraphCRF(n_features=4, n_states=3, inference_method='lp') logger = SaveLogger(file_name) svm = NSlackSSVM(pbl, C=100, n_jobs=1, logger=logger) svm.fit(X_train, y_train) assert_less(.97, svm.score(X_test, y_test)) assert_less(.97, logger.load().score(X_test, y_test))
def createModel(data, labels): model = GraphCRF(n_states=3, n_features=int(len(columns) - 4), directed=True, inference_method='max-product') clf = StructuredPerceptron(model=model, max_iter=100, verbose=False, batch=False, average=True) print("Structured Perceptron + Graph CRF") train_start = time() clf.fit(X=data, Y=labels) train_end = time() print("Training took " + str((train_end - train_start) / 60) + " minutes to complete\n") return clf
def test_svm_as_crf_pickling_batch(): iris = load_iris() X, y = iris.data, iris.target X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X] Y = y.reshape(-1, 1) X_train, X_test, y_train, y_test = train_test_split(X_, Y, random_state=1) _, file_name = mkstemp() pbl = GraphCRF(n_features=4, n_states=3, inference_method='unary') logger = SaveLogger(file_name) svm = FrankWolfeSSVM(pbl, C=10, logger=logger, max_iter=50, batch_mode=False) svm.fit(X_train, y_train) assert_less(.97, svm.score(X_test, y_test)) assert_less(.97, logger.load().score(X_test, y_test))
def test_standard_svm_blobs_2d(): # no edges, reduce to crammer-singer svm X, Y = make_blobs(n_samples=80, centers=3, random_state=42) # we have to add a constant 1 feature by hand :-/ X = np.hstack([X, np.ones((X.shape[0], 1))]) X_train, X_test, Y_train, Y_test = X[:40], X[40:], Y[:40], Y[40:] X_train_graphs = [(x[np.newaxis, :], np.empty((0, 2), dtype=np.int)) for x in X_train] X_test_graphs = [(x[np.newaxis, :], np.empty((0, 2), dtype=np.int)) for x in X_test] pbl = GraphCRF(n_features=3, n_states=3, inference_method='unary') svm = OneSlackSSVM(pbl, check_constraints=True, C=1000) svm.fit(X_train_graphs, Y_train[:, np.newaxis]) assert_array_equal(Y_test, np.hstack(svm.predict(X_test_graphs)))
def runIt(train_list): X_org = list2features(train_list) X = np.array(X_org) y = list2labels_sleep(train_list) y_org = np.array(y) Y = y_org.reshape(-1, 1) X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X] X_train, X_test, y_train, y_test = train_test_split(X_, Y, test_size=.5) pbl = GraphCRF(inference_method='unary') svm = NSlackSSVM(pbl, C=100) start = time() svm.fit(X_train, y_train) time_svm = time() - start y_pred = np.vstack(svm.predict(X_test)) print("Score with pystruct crf svm: %f (took %f seconds)" % (np.mean(y_pred == y_test), time_svm))
def test_graph_crf_energy_lp_relaxed(): crf = GraphCRF(n_states=2, n_features=2) for i in xrange(10): w_ = np.random.uniform(size=w.shape) inf_res, energy_lp = crf.inference((x_1, g_1), w_, relaxed=True, return_energy=True) assert_almost_equal(energy_lp, -np.dot(w_, crf.joint_feature((x_1, g_1), inf_res))) # now with fractional solution x = np.array([[0, 0], [0, 0], [0, 0]]) inf_res, energy_lp = crf.inference((x, g_1), w, relaxed=True, return_energy=True) assert_almost_equal(energy_lp, -np.dot(w, crf.joint_feature((x, g_1), inf_res)))
def learn(train_set): X = [] y = [] for num in train_set: X += get_features_value(num) y += get_segments_classes(num) X = np.array(X) X = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X] y = np.vstack(y) pbl = GraphCRF(inference_method='unary') #svm = NSlackSSVM(pbl, C=100) svm = FrankWolfeSSVM(pbl, C=10, max_iter=50) svm.fit(X, y) cPickle.dump(svm, open("classifier", "wb+")) return svm
def Strukturni(x_train, y_train, x_test, y_test): import itertools import time import numpy as np from scipy import sparse from sklearn.metrics import hamming_loss from sklearn.metrics import accuracy_score from sklearn.metrics import mutual_info_score from scipy.sparse.csgraph import minimum_spanning_tree from pystruct.learners import OneSlackSSVM # from pystruct.learners import FrankWolfeSSVM from pystruct.models import MultiLabelClf from pystruct.models import GraphCRF from sklearn.neural_network import MLPClassifier from sklearn.tree import DecisionTreeClassifier def chow_liu_tree(y_): n_labels = y_.shape[1] mi = np.zeros((n_labels, n_labels)) for i in range(n_labels): for j in range(n_labels): mi[i, j] = mutual_info_score(y_[:, i], y_[:, j]) mst = minimum_spanning_tree(sparse.csr_matrix(-mi)) edges = np.vstack(mst.nonzero()).T edges.sort(axis=1) return edges x_train = x_train.values y_train = y_train.values y_train = y_train.astype(int) y_test = y_test.values y_test = y_test.astype(int) x_test = x_test.values time_ST = np.zeros(7) HL = np.zeros(7) ACC = np.zeros(7) n_labels = y_train.shape[1] full = np.vstack([x for x in itertools.combinations(range(n_labels), 2)]) tree = chow_liu_tree(y_train) """ CRF chain """ train_tree = [] train_full = [] test_tree = [] test_full = [] for k in range(y_train.shape[0]): X_train_CRF = np.zeros([y_train.shape[1], 18]) for i in range(y_train.shape[1]): kolone = np.array([x for x in range(i * 18, 18 * (i + 1))]) X_train_CRF[i, :] = x_train[k, kolone] train_tree.append((X_train_CRF.copy(), tree.T)) train_full.append((X_train_CRF.copy(), full.T)) for k in range(y_test.shape[0]): X_test_CRF = np.zeros([y_test.shape[1], 18]) for i in range(y_test.shape[1]): kolone = np.array([x for x in range(i * 18, 18 * (i + 1))]) X_test_CRF[i, :] = x_test[k, kolone] test_tree.append((X_test_CRF.copy(), tree)) test_full.append((X_test_CRF.copy(), full)) """ SSVM, MLP, CRF-graph, DT - pystruct """ """CREATE DATASET FOR GNN """ """ Define models """ full_model = MultiLabelClf(edges=full) independent_model = MultiLabelClf() tree_model = MultiLabelClf(edges=tree, inference_method='max-product') modelCRF_tree = GraphCRF(directed=False, inference_method="max-product") modelCRF_full = GraphCRF(directed=False, inference_method="max-product") """ Define learn algorithm """ full_ssvm = OneSlackSSVM(full_model, inference_cache=50, C=.1, tol=0.01, max_iter=150) tree_ssvm = OneSlackSSVM(tree_model, inference_cache=50, C=.1, tol=0.01, max_iter=150) independent_ssvm = OneSlackSSVM(independent_model, C=.1, tol=0.01, max_iter=150) MLP = MLPClassifier() DT = DecisionTreeClassifier() CRF_tree = OneSlackSSVM(model=modelCRF_tree, C=.1, max_iter=250) CRF_full = OneSlackSSVM(model=modelCRF_full, C=.1, max_iter=250) """ Fit models """ start_time = time.time() independent_ssvm.fit(x_train, y_train) y_ind = independent_ssvm.predict(x_test) time_ST[0] = time.time() - start_time start_time = time.time() full_ssvm.fit(x_train, y_train) y_full = full_ssvm.predict(x_test) time_ST[1] = time.time() - start_time start_time = time.time() tree_ssvm.fit(x_train, y_train) y_tree = tree_ssvm.predict(x_test) time_ST[2] = time.time() - start_time start_time = time.time() MLP.fit(x_train, y_train) y_MLP = MLP.predict(x_test) time_ST[3] = time.time() - start_time start_time = time.time() DT.fit(x_train, y_train) y_DT = DT.predict(x_test) time_ST[4] = time.time() - start_time start_time = time.time() CRF_tree.fit(train_tree, y_train) yCRF_tree = np.asarray(CRF_tree.predict(test_tree)) time_ST[5] = time.time() - start_time start_time = time.time() CRF_full.fit(train_full, y_train) yCRF_full = np.asarray(CRF_full.predict(test_full)) time_ST[6] = time.time() - start_time """ EVALUATE models """ y_full = np.asarray(y_full) y_ind = np.asarray(y_ind) y_tree = np.asarray(y_tree) HL[0] = hamming_loss(y_test, y_ind) HL[1] = hamming_loss(y_test, y_full) HL[2] = hamming_loss(y_test, y_tree) HL[3] = hamming_loss(y_test, y_MLP) HL[4] = hamming_loss(y_test, y_DT) HL[5] = hamming_loss(y_test, yCRF_tree) HL[6] = hamming_loss(y_test, yCRF_full) y_ind = y_ind.reshape([y_ind.shape[0] * y_ind.shape[1]]) y_full = y_full.reshape([y_full.shape[0] * y_full.shape[1]]) y_tree = y_tree.reshape([y_tree.shape[0] * y_tree.shape[1]]) y_MLP = y_MLP.reshape([y_MLP.shape[0] * y_MLP.shape[1]]) y_DT = y_DT.reshape([y_DT.shape[0] * y_DT.shape[1]]) yCRF_tree = yCRF_tree.reshape([yCRF_tree.shape[0] * yCRF_tree.shape[1]]) yCRF_full = yCRF_full.reshape([yCRF_full.shape[0] * yCRF_full.shape[1]]) y_test = y_test.reshape([y_test.shape[0] * y_test.shape[1]]) ACC[0] = accuracy_score(y_test, y_ind) ACC[1] = accuracy_score(y_test, y_full) ACC[2] = accuracy_score(y_test, y_tree) ACC[3] = accuracy_score(y_test, y_MLP) ACC[4] = accuracy_score(y_test, y_DT) ACC[5] = accuracy_score(y_test, y_MLP) ACC[6] = accuracy_score(y_test, y_DT) return ACC, HL, time_ST
def test_graph_crf_continuous_inference(): for inference_method in ["lp", "ad3"]: crf = GraphCRF(n_states=2, inference_method=inference_method) assert_array_equal(np.argmax(crf.inference((x_1, g_1), w, relaxed=True)[0], axis=-1), y_1) assert_array_equal(np.argmax(crf.inference((x_2, g_2), w, relaxed=True)[0], axis=-1), y_2)
def __init__(self, n_labels=None, n_features=None, n_states_per_label=None, inference_method=None): self.n_labels = n_labels self.n_states_per_label = n_states_per_label GraphCRF.__init__(self, n_states=None, n_features=n_features, inference_method=inference_method)
""" x_test = [] for i in range(0, valid_data.shape[0]): temp = np.zeros((1,2),dtype = int) if clf.predict(valid_data[i]) == 0: temp[0][0] = 1 else: temp[0][1] = 1 x_test.append(temp[0]) X_test = [(x, np.empty((0, 2), dtype=np.int)) for x in x_test] print len(x_test) for i in range(len(test_labels)): test_labels = test_labels.astype(int) """ print len(test_labels) pbl = GraphCRF(inference_method='ad3') svm = NSlackSSVM(pbl, C=1,n_jobs = 1,verbose = 1) start = time() print len(X_valid) print len(valid_Y) svm.fit(X_valid, valid_Y) print "fit finished" time_svm = time() - start print X_test[i][0].shape print svm.score(X_valid,valid_Y) print svm.score(X_test,test_Y) y_pred = np.vstack(svm.predict(np.array(X_valid))) print("Score with pystruct crf svm: %f (took %f seconds)" % (np.mean(y_pred == valid_Y), time_svm)) y_predt = np.vstack(svm.predict(np.array(X_test))) print("Score with pystruct crf svm: %f (took %f seconds)"
def make_random_trees(n_samples=50, n_nodes=100, n_states=7, n_features=10): crf = GraphCRF(inference_method='max-product', n_states=n_states, n_features=n_features) weights = np.random.randn(crf.size_joint_feature) X, y = [], [] for i in range(n_samples): distances = np.random.randn(n_nodes, n_nodes) features = np.random.randn(n_nodes, n_features) tree = minimum_spanning_tree(sparse.csr_matrix(distances)) edges = np.c_[tree.nonzero()] X.append((features, edges)) y.append(crf.inference(X[-1], weights)) return X, y, weights X, y, weights = make_random_trees(n_nodes=1000) X_train, X_test, y_train, y_test = train_test_split(X, y) #tree_model = MultiLabelClf(edges=tree, inference_method=('ogm', {'alg': 'dyn'})) tree_model = GraphCRF(inference_method='max-product') tree_ssvm = SubgradientSSVM(tree_model, max_iter=4, C=1, verbose=10) print("fitting tree model...") tree_ssvm.fit(X_train, y_train) print("Training loss tree model: %f" % tree_ssvm.score(X_train, y_train)) print("Test loss tree model: %f" % tree_ssvm.score(X_test, y_test))
def main(): tweets_data_train = [] with open('Train\dataset_train.pkl', 'rb') as r: tweets_set = pickle.load(r) for i in range(0, len(tweets_set)): for j in range(0, len(tweets_set[i])): t = tweets_set[i][j][1].encode('ascii', 'ignore') tweets_data_train.append(t) features_train_transformed = get_extra_features(tweets_data_train) print(features_train_transformed.shape) features_train_transformed.dump("Train\extra_features_train.pkl") extra_features_train = numpy.load("Train\extra_features_train.pkl") print "EXTRA FEATURES FOR TRAIN DATA IS SUCCESSFULLY EXTRACTED" tweets_data_test = [] with open('Test\dataset_test.pkl', 'rb') as r: tweets_set = pickle.load(r) for i in range(0, len(tweets_set)): for j in range(0, len(tweets_set[i])): t = tweets_set[i][j][1].encode('ascii', 'ignore') tweets_data_test.append(t) features_test_transformed = get_extra_features(tweets_data_test) features_test_transformed.dump("Test\extra_features_test.pkl") extra_features_test = numpy.load("Test\extra_features_test.pkl") print "EXTRA FEATURES FOR TEST DATA IS SUCCESSFULLY EXTRACTED" #TFIDF VECTORIZER features_train_tfidf, features_test_tfidf = get_main_features( tweets_data_train, tweets_data_test) with open('Train\edges_train.pkl', 'rb') as e: edges_train = pickle.load(e) with open('Train\labels_train.pkl', 'rb') as l: labels_tr = pickle.load(l) with open('Test\edges_test.pkl', 'rb') as e: edges_test = pickle.load(e) with open('Test\labels_test.pkl', 'rb') as l: labels_te = pickle.load(l) #edges=numpy.array(edges) labels_tr = numpy.array(labels_tr) labels_te = numpy.array(labels_te) #labels_1D=numpy.zeros(1) labels_train = array_to_list(labels_tr) labels_test = array_to_list(labels_te) labels_test = numpy.array(labels_test) #labels_1D=numpy.delete(labels_1D,(0),0) """ selector=SelectPercentile(f_classif,percentile=70) selector.fit(features_train_tfidf,labels_1D) features_train_transformed=selector.transform(features_train_tfidf).toarray() features_test_transformed=selector.transform(features_test_tfidf).toarray() print "Features Selection is done successfully """ print features_test_tfidf.shape, extra_features_test.shape features_train_transformed = numpy.concatenate( (features_train_tfidf, extra_features_train), axis=1) features_test_transformed = numpy.concatenate( (features_test_tfidf, extra_features_test), axis=1) print "TFIDF FEATURES ARE SUCCESSFULLY CREATED" features_train = get_features_and_edges(features_train_transformed, edges_train) features_test = get_features_and_edges(features_test_transformed, edges_test) labels_train = numpy.array(labels_train) print labels_train.shape model_name = "GraphCRF_model" model = GraphCRF(directed=True) ssvm = FrankWolfeSSVM(model=model, C=1.0, max_iter=100, logger=SaveLogger(model_name + ".pickle", save_every=100)) start_time = time.time() final_model = ssvm.fit(features_train, labels_train) print("--- Time taken to train the classifier is %s seconds " % (time.time() - start_time)) print "YAAY ! A GRAPH CRF MODEL IS SUCCESSFULLY CREATED AND TRAINED" print("Charliehedbo event is the Test Data") pickle.dump(final_model, open('Saved_Model/sdqc_final_model.pkl', 'wb')) ssvm = pickle.load(open('Saved_Model/sdqc_final_model.pkl', 'rb')) #ssvm = SaveLogger(model_name+".pickle").load() X_test = [] y_test = [] for i in range(0, len(features_test)): if features_test[i][0].shape[0] >= 3: X_test.append(features_test[i]) y_test.append(labels_test[i]) #print X_test #print ("Accuracy score with Graph CRF : %f" % ssvm.score(X_test,y_test)) predictions = ssvm.predict(X_test) #PREDICTIONS AND y_TEST ARE LIST OF ARRAYS true = numpy.zeros(1) prediction = numpy.zeros(1) for i in range(0, len(predictions)): true = numpy.hstack((true, y_test[i])) prediction = numpy.hstack((prediction, predictions[i])) true = numpy.delete(true, (0), axis=0) prediction = numpy.delete(prediction, (0), axis=0) print "TOTAL", true.shape[0] print accuracy_score(true, prediction) with open('SDQC_Result.pkl', 'wb') as w: pickle.dump(prediction, w) print( classification_report( true, prediction, target_names=["support", "deny", "query", "comment"])) print confusion_matrix(true, prediction, labels=[0, 1, 2, 3]) plot_cmat(true, prediction)
# Make binary task by doing odd vs even numers. y = y_org % 2 # Make each example into a tuple of a single feature vector and an empty edge # list X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X] Y = y.reshape(-1, 1) X_train_, X_test_, X_train, X_test, y_train, y_test, y_org_train, y_org_test =\ train_test_split(X_, X, Y, y_org, test_size=.5) # First, perform the equivalent of the usual SVM. This is represented as # a CRF problem with no edges. pbl = GraphCRF(inference_method='unary') # We use batch_size=-1 as a binary problem can be solved in one go. svm = NSlackSSVM(pbl, C=1, batch_size=-1) svm.fit(X_train_, y_train) # Now, use a latent-variabile CRF model with SVM training. # 5 states per label is enough capacity to encode the 5 digit classes. latent_pbl = LatentGraphCRF(n_states_per_label=5, inference_method='unary') base_ssvm = NSlackSSVM(latent_pbl, C=1, tol=.01, inactive_threshold=1e-3, batch_size=10) latent_svm = LatentSSVM(base_ssvm=base_ssvm, latent_iter=2)
def __init__(self, n_states=None, n_features=None, inference_method=None, neighborhood=4, class_weight=None): self.neighborhood = neighborhood GraphCRF.__init__(self, n_states=n_states, n_features=n_features, inference_method=inference_method, class_weight=class_weight)