def test_constraint_removal(): digits = load_digits() X, y = digits.data, digits.target y = 2 * (y % 2) - 1 # even vs odd as +1 vs -1 X = X / 16. pbl = BinaryClf(n_features=X.shape[1]) clf_no_removal = OneSlackSSVM(model=pbl, max_iter=500, C=1, inactive_window=0, tol=0.01) clf_no_removal.fit(X, y) clf = OneSlackSSVM(model=pbl, max_iter=500, C=1, tol=0.01, inactive_threshold=1e-8) clf.fit(X, y) # check that we learned something assert_greater(clf.score(X, y), .92) # results are mostly equal # if we decrease tol, they will get more similar assert_less(np.mean(clf.predict(X) != clf_no_removal.predict(X)), 0.02) # without removal, have as many constraints as iterations assert_equal(len(clf_no_removal.objective_curve_), len(clf_no_removal.constraints_)) # with removal, there are less constraints than iterations assert_less(len(clf.constraints_), len(clf.objective_curve_))
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_with_crosses_bad_init(): # use less perfect initialization rnd = np.random.RandomState(0) X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] crf = LatentGridCRF(n_states_per_label=2) crf.initialize(X, Y) H_init = crf.init_latent(X, Y) mask = rnd.uniform(size=H_init.shape) > .7 H_init[mask] = 2 * (H_init[mask] / 2) one_slack_ssvm = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=.0001, inference_cache=50, C=100) clf = LatentSSVM(one_slack_ssvm) clf.fit(X, Y, H_init=H_init) Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(.98 < clf.score(X_test, Y_test) < 1)
def test_one_slack_constraint_caching(): #testing cutting plane ssvm on easy multinomial dataset X, Y = generate_blocks_multinomial(n_samples=10, noise=0.5, seed=0, size_x=9) n_labels = len(np.unique(Y)) crf = GridCRF(n_states=n_labels, inference_method='lp') clf = OneSlackSSVM(model=crf, max_iter=150, C=1, check_constraints=True, break_on_bad=True, inference_cache=50, inactive_window=0, verbose=10) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred) assert_equal(len(clf.inference_cache_), len(X)) # there should be 11 constraints, which are less than the 94 iterations # that are done assert_equal(len(clf.inference_cache_[0]), 11) # check that we didn't change the behavior of how we construct the cache constraints_per_sample = [len(cache) for cache in clf.inference_cache_] assert_equal(np.max(constraints_per_sample), 19) assert_equal(np.min(constraints_per_sample), 11)
def __init__(self, prob_estimator, C_ssvm=1., inference='ad3', inference_cache=50, tol=1., max_iter=200, n_jobs=1): """ Called when initializing the classifier """ #self.C_logreg = C_logreg self.C_ssvm = C_ssvm self.inference = inference self.inference_cache = inference_cache self.tol = tol self.max_iter = max_iter self.n_jobs = n_jobs self.prob_estimator = prob_estimator self.crf = EdgeFeatureGraphCRF(inference_method=inference) self.ssvm = OneSlackSSVM(self.crf, inference_cache=inference_cache, C=C_ssvm, tol=tol, max_iter=max_iter, n_jobs=n_jobs)
def do_comparison(X_train, y_train, X_test, y_test, dataset): # evaluate both svms on a given datasets, generate plots Cs = 10.**np.arange(-4, 1) multisvm = MultiSVM() svm = OneSlackSSVM(MultiClassClf(), tol=0.01) accs_pystruct, times_pystruct = eval_on_data(X_train, y_train, X_test, y_test, svm, Cs=Cs) accs_svmstruct, times_svmstruct = eval_on_data(X_train, y_train, X_test, y_test, multisvm, Cs=Cs) plot_curves(times_svmstruct, times_pystruct, Cs=Cs, title="learning time (s) %s" % dataset, filename="times_%s.pdf" % dataset) plot_curves(accs_svmstruct, accs_pystruct, Cs=Cs, title="accuracy %s" % dataset, filename="accs_%s.pdf" % dataset)
def msrc(): models_basedir = 'models/msrc/' crf = EdgeCRF(n_states=24, n_features=2028, n_edge_features=4, inference_method='gco') clf = OneSlackSSVM(crf, max_iter=10000, C=0.01, verbose=2, tol=0.1, n_jobs=4, inference_cache=100) X, Y = load_msrc('train') Y = remove_areas(Y) start = time() clf.fit(X, Y) stop = time() np.savetxt(models_basedir + 'msrc_full.csv', clf.w) with open(models_basedir + 'msrc_full' + '.pickle', 'w') as f: cPickle.dump(clf, f) X, Y = load_msrc('test') Y = remove_areas(Y) Y_pred = clf.predict(X) print 'Error on test set: %f' % compute_error(Y, Y_pred) print 'Score on test set: %f' % clf.score(X, Y) print 'Norm of weight vector: |w|=%f' % np.linalg.norm(clf.w) print 'Elapsed time: %f s' % (stop - start) return clf
def test_class_weights_rescale_C(): # check that our crammer-singer implementation with class weights and # rescale_C=True is the same as LinearSVC's c-s class_weight implementation raise SkipTest("class weight test needs update") from sklearn.svm import LinearSVC 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] weights = len(Y) / (np.bincount(Y) * len(np.unique(Y))) pbl_class_weight = MultiClassClf(n_features=3, n_classes=3, class_weight=weights, rescale_C=True) svm_class_weight = OneSlackSSVM(pbl_class_weight, C=10, tol=1e-5) svm_class_weight.fit(X, Y) try: linearsvm = LinearSVC(multi_class='crammer_singer', fit_intercept=False, class_weight='balanced', C=10) linearsvm.fit(X, Y) assert_array_almost_equal(svm_class_weight.w, linearsvm.coef_.ravel(), 3) except TypeError: # travis has a really old sklearn version that doesn't support # class_weight in LinearSVC pass
def syntetic(): # train model on a single set models_basedir = 'models/syntetic/' crf = EdgeCRF(n_states=10, n_features=10, n_edge_features=2, inference_method='gco') clf = OneSlackSSVM(crf, max_iter=10000, C=0.01, verbose=2, tol=0.1, n_jobs=4, inference_cache=100) X, Y = load_syntetic(1) x_train, x_test, y_train, y_test = train_test_split(X, Y, train_size=100, random_state=179) start = time() clf.fit(x_train, y_train) stop = time() np.savetxt(models_basedir + 'syntetic_full.csv', clf.w) with open(models_basedir + 'syntetic_full' + '.pickle', 'w') as f: cPickle.dump(clf, f) y_pred = clf.predict(x_test) print 'Error on test set: %f' % compute_error(y_test, y_pred) print 'Score on test set: %f' % clf.score(x_test, y_test) print 'Score on train set: %f' % clf.score(x_train, y_train) print 'Norm of weight vector: |w|=%f' % np.linalg.norm(clf.w) print 'Elapsed time: %f s' % (stop - start) return clf
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_latent_node_boxes_edge_features(): # learn the "easy" 2x2 boxes dataset. # smoketest using a single constant edge feature X, Y = make_simple_2x2(seed=1, n_samples=40) latent_crf = EdgeFeatureLatentNodeCRF(n_labels=2, n_hidden_states=2, n_features=1) base_svm = OneSlackSSVM(latent_crf) base_svm.C = 10 latent_svm = LatentSSVM(base_svm, latent_iter=10) G = [make_grid_edges(x) for x in X] # make edges for hidden states: edges = make_edges_2x2() G = [np.vstack([make_grid_edges(x), edges]) for x in X] # reshape / flatten x and y X_flat = [x.reshape(-1, 1) for x in X] Y_flat = [y.ravel() for y in Y] #X_ = zip(X_flat, G, [2 * 2 for x in X_flat]) # add edge features X_ = [(x, g, np.ones((len(g), 1)), 4) for x, g in zip(X_flat, G)] latent_svm.fit(X_[:20], Y_flat[:20]) assert_array_equal(latent_svm.predict(X_[:20]), Y_flat[:20]) assert_equal(latent_svm.score(X_[:20], Y_flat[:20]), 1) # test that score is not always 1 assert_true(.98 < latent_svm.score(X_[20:], Y_flat[20:]) < 1)
def syntetic_test(): # test model on different train set size & on different train sets results = np.zeros((18, 5)) full_labeled = np.array([2, 4, 10, 25, 100]) train_size = 400 for dataset in xrange(1, 19): X, Y = load_syntetic(dataset) for j, nfull in enumerate(full_labeled): crf = EdgeCRF(n_states=10, n_features=10, n_edge_features=2, inference_method='qpbo') clf = OneSlackSSVM(crf, max_iter=10000, C=0.01, verbose=0, tol=0.1, n_jobs=4, inference_cache=100) x_train = X[:nfull] y_train = Y[:nfull] x_test = X[(train_size + 1):] y_test = Y[(train_size + 1):] try: clf.fit(x_train, y_train) y_pred = clf.predict(x_test) results[dataset - 1, j] = compute_error(y_test, y_pred) print 'dataset=%d, nfull=%d, error=%f' % (dataset, nfull, results[dataset - 1, j]) except ValueError: print 'dataset=%d, nfull=%d: Failed' % (dataset, nfull) np.savetxt('results/syntetic/full_labeled.txt', results)
def ssvm_classifier() : x_train,y_train,x_test,y_test = load_data1() print "Data Loaded" pca = PCA(n_components= 1000) x_train_reduced = pca.fit_transform(x_train) x_test_reduced = pca.fit_transform(x_test) print "PCA finished" print "Learning the model" 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) independent_model = MultiLabelClf(inference_method='unary') independent_ssvm = OneSlackSSVM(independent_model, C=.1, tol=0.01) independent_ssvm.fit(x_train_reduced, y_train) print "saving model ..." with open("data/independent_ssvm.pkl","wb+") as f : cp.dump(independent_ssvm,f) #print "Calculatin the cross-validation scores" #scores = model_selection.cross_val_score(independent_ssvm,x_train_reduced,y_train,cv=3) print independent_ssvm.score(x_test_reduced,y_test)
def train(x_train, y_train, x_test, y_test): x_train = np.asarray(x_train, dtype=np.float) y_train = np.asarray(y_train, dtype=np.int64) # x_test = np.asarray(x_test, dtype=np.float) # y_test = np.asarray(y_test, dtype=np.int64) x_test = x_train y_test = y_train from pystruct.learners import NSlackSSVM, OneSlackSSVM, SubgradientSSVM, LatentSSVM, SubgradientLatentSSVM, PrimalDSStructuredSVM from pystruct.models import MultiLabelClf, MultiClassClf clf = OneSlackSSVM(MultiLabelClf(), C=1, show_loss_every=1, verbose=1, max_iter=1000) # print(x_train, y_train) # input() clf.fit(x_train, y_train) result = clf.predict(x_test) print('Result: \n', result) print('True label:\n', y_test) clf.score(x_test, y_test) print('\n') count = 0 for i in range(len(result)): # print(np.sum(np.square(y_test[i]-result[i]))) if np.sum(np.square(y_test[i] - result[i])) != 0: print('True label: ', y_test[i], 'Predict: ', result[i]) count += 1 print(count) translate_vector(x_test, y_test)
def test_latent_node_boxes_standard_latent(): # learn the "easy" 2x2 boxes dataset. # a 2x2 box is placed randomly in a 4x4 grid # we add a latent variable for each 2x2 patch # that should make the model fairly simple X, Y = make_simple_2x2(seed=1, n_samples=40) latent_crf = LatentNodeCRF(n_labels=2, n_hidden_states=2, n_features=1) one_slack = OneSlackSSVM(latent_crf) n_slack = NSlackSSVM(latent_crf) subgradient = SubgradientSSVM(latent_crf, max_iter=100) for base_svm in [one_slack, n_slack, subgradient]: base_svm.C = 10 latent_svm = LatentSSVM(base_svm, latent_iter=10) G = [make_grid_edges(x) for x in X] # make edges for hidden states: edges = make_edges_2x2() G = [np.vstack([make_grid_edges(x), edges]) for x in X] # reshape / flatten x and y X_flat = [x.reshape(-1, 1) for x in X] Y_flat = [y.ravel() for y in Y] X_ = zip(X_flat, G, [2 * 2 for x in X_flat]) latent_svm.fit(X_[:20], Y_flat[:20]) assert_array_equal(latent_svm.predict(X_[:20]), Y_flat[:20]) assert_equal(latent_svm.score(X_[:20], Y_flat[:20]), 1) # test that score is not always 1 assert_true(.98 < latent_svm.score(X_[20:], Y_flat[20:]) < 1)
def test_one_slack_constraint_caching(): # testing cutting plane ssvm on easy multinomial dataset X, Y = generate_blocks_multinomial(n_samples=10, noise=0.5, seed=0, size_x=9) n_labels = len(np.unique(Y)) exact_inference = get_installed([('ad3', {'branch_and_bound': True}), "lp"])[0] crf = GridCRF(n_states=n_labels, inference_method=exact_inference) clf = OneSlackSSVM(model=crf, max_iter=150, C=1, check_constraints=True, break_on_bad=True, inference_cache=50, inactive_window=0) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred) assert_equal(len(clf.inference_cache_), len(X)) # there should be 13 constraints, which are less than the 94 iterations # that are done # check that we didn't change the behavior of how we construct the cache constraints_per_sample = [len(cache) for cache in clf.inference_cache_] if exact_inference == "lp": assert_equal(len(clf.inference_cache_[0]), 18) assert_equal(np.max(constraints_per_sample), 18) assert_equal(np.min(constraints_per_sample), 18) else: assert_equal(len(clf.inference_cache_[0]), 13) assert_equal(np.max(constraints_per_sample), 20) assert_equal(np.min(constraints_per_sample), 11)
def CRF_pred(eeg1, eeg2, emg, y, eeg1test, eeg2test, emgtest, C=0.9, weight_shift=0, max_iter=1000, fs=128): # For the ith iteration, select as trainin the sub_indices other than those at index i for train_index eeg1_train = eeg1.values eeg2_train = eeg2.values emg_train = emg.values y_train = y.values # The test subject is the one at index i eeg1_test = eeg1test.values eeg2_test = eeg2test.values emg_test = emgtest.values # CRF Model Preprocessing eeg1_ = process_EEG(eeg1_train) eeg2_ = process_EEG(eeg2_train) emg_ = process_EMG(emg_train) xtrain_ = np.concatenate((eeg1_, eeg2_, emg_), axis=1) ytrain_classes = np.reshape(y_train, (y_train.shape[0], )) ytrain_ = y_train eeg1_ = process_EEG(eeg1_test) eeg2_ = process_EEG(eeg2_test) emg_ = process_EMG(emg_test) xtest_ = np.concatenate((eeg1_, eeg2_, emg_), axis=1) xtrain_crf = np.reshape( xtrain_, (3, -1, xtrain_.shape[1])) # Reshape so that it works with CRF ytrain_crf = np.reshape(ytrain_, (3, -1)) - 1 # Reshape so that it works with CRF # CRF Model fitting: classes = np.unique(ytrain_) weights_crf = compute_class_weight("balanced", list(classes), list(ytrain_classes)) weights_crf[0] = weights_crf[0] + (2.5 * weight_shift) weights_crf[1] = weights_crf[1] + (1.5 * weight_shift) model = ChainCRF(class_weight=weights_crf) ssvm = OneSlackSSVM(model=model, C=C, max_iter=max_iter) ssvm.fit(xtrain_crf, ytrain_crf) # Test on the third guy xtest_crf = np.reshape(xtest_, (2, -1, xtest_.shape[1])) y_pred_crf = ssvm.predict(xtest_crf) y_pred_crf = np.asarray(y_pred_crf).reshape(-1) + 1 return y_pred_crf
def losocv_CRF_prepro(xtrain, y, C=0.5, weight_shift=0, max_iter=1000, fs=128): """Leave one subject out cross validation for the CRF model becasuse it requires special datahandling. Input should be a Pandas Dataframe.""" epochs = 21600 num_sub = 3 # Indices of the subjects sub_indices = [ np.arange(0, epochs), np.arange(epochs, epochs * 2), np.arange(epochs * 2, epochs * 3) ] res = [] for i in range(len(sub_indices)): # For the ith iteration, select as trainin the sub_indices other than those at index i for train_index train_index = np.concatenate( [sub_indices[(i + 1) % num_sub], sub_indices[(i + 2) % num_sub]]) xtrain_ = xtrain[train_index] y_train = y.values[train_index] ytrain_ = y_train # The test subject is the one at index i test_index = sub_indices[i] xtest_ = xtrain[test_index] y_test = y.values[test_index] ytest_ = y_test # CRF Model Preprocessing ytrain_classes = np.reshape(y_train, (y_train.shape[0], )) xtrain_crf = np.reshape( xtrain_, (2, -1, xtrain_.shape[1])) # Reshape so that it works with CRF ytrain_crf = np.reshape( ytrain_, (2, -1)) - 1 # Reshape so that it works with CRF # CRF Model fitting: classes = np.unique(ytrain_) weights_crf = compute_class_weight("balanced", list(classes), list(ytrain_classes)) weights_crf[0] = weights_crf[0] + (2.5 * weight_shift) weights_crf[1] = weights_crf[1] + (1.5 * weight_shift) model = ChainCRF(class_weight=weights_crf) ssvm = OneSlackSSVM(model=model, C=C, max_iter=max_iter) ssvm.fit(xtrain_crf, ytrain_crf) # Test on the third guy xtest_crf = np.reshape(xtest_, (1, -1, xtest_.shape[1])) ytest_crf = np.reshape(ytest_, (1, -1)) - 1 y_pred_crf = ssvm.predict(xtest_crf) y_pred_crf = np.asarray(y_pred_crf).reshape(-1) + 1 resy = sklearn.metrics.balanced_accuracy_score(ytest_, y_pred_crf) print("Iteration, result:", i, resy) res.append(resy) return res
def __init__(self): # the model self.model = ChainCRF(directed=True) # the learner self.learner = OneSlackSSVM(model=self.model, C=.1, inference_cache=50, tol=0.1, n_jobs=1)
def train(self, trajid_list, n_jobs=4): if self.poi_info is None: self.poi_info = self.dat_obj.calc_poi_info(trajid_list) # build POI_ID <--> POI__INDEX mapping for POIs used to train CRF # which means only POIs in traj such that len(traj) >= 2 are included poi_set = {p for tid in trajid_list for p in self.dat_obj.traj_dict[tid] if len(self.dat_obj.traj_dict[tid]) >= 2} self.poi_list = sorted(poi_set) self.poi_id_dict, self.poi_id_rdict = dict(), dict() for idx, poi in enumerate(self.poi_list): self.poi_id_dict[poi] = idx self.poi_id_rdict[idx] = poi # generate training data train_traj_list = [self.dat_obj.traj_dict[k] for k in trajid_list if len(self.dat_obj.traj_dict[k]) >= 2] node_features_list = Parallel(n_jobs=n_jobs)(delayed(calc_node_features)( tr[0], len(tr), self.poi_list, self.poi_info, self.dat_obj) for tr in train_traj_list) edge_features = calc_edge_features(trajid_list, self.poi_list, self.poi_info, self.dat_obj) # feature scaling: node features # should each example be flattened to one vector before scaling? self.fdim_node = node_features_list[0].shape X_node_all = np.vstack(node_features_list) X_node_all = self.scaler_node.fit_transform(X_node_all) X_node_all = X_node_all.reshape(-1, self.fdim_node[0], self.fdim_node[1]) # feature scaling: edge features fdim_edge = edge_features.shape edge_features = self.scaler_edge.fit_transform(edge_features.reshape(fdim_edge[0] * fdim_edge[1], -1)) self.edge_features = edge_features.reshape(fdim_edge) assert(len(train_traj_list) == X_node_all.shape[0]) X_train = [(X_node_all[k, :, :], self.edge_features.copy(), (self.poi_id_dict[train_traj_list[k][0]], len(train_traj_list[k]))) for k in range(len(train_traj_list))] y_train = [np.array([self.poi_id_dict[k] for k in tr]) for tr in train_traj_list] assert(len(X_train) == len(y_train)) # train sm = MyModel(inference_train=self.inference_train, inference_pred=self.inference_pred, share_params=self.share_params, multi_label=self.multi_label) if self.debug is True: print('C:', self.C) verbose = 1 if self.debug is True else 0 self.osssvm = OneSlackSSVM(model=sm, C=self.C, n_jobs=n_jobs, verbose=verbose) try: self.osssvm.fit(X_train, y_train, initialize=True) self.trained = True print('SSVM training finished.') # except ValueError: except: self.trained = False sys.stderr.write('SSVM training FAILED.\n') # raise return self.trained
def model_test(k, head, tail): """ CRF训练和预测 """ each_fold_time = time.time() #开始计时 #divide train set and test set train_id = dataId[head:tail] test_id = dataId[:head] + dataId[tail:] X_train = X_arr[train_id, :] Y_train = Y_arr[train_id] X_test = X_arr[test_id, :] Y_test = Y_arr[test_id] campTest = Camp_arr[test_id] #ends divide train set and test set if len(X_train) > 0: #实例化CRF EFGCRF = EdgeFeatureGraphCRF(inference_method='qpbo', class_weight=CLASS_WEIGHT) if LEARNER == "OneSlackSSVM": #利用OneSlackSSVM训练模型参数 ssvm = OneSlackSSVM(EFGCRF, C=.1, tol=.1, max_iter=100, switch_to='ad3') elif LEARNER == "FrankWolfeSSVM": #利用FrankWolfeSSVM训练模型参数 ssvm = FrankWolfeSSVM(EFGCRF, C=.1, tol=.1, max_iter=100) else: #没有选择分类器退出 pass ssvm.fit(X_train, Y_train) Y_pred = ssvm.predict(X_test) df_result = statistic_result(Y_pred, Y_test, campTest) V_precision = precision_score(df_result["label"], df_result["pred"]) V_recall = recall_score(df_result["label"], df_result["pred"]) V_f1 = f1_score(df_result["label"], df_result["pred"]) camps_pred, camps_lbl = statistic_campaign_result(Y_pred, Y_test) C_precision = precision_score(camps_lbl, camps_pred) C_recall = recall_score(camps_lbl, camps_pred) C_f1 = f1_score(camps_lbl, camps_pred) result_Queue.put( [V_precision, V_recall, V_f1, C_precision, C_recall, C_f1]) else: print("TRAIN SET is NULL") print("the {}th fold using time: {:.4f} min".format( k + 1, (time.time() - each_fold_time) / 60)) del X_train, Y_train, X_test, Y_test, Y_pred, campTest
def test_switch_to_ad3(): # test if switching between qpbo and ad3 works if not get_installed(['qpbo']) or not get_installed(['ad3']): return X, Y = generate_blocks_multinomial(n_samples=5, noise=1.5, seed=0) crf = GridCRF(n_states=3, inference_method='qpbo') ssvm = OneSlackSSVM(crf, inference_cache=50, max_iter=10000) ssvm_with_switch = OneSlackSSVM(crf, inference_cache=50, max_iter=10000, switch_to=('ad3')) ssvm.fit(X, Y) ssvm_with_switch.fit(X, Y) assert_equal(ssvm_with_switch.model.inference_method, 'ad3') # we check that the dual is higher with ad3 inference # as it might use the relaxation, that is pretty much guraranteed assert_greater(ssvm_with_switch.objective_curve_[-1], ssvm.objective_curve_[-1])
def train(X,y): X_train_directions = make_directions(X) Y_train_flat = [y.ravel()] inference = 'qpbo' # first, train on X with directions only: crf = EdgeFeatureGraphCRF(inference_method=inference) ssvm = OneSlackSSVM(crf, inference_cache=50, C=.1, tol=.1, max_iter=10, n_jobs=1,show_loss_every=1) ssvm.fit(X_train_directions, Y_train_flat, warm_start=False) return ssvm
def train_seq(X, y, crf_params): X_ = [X[k] for k in sorted(X.keys())] y_ = [y[k] for k in sorted(y.keys())] class_sizes = np.bincount(np.hstack(y_)) cw = 1. / class_sizes cw = cw / cw.sum() return OneSlackSSVM(model=ChainCRF(inference_method='max-product', class_weight=cw), max_iter=100000, verbose=False, **crf_params).fit(X_, y_)
def test_multinomial_blocks_one_slack(): #testing cutting plane ssvm on easy multinomial dataset X, Y = generate_blocks_multinomial(n_samples=10, noise=0.5, seed=0) n_labels = len(np.unique(Y)) crf = GridCRF(n_states=n_labels, inference_method=inference_method) clf = OneSlackSSVM(model=crf, max_iter=150, C=1, check_constraints=True, break_on_bad=True, tol=.1, inference_cache=50) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred)
def test_equal_class_weights(): # test that equal class weight is the same as no class weight X, Y = make_blobs(n_samples=80, centers=3, random_state=42) 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:] pbl = MultiClassClf(n_features=3, n_classes=3) svm = OneSlackSSVM(pbl, C=10) svm.fit(X_train, Y_train) predict_no_class_weight = svm.predict(X_test) pbl_class_weight = MultiClassClf(n_features=3, n_classes=3, class_weight=np.ones(3)) svm_class_weight = OneSlackSSVM(pbl_class_weight, C=10) svm_class_weight.fit(X_train, Y_train) predict_class_weight = svm_class_weight.predict(X_test) assert_array_equal(predict_no_class_weight, predict_class_weight) assert_array_almost_equal(svm.w, svm_class_weight.w)
def test_one_slack_attractive_potentials(): # test that submodular SSVM can learn the block dataset X, Y = generate_blocks(n_samples=10) crf = GridCRF(inference_method=inference_method) submodular_clf = OneSlackSSVM(model=crf, max_iter=200, C=1, check_constraints=True, negativity_constraint=[5], inference_cache=50) submodular_clf.fit(X, Y) Y_pred = submodular_clf.predict(X) assert_array_equal(Y, Y_pred) assert_true(submodular_clf.w[5] < 0)
def test_blobs_2d_one_slack(): # make two gaussian blobs 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:] pbl = MultiClassClf(n_features=3, n_classes=3) svm = OneSlackSSVM(pbl, check_constraints=True, C=1000) svm.fit(X_train, Y_train) assert_array_equal(Y_test, np.hstack(svm.predict(X_test)))
def test_one_slack_repellent_potentials(): # test non-submodular problem with and without submodularity constraint # dataset is checkerboard X, Y = generate_checker() crf = GridCRF(inference_method=inference_method) clf = OneSlackSSVM(model=crf, max_iter=10, C=.01, check_constraints=True) clf.fit(X, Y) Y_pred = clf.predict(X) # standard crf can predict perfectly assert_array_equal(Y, Y_pred) submodular_clf = OneSlackSSVM(model=crf, max_iter=10, C=.01, check_constraints=True, negativity_constraint=[4, 5, 6]) submodular_clf.fit(X, Y) Y_pred = submodular_clf.predict(X) assert_less(submodular_clf.score(X, Y), .99) # submodular crf can not do better than unaries for i, x in enumerate(X): y_pred_unaries = crf.inference(x, np.array([1, 0, 0, 1, 0, 0, 0])) assert_array_equal(y_pred_unaries, Y_pred[i])
def test_switch_to_ad3(): # smoketest only # test if switching between qpbo and ad3 works inside latent svm # use less perfect initialization if not get_installed(['qpbo']) or not get_installed(['ad3']): return X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8) X_test, Y_test = X[10:], Y[10:] X, Y = X[:10], Y[:10] crf = LatentGridCRF(n_states_per_label=2, inference_method='qpbo') crf.initialize(X, Y) H_init = crf.init_latent(X, Y) np.random.seed(0) mask = np.random.uniform(size=H_init.shape) > .7 H_init[mask] = 2 * (H_init[mask] / 2) base_ssvm = OneSlackSSVM(crf, inactive_threshold=1e-8, cache_tol=.0001, inference_cache=50, max_iter=10000, switch_to=('ad3', { 'branch_and_bound': True }), C=10.**3) clf = LatentSSVM(base_ssvm) # evil hackery to get rid of ad3 output try: devnull = open('/dev/null', 'w') oldstdout_fno = os.dup(sys.stdout.fileno()) os.dup2(devnull.fileno(), 1) replaced_stdout = True except: replaced_stdout = False clf.fit(X, Y, H_init=H_init) if replaced_stdout: os.dup2(oldstdout_fno, 1) assert_equal(clf.model.inference_method[0], 'ad3') Y_pred = clf.predict(X) assert_array_equal(np.array(Y_pred), Y) # test that score is not always 1 assert_true(.98 < clf.score(X_test, Y_test) < 1)