def create_crf(self): """ :return: """ # to load nltk tagger, a time consuming, one time needed operation self.nltk_tagger = nltk.tag._get_tagger() self.crf = FrankWolfeSSVM(model=ChainCRF(), C=1.0, max_iter=50) self.X, self.y, self.label_code, self.folds, generate_fold = self.load_training_data( ) score = 0 # only need to iterate through if fold was generated num_tries = 10 if generate_fold else 1 while (score <= 0.90) and (num_tries > 0): try: X_train, y_train = self.get_train_data() self.train(X_train, y_train) X_test, y_test = self.get_test_data() score = self.evaluate(X_test, y_test) except Exception as e: current_app.logger.error('Exception: %s' % (str(e))) current_app.logger.error(traceback.format_exc()) pass num_tries -= 1 return (score > 0)
def train_SSVM(X_train, y_train): #print X_train.shape, X_train[0].shape # splitting the 8 sub-arrays into further: #X_train = np.concatenate([np.array_split(x, 100) for x in X_train]) #y_train = np.concatenate([np.array_split(y, 100) for y in y_train]) #X_test = np.concatenate([np.array_split(x, 30) for x in X_test]) #y_test = np.concatenate([np.array_split(y, 30) for y in y_test]) #print X_train.shape #print X_train[0].shape #print y_train[0].shape #exit() #Train using linear chain CRF #https://groups.google.com/forum/#!topic/pystruct/KIkF7fzCyDI model = ChainCRF() #ssvm = NSlackSSVM(model=model, C=.1, max_iter=11) # almost similar to FrankWolfeSSVM ssvm = FrankWolfeSSVM(model=model, C=0.001, max_iter=11) # c=0.2 -> 62.86 % accuracy <==> c=0.1 #ssvm = OneSlackSSVM(model=model) #doesn't work as well ssvm.fit(X_train, y_train) print "Learning complete..." return ssvm
def __init__(self): self.classifierMNB = Pipeline([ #Multinomial Naive Bayes ('extract', ExtractFeatures()), #('encoding', MultiColumnLabelEncoder()), ('clf', MultinomialNB(alpha=0.5)) ]) # self.classifierMaxEnt = Pipeline([ # ('extract', ExtractFeatures()), # #('encoding', MultiColumnLabelEncoder()), # ('clf', nltk.maxent.MaxentClassifier.train(x, algorithm = 'gis', trace = 0, max_iter = 10)) # ]) self.classifierMaxEnt_LogReg = Pipeline([ #Maximum Entropy ('extract', ExtractFeatures()), ('clf', linear_model.LogisticRegression()) ]) self.classifierCRF = Pipeline([ #CRF ('extract', ExtractFeaturesToArray()), ('clf', FrankWolfeSSVM(model=ChainCRF(), C=2, max_iter=10, tol=0.01)) ]) self.classifierSVM = Pipeline([ #Support Vector Machine ('extract', ExtractFeatures()), ('clf', svm.LinearSVC()) ]) pass
def pick_best_C_value(train_sentences, sentence_labels, test_SF, test_sentences, test_sentence_labels): i = 0.10 best_C = i f_old = 0 for z in range(1, 20): print "----------------- Training on C-value %f" % i modelCRF = ChainCRF() ssvm = FrankWolfeSSVM(model=modelCRF, C=i, max_iter=20, random_state=5) ssvm.fit(train_sentences, sentence_labels) print "\n" print "-------- Training complete --------" predictions = ssvm.predict(test_sentences) test_SF['predicted_labels'] = predictions #Saving model print "Saving model...." pickle.dump(ssvm, open('models/ote/otemodel.sav', 'wb')) #Evaluating Trained CRF model p, r, f1, common, retrieved, relevant = evaluating_ote(test_SF) if (f1 >= f_old): #save value of 'C' f_old = f1 best_C = i i = i + 0.05 return best_C
def train(trainSetX, trainSetY, testSetX, testSetY): modelLogger = SaveLogger('imagesegmentation-horse-hog_96_lbp_test.model', save_every=1) # Load trained CRF model print 'Loading trained model for CRF' #clf = modelLogger.load() # Uncomment if we want to train from scratch first layer CRF print 'Training CRF...' start_time = time.time() crf = EdgeFeatureGraphCRF() #antisymmetric_edge_features=[1,2] clf = FrankWolfeSSVM(model=crf, C=10., tol=.1, verbose=3, show_loss_every=1, logger=modelLogger) # #max_iter=50 ##clf = OneSlackSSVM(model=crf, verbose=1, show_loss_every=1, logger=modelLogger) clf.fit(numpy.array(trainSetX), numpy.array(trainSetY)) print 'Training CRF took ' + str(time.time() - start_time) + ' seconds' #print("Overall super pixelwise accuracy (training set): %f" % clf.score(numpy.array(trainSetX), numpy.array(trainSetY) )) #print("Overall super pixelwise accuracy (test set): %f" % clf.score(numpy.array(testSetX), numpy.array(testSetY) )) print 'SUPERPIXELWISE ACCURACY' print '-----------------------------------------------------------------------' print '' print 'TRAINING SET RESULTS' train_ypred = evaluatePerformance(clf, numpy.array(trainSetX), numpy.array(trainSetY)) print '' print 'TEST SET RESULTS' evaluatePerformance(clf, numpy.array(testSetX), numpy.array(testSetY)) print '-----------------------------------------------------------------------'
def fit_predict(train_docs, test_docs, dataset, C, class_weight, constraints, compat_features, second_order, coparents, grandparents, siblings, exact_test=False): stats = stats_train(train_docs) prop_vect, _ = prop_vectorizer(train_docs, which=dataset, stats=stats, n_most_common_tok=None, n_most_common_dep=2000, return_transf=True) link_vect = link_vectorizer(train_docs, stats, n_most_common=500) sec_ord_vect = (second_order_vectorizer(train_docs) if second_order else None) _, _, _, pmi_in, pmi_out = stats def _transform_x_y(docs): X = [ _vectorize(doc, pmi_in, pmi_out, prop_vect, link_vect, sec_ord_vect) for doc in docs ] Y = [doc.label for doc in docs] return X, Y X_tr, Y_tr = _transform_x_y(train_docs) X_te, Y_te = _transform_x_y(test_docs) model = ArgumentGraphCRF(class_weight=class_weight, constraints=constraints, compat_features=compat_features, coparents=coparents, grandparents=grandparents, siblings=siblings) clf = FrankWolfeSSVM(model, C=C, random_state=0, verbose=1, check_dual_every=25, show_loss_every=25, max_iter=100, tol=0) clf.fit(X_tr, Y_tr) if exact_test: clf.model.exact = True Y_pred = clf.predict(X_te) return clf, Y_te, Y_pred
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])]) edge_features = np.vstack([edge_features, np.array([[0]])]) 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])]) edge_features = np.vstack([edge_features, np.array([[1]])]) cnt += 1 total_datas.append( (features, edge_nodes.astype(int), edge_features)) 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 = EdgeFeatureGraphCRF(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 chain_crf(): letters = load_letters() x, y, folds = letters['data'], letters['labels'], letters['folds'] print "Letters : " print letters # print "Data : " # print letters['data'] # print "Labels : " # print letters['labels'] x, y = np.array(x), np.array(y) x_train, x_test = x[folds == 1], x[folds != 1] y_train, y_test = y[folds == 1], y[folds != 1] print len(x_train) print len(x_test) print "Done" print x_train[0].shape print y_train[0].shape print x_train[10].shape print y_train[10].shape model = ChainCRF() ssvm = FrankWolfeSSVM(model=model, C=.1, max_iter=10) print ssvm.fit(x_train, y_train) print ssvm.score(x_test, y_test)
def test_multinomial_blocks_frankwolfe(): X, Y = generate_blocks_multinomial(n_samples=10, noise=0.5, seed=0) crf = GridCRF(inference_method='qpbo') clf = FrankWolfeSSVM(model=crf, C=1, max_iter=50, verbose=3) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred)
def __init__(self, c_value, classifier_name='ChainCRF'): self.c_value = c_value self.classifier_name = classifier_name if self.classifier_name == 'ChainCRF': model = ChainCRF() self.clf = FrankWolfeSSVM(model=model, C=self.c_value, max_iter=50) else: raise TypeError('Invalid classifier type')
def scope_trainer(sentence_dicts): scope_instances, scope_labels, sentence_splits = extract_features_scope(sentence_dicts, 'training') scope_vec = DictVectorizer() fvs = scope_vec.fit_transform(scope_instances).toarray() X_train, y_train = split_data(fvs, scope_labels, sentence_splits) scope_ssvm = FrankWolfeSSVM(model=ChainCRF(), C=0.20, max_iter=10) scope_ssvm.fit(X_train, y_train) return scope_ssvm, scope_vec
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 main(): parser = argparse.ArgumentParser( description="learn to segment and tokenize (really, any labeling)", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--untokfile", "-u", nargs='?', type=argparse.FileType('r'), default=sys.stdin, help="untok file") parser.add_argument( "--biofile", "-b", nargs='?', type=argparse.FileType('r'), default=sys.stdin, help="bio file. must match untok file and be space separated") parser.add_argument("--outfile", "-o", nargs='?', type=argparse.FileType('wb'), default=None, help="output file") parser.add_argument("--debug", "-d", action='store_true', default=False, help="debug mode") try: args = parser.parse_args() except IOError as msg: parser.error(str(msg)) untokfile = prepfile(args.untokfile, 'r') biofile = prepfile(args.biofile, 'r') data, labels, datamap, labelmap = prepdata(untokfile, biofile, args.debug) # print(data) # print(labels) model = ChainCRF() #ssvm = SubgradientSSVM(model=model, C=.1)#, show_loss_every=5) ssvm = FrankWolfeSSVM(model=model, max_iter=100, C=.1) #, show_loss_every=5) ssvm.fit(data, labels) # curve = ssvm.loss_curve_ # TONT # print("TONT score with chain CRF: %f" % ssvm.score(data, labels)) ret = {} ret['model'] = ssvm ret['feats'] = datamap ret['labels'] = labelmap if args.outfile is not None: pickle.dump(ret, args.outfile)
def train_scope_learner(sentence_dicts, C_value): scope_sentence_dicts, scope_instances, scope_labels, sentence_splits = extract_features_scope( sentence_dicts, 'training') vectorizer = DictVectorizer() fvs = vectorizer.fit_transform(scope_instances).toarray() X_train, y_train = make_splits(fvs, scope_labels, sentence_splits) model = ChainCRF() scope_ssvm = FrankWolfeSSVM(model=model, C=C_value, max_iter=10) scope_ssvm.fit(X_train, y_train) return scope_ssvm, vectorizer
def test_multinomial_blocks_frankwolfe(): X, Y = generate_blocks_multinomial(n_samples=50, noise=0.5, seed=0) crf = GridCRF(inference_method='qpbo') clf = FrankWolfeSSVM(model=crf, C=1, line_search=True, batch_mode=False, check_dual_every=500) clf.fit(X, Y) Y_pred = clf.predict(X) assert_array_equal(Y, Y_pred)
def cross_val(self, X_train, y_train): ''' method to conduct 5-fold cross validation ''' kf = KFold(len(X_train), n_folds=5, random_state=None, shuffle=False) for train_idx, test_idx in kf: xtrain, xval = X_train[train_idx], X_train[test_idx] ytrain, yval = y_train[train_idx], y_train[test_idx] model = ChainCRF() ssvm = FrankWolfeSSVM(model=model, C=0.5, max_iter=15) ssvm.fit(xtrain, ytrain) print ssvm.score(xval, yval)
def __init__(self, c_value, classifier_name='ChainCRF'): self.c_value = c_value self.classifier_name = classifier_name #using chain crf to analyze the data, so add an error check for this: if self.classifier_name == 'ChainCRF': model = ChainCRF() #define the classifier to use with CRF model. self.clf = FrankWolfeSSVM(model=model, C=self.c_value, max_iter=100) else: raise TypeError('Invalid classifier type')
def test_ssvm_objectives(): # test that the algorithms provide consistent objective curves. # this is not that strong a test now but at least makes sure that # the objective function is called. X, Y = generate_blocks_multinomial(n_samples=10, noise=1.5, seed=0) n_labels = len(np.unique(Y)) crf = GridCRF(n_states=n_labels, inference_method=inference_method) # once for n-slack clf = NSlackSSVM(model=crf, max_iter=5, C=1, tol=.1) clf.fit(X, Y) primal_objective = objective_primal(clf.model, clf.w, X, Y, clf.C) assert_almost_equal(clf.primal_objective_curve_[-1], primal_objective) # once for one-slack clf = OneSlackSSVM(model=crf, max_iter=5, C=1, tol=.1) clf.fit(X, Y) primal_objective = objective_primal(clf.model, clf.w, X, Y, clf.C, variant='one_slack') assert_almost_equal(clf.primal_objective_curve_[-1], primal_objective) # now subgradient. Should also work in batch-mode. clf = SubgradientSSVM(model=crf, max_iter=5, C=1, batch_size=-1) clf.fit(X, Y) primal_objective = objective_primal(clf.model, clf.w, X, Y, clf.C) assert_almost_equal(clf.objective_curve_[-1], primal_objective) # frank wolfe clf = FrankWolfeSSVM(model=crf, max_iter=5, C=1, batch_mode=True) clf.fit(X, Y) primal_objective = objective_primal(clf.model, clf.w, X, Y, clf.C) assert_almost_equal(clf.primal_objective_curve_[-1], primal_objective) # block-coordinate Frank-Wolfe clf = FrankWolfeSSVM(model=crf, max_iter=5, C=1, batch_mode=False) clf.fit(X, Y) primal_objective = objective_primal(clf.model, clf.w, X, Y, clf.C) assert_almost_equal(clf.primal_objective_curve_[-1], primal_objective)
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_svm_as_crf_pickling_bcfw(): 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) 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 MLfitCRF(data_train, data_test, records, folds): fvector = np.array([data_train[0]]) labels = np.array([data_train[1]]) #create CRF model CRFmodel = ChainCRF() #create ML classifier ssvm = FrankWolfeSSVM(model = CRFmodel, C = 0.1) #training ssvm.fit(fvector, labels) #model testing fvector_test = np.array(data_test[0]) labels_test = np.array(data_test[1]) score = ssvm.score(fvector_train, labels_test) print score return
def chaincrf_test(): num_pics = 3000 X, Y = load_pictures(num_pics) X = np.array(X) Y = np.array(Y) print X.shape print Y.shape # 0: pixel, 1: row, 2: picture mode = 0 outstr = "Test score with data arranged by " if mode == 0: X, Y = arrange_by_pixel(X, Y) outstr += "pixel:" elif mode == 1: X, Y = arrange_by_row(X, Y) outstr += "row:" elif mode == 2: X, Y = arrange_by_picture(X, Y) outstr += "picture:" print X.shape print Y.shape #print X.shape, Y.shape train_pct = 0.66 test_pct = 1 - train_pct X_train = X[0:math.floor(train_pct * num_pics)] X_test = X[math.floor(test_pct * num_pics):] Y_train = Y[0:math.floor(train_pct * num_pics)] Y_test = Y[math.floor(test_pct * num_pics):] model = ChainCRF() ssvm = FrankWolfeSSVM(model=model, C=.1, max_iter=10) # #print X_train.shape, Y_train.shape ssvm.fit(X_train, Y_train) results = ssvm.score(X_test, Y_test) print outstr print results
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 __init__(self, do_train=False, trained_model_name="passage_crf_model", algorithm="crf"): self.trained_model_name = trained_model_name self.fp = FeatureProcessing() self.do_train = do_train self.algorithm = algorithm if algorithm == "crf": if do_train: self.trainer = Trainer() else: self.tagger = Tagger() else: if do_train: model = ChainCRF() self.trainer = FrankWolfeSSVM(model=model) self.feat_index = {} self.label_index = {} else: self.tagger = pickle.load(open(self.trained_model_name, "rb")) self.feat_index = pickle.load(open("ssvm_feat_index.pkl", "rb")) label_index = pickle.load(open("ssvm_label_index.pkl", "rb")) self.rev_label_index = {i: x for x, i in label_index.items()}
def Chain_CRF(x, y, x_test, model_args): # Reshape for CRF #svc = SVC(class_weight='balanced', kernel='rbf', decision_function_shape='ovr') #svc.fit(x, y) #x = svc.decision_function(x) #x_test = svc.decision_function(x_test) #scaler = StandardScaler().fit(x) #x = scaler.transform(x) #x_test = scaler.transform(x_test) x = x[:, :11] x_test = x_test[:, :11] x = x.reshape(-1, 21600, x.shape[-1]) x_test = x_test.reshape(-1, 21600, x.shape[-1]) y = y.reshape(-1, 21600) crf = ChainCRF(directed=False) ssvm = FrankWolfeSSVM(model=crf, C=model_args['C'], max_iter=model_args['max_iter']) ssvm.fit(x, y) y_pred = np.array(ssvm.predict(x_test)) return y_pred.flatten()
def build_models(X_train, y_train): ''' PURPOSE: ouput model objects which have been fitted with training data INPUT: X_train (np.array) - features matrix y_train (np.array) - label matrix OUTPUT: nmb (MultinomialNB obj) - model trained on X_train, y_train svm (LinearSVC obj) - model trained on X_train, y_train ssvm (PyStruct chainCRF object) - trained Chain CRF model ''' # Multinomial Naive Bayes Classifier: nmb = MultinomialNB() nmb.fit(np.vstack(X_train), np.hstack(y_train)) # Support Vector Machine Classifier svm = LinearSVC(dual=False, C=.1) svm.fit(np.vstack(X_train), np.hstack(y_train)) # Chain Conditional Random Field Classifier model = ChainCRF() ssvm = FrankWolfeSSVM(model=model, C=0.5, max_iter=15) ssvm.fit(X_train, y_train) return nmb, svm, ssvm
n_classes=n_classes, Loss=Loss) gmodel = GeneralizedMultiClassClf(n_features=X_train_bias.shape[1], n_classes=n_classes, Loss=Loss) method = 'generalized' # method = 'vanilla' Cs = [1.] # Cs = [6.5, 7., 7.5] for C in Cs: fw_bc_svm = FrankWolfeSSVM(model, C=C, max_iter=300, check_dual_every=50, line_search=False, verbose=True) # fw_batch_svm = FrankWolfeSSVM(model, C=.1, max_iter=50, batch_mode=True) gfw_bc_svm = GeneralizedFrankWolfeSSVM(gmodel, C=C, max_iter=300, check_dual_every=50, line_search=False, verbose=True) if method == 'generalized': start = time() gfw_bc_svm.fit(X_train_bias, y_train) y_pred = np.hstack(gfw_bc_svm.predict(X_test_bias)) time_fw_bc_svm = time() - start
X = X / 16. #y = y.astype(np.int) - 1 X_train, X_test, y_train, y_test = train_test_split(X, y) # we add a constant 1 feature for the bias X_train_bias = np.hstack([X_train, np.ones((X_train.shape[0], 1))]) X_test_bias = np.hstack([X_test, np.ones((X_test.shape[0], 1))]) model = MultiClassClf(n_features=X_train_bias.shape[1], n_classes=10) n_slack_svm = NSlackSSVM(model, verbose=2, check_constraints=False, C=0.1, batch_size=100, tol=1e-2) one_slack_svm = OneSlackSSVM(model, verbose=2, C=.10, tol=.001) subgradient_svm = SubgradientSSVM(model, C=0.1, learning_rate=0.000001, max_iter=1000, verbose=0) fw_bc_svm = FrankWolfeSSVM(model, C=.1, max_iter=50) fw_batch_svm = FrankWolfeSSVM(model, C=.1, max_iter=50, batch_mode=True) # n-slack cutting plane ssvm start = time() n_slack_svm.fit(X_train_bias, y_train) time_n_slack_svm = time() - start y_pred = np.hstack(n_slack_svm.predict(X_test_bias)) print("Score with pystruct n-slack ssvm: %f (took %f seconds)" % (np.mean(y_pred == y_test), time_n_slack_svm)) ## 1-slack cutting plane ssvm start = time() one_slack_svm.fit(X_train_bias, y_train) time_one_slack_svm = time() - start y_pred = np.hstack(one_slack_svm.predict(X_test_bias))
letters = load_letters() X, y, folds = letters['data'], letters['labels'], letters['folds'] # we convert the lists to object arrays, as that makes slicing much more # convenient X, y = np.array(X), np.array(y) X_train, X_test = X[folds == 1], X[folds != 1] y_train, y_test = y[folds == 1], y[folds != 1] # Train linear SVM svm = LinearSVC(dual=False, C=.1) # flatten input svm.fit(np.vstack(X_train), np.hstack(y_train)) # Train linear chain CRF model = ChainCRF() ssvm = FrankWolfeSSVM(model=model, C=.1, max_iter=11) ssvm.fit(X_train, y_train) print("Test score with chain CRF: %f" % ssvm.score(X_test, y_test)) print("Test score with linear SVM: %f" % svm.score(np.vstack(X_test), np.hstack(y_test))) # plot some word sequenced n_words = 4 rnd = np.random.RandomState(1) selected = rnd.randint(len(y_test), size=n_words) max_word_len = max([len(y_) for y_ in y_test[selected]]) fig, axes = plt.subplots(n_words, max_word_len, figsize=(10, 10)) fig.subplots_adjust(wspace=0) for ind, axes_row in zip(selected, axes):
def __init__(self, c_val=1.0): self.clf = FrankWolfeSSVM(model=ChainCRF(), C=c_val, max_iter=50)