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 structure_hierarchical_multilabel_classification(train_file_name,
                                                     test_file_name):
    train_file = open(train_file_name)
    test_file = open(test_file_name)

    train_features, train_labels, train_taxonomy = get_features_labels(
        train_file)

    model = HierarchicalMultilabelModel(train_features, train_labels,
                                        train_taxonomy)
    sgd = StochasticSOSVM(model, train_labels)
    t1 = time.time()
    sgd.train()
    print('>>> Took %f time for training' % (time.time() - t1))

    test_features, test_labels, test_taxonomy = get_features_labels(test_file)
    assert(test_taxonomy.all() == train_taxonomy.all())

    evaluator = StructuredAccuracy()
    outlabel = LabelsFactory.to_structured(sgd.apply(test_features))

    print('>>> Accuracy of classification = %f' % evaluator.evaluate(
        outlabel, test_labels))