def graphcuts_sosvm(num_train_samples = 10, len_label = 5, len_feat = 20, num_test_samples = 5): """ Graph cuts as approximate inference in structured output SVM framework. Args: num_train_samples: number of training samples len_label: number of classes, i.e., size of label space len_feat: the dimension of the feature vector num_test_samples: number of testing samples """ import time # generate synthetic dataset (labels_train, feats_train) = generate_data(num_train_samples, len_label, len_feat) # compute full-connected edge table full = np.vstack([x for x in itertools.combinations(range(len_label), 2)]) # define factor types factor_types = define_factor_types(len_label, len_feat, full) # create features and labels for factor graph mode (labels_fg, feats_fg) = build_factor_graph_model(labels_train, feats_train, factor_types, full, GRAPH_CUT) # create model and register factor types model = FactorGraphModel(feats_fg, labels_fg, GRAPH_CUT) for i in range(len(factor_types)): model.add_factor_type(factor_types[i]) # Training # the 3rd parameter is do_weighted_averaging, by turning this on, # a possibly faster convergence rate may be achieved. # the 4th parameter controls outputs of verbose training information sgd = StochasticSOSVM(model, labels_fg, True, True) sgd.set_num_iter(150) sgd.set_lambda(0.0001) # train t0 = time.time() sgd.train() t1 = time.time() w_sgd = sgd.get_w() #print "SGD took", t1 - t0, "seconds." # training error labels_pr = sgd.apply() ave_loss = evaluation(labels_pr, labels_fg, model) #print('SGD: Average training error is %.4f' % ave_loss) # testing error # generate synthetic testing dataset (labels_test, feats_test) = generate_data(num_test_samples, len_label, len_feat) # create features and labels for factor graph mode (labels_fg_test, feats_fg_test) = build_factor_graph_model(labels_test, feats_test, factor_types, full, GRAPH_CUT) # set features and labels to sgd sgd.set_features(feats_fg_test) sgd.set_labels(labels_fg_test) # test labels_pr = sgd.apply() ave_loss = evaluation(labels_pr, labels_fg_test, model)
def structure_factor_graph_model(tr_samples=samples, tr_labels=labels, w=w_all, ftype=ftype_all): from modshogun import SOSVMHelper, LabelsFactory from modshogun import FactorGraphModel, MAPInference, TREE_MAX_PROD from modshogun import DualLibQPBMSOSVM, StochasticSOSVM # create model model = FactorGraphModel(tr_samples, tr_labels, TREE_MAX_PROD, False) w_truth = [w[0].copy(), w[1].copy(), w[2].copy()] w[0] = np.zeros(8) w[1] = np.zeros(4) w[2] = np.zeros(2) ftype[0].set_w(w[0]) ftype[1].set_w(w[1]) ftype[2].set_w(w[2]) model.add_factor_type(ftype[0]) model.add_factor_type(ftype[1]) model.add_factor_type(ftype[2]) # --- training with BMRM --- bmrm = DualLibQPBMSOSVM(model, tr_labels, 0.01) #bmrm.set_verbose(True) bmrm.train() #print 'learned weights:' #print bmrm.get_w() #print 'ground truth weights:' #print w_truth # evaluation lbs_bmrm = LabelsFactory.to_structured(bmrm.apply()) acc_loss = 0.0 ave_loss = 0.0 for i in xrange(num_samples): y_pred = lbs_bmrm.get_label(i) y_truth = tr_labels.get_label(i) acc_loss = acc_loss + model.delta_loss(y_truth, y_pred) ave_loss = acc_loss / num_samples #print('BMRM: Average training error is %.4f' % ave_loss) # show primal objs and dual objs #hbm = bmrm.get_helper() #print hbm.get_primal_values() #print hbm.get_eff_passes() #print hbm.get_train_errors() # --- training with SGD --- sgd = StochasticSOSVM(model, tr_labels) #sgd.set_verbose(True) sgd.set_lambda(0.01) sgd.train()
def structure_factor_graph_model(tr_samples = samples, tr_labels = labels, w = w_all, ftype = ftype_all): from modshogun import SOSVMHelper, LabelsFactory from modshogun import FactorGraphModel, MAPInference, TREE_MAX_PROD from modshogun import DualLibQPBMSOSVM, StochasticSOSVM # create model model = FactorGraphModel(tr_samples, tr_labels, TREE_MAX_PROD, False) w_truth = [w[0].copy(), w[1].copy(), w[2].copy()] w[0] = np.zeros(8) w[1] = np.zeros(4) w[2] = np.zeros(2) ftype[0].set_w(w[0]) ftype[1].set_w(w[1]) ftype[2].set_w(w[2]) model.add_factor_type(ftype[0]) model.add_factor_type(ftype[1]) model.add_factor_type(ftype[2]) # --- training with BMRM --- bmrm = DualLibQPBMSOSVM(model, tr_labels, 0.01) #bmrm.set_verbose(True) bmrm.train() #print 'learned weights:' #print bmrm.get_w() #print 'ground truth weights:' #print w_truth # evaluation eva_bmrm = bmrm.apply() lbs_bmrm = LabelsFactory.to_structured(eva_bmrm) acc_loss = 0.0 ave_loss = 0.0 for i in xrange(num_samples): y_pred = lbs_bmrm.get_label(i) y_truth = tr_labels.get_label(i) acc_loss = acc_loss + model.delta_loss(y_truth, y_pred) ave_loss = acc_loss / num_samples #print('BMRM: Average training error is %.4f' % ave_loss) # show primal objs and dual objs #hbm = bmrm.get_helper() #print hbm.get_primal_values() #print hbm.get_eff_passes() #print hbm.get_train_errors() # --- training with SGD --- sgd = StochasticSOSVM(model, tr_labels) #sgd.set_verbose(True) sgd.set_lambda(0.01) sgd.train()
def graphcuts_sosvm(num_train_samples=20, len_label=10, len_feat=40, num_test_samples=10): """ Graph cuts as approximate inference in structured output SVM framework. Args: num_train_samples: number of training samples len_label: number of classes, i.e., size of label space len_feat: the dimention of the feature vector num_test_samples: number of testing samples """ import time # generate synthetic dataset (labels_train, feats_train) = generate_data(num_train_samples, len_label, len_feat) # compute full-connected edge table full = np.vstack([x for x in itertools.combinations(range(len_label), 2)]) # define factor types factor_types = define_factor_types(len_label, len_feat, full) # create features and labels for factor graph mode (labels_fg, feats_fg) = build_factor_graph_model(labels_train, feats_train, factor_types, full, GRAPH_CUT) # create model and register factor types model = FactorGraphModel(feats_fg, labels_fg, GRAPH_CUT) for i in range(len(factor_types)): model.add_factor_type(factor_types[i]) # Training # the 3rd parameter is do_weighted_averaging, by turning this on, # a possibly faster convergence rate may be achieved. # the 4th parameter controls outputs of verbose training information sgd = StochasticSOSVM(model, labels_fg, True, True) sgd.set_num_iter(150) sgd.set_lambda(0.0001) # train t0 = time.time() sgd.train() t1 = time.time() w_sgd = sgd.get_w() #print "SGD took", t1 - t0, "seconds." # training error labels_pr = sgd.apply() ave_loss = evaluation(labels_pr, labels_fg, model) #print('SGD: Average training error is %.4f' % ave_loss) # testing error # generate synthetic testing dataset (labels_test, feats_test) = generate_data(num_test_samples, len_label, len_feat) # create features and labels for factor graph mode (labels_fg_test, feats_fg_test) = build_factor_graph_model(labels_test, feats_test, factor_types, full, GRAPH_CUT) # set features and labels to sgd sgd.set_features(feats_fg_test) sgd.set_labels(labels_fg_test) # test labels_pr = sgd.apply() ave_loss = evaluation(labels_pr, labels_fg_test, model)