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))
def structure_multiclass_bmrm(fm_train_real=traindat,
                              label_train_multiclass=label_traindat):
    from modshogun import MulticlassSOLabels, LabelsFactory
    from modshogun import RealFeatures
    from modshogun import SOSVMHelper
    try:
        from modshogun import BMRM, PPBMRM, P3BMRM, DualLibQPBMSOSVM
    except ImportError:
        print(
            "At least one of BMRM, PPBMRM, P3BMRM, DualLibQPBMSOSVM not available"
        )
        exit(0)
    from modshogun import MulticlassModel, RealNumber

    labels = MulticlassSOLabels(label_train_multiclass)
    features = RealFeatures(fm_train_real.T)

    model = MulticlassModel(features, labels)
    sosvm = DualLibQPBMSOSVM(model, labels, 1.0)

    # BMRM
    sosvm.set_solver(BMRM)
    sosvm.set_verbose(True)
    sosvm.train()

    bmrm_out = LabelsFactory.to_multiclass_structured(sosvm.apply())
    count = 0
    for i in range(bmrm_out.get_num_labels()):
        yi_pred = RealNumber.obtain_from_generic(bmrm_out.get_label(i))
        if yi_pred.value == label_train_multiclass[i]:
            count = count + 1

    #print("BMRM: Correct classification rate: %0.2f" % ( 100.0*count/bmrm_out.get_num_labels() ))
    #hp = sosvm.get_helper()
    #print hp.get_primal_values()
    #print hp.get_train_errors()

    # PPBMRM
    w = np.zeros(model.get_dim())
    sosvm.set_w(w)
    sosvm.set_solver(PPBMRM)
    sosvm.set_verbose(True)
    sosvm.train()

    ppbmrm_out = LabelsFactory.to_multiclass_structured(sosvm.apply())
    count = 0
    for i in range(ppbmrm_out.get_num_labels()):
        yi_pred = RealNumber.obtain_from_generic(ppbmrm_out.get_label(i))
        if yi_pred.value == label_train_multiclass[i]:
            count = count + 1

    #print("PPBMRM: Correct classification rate: %0.2f" % ( 100.0*count/ppbmrm_out.get_num_labels() ))

    # P3BMRM
    w = np.zeros(model.get_dim())
    sosvm.set_w(w)
    sosvm.set_solver(P3BMRM)
    sosvm.set_verbose(True)
    sosvm.train()

    p3bmrm_out = LabelsFactory.to_multiclass_structured(sosvm.apply())
    count = 0
    for i in range(p3bmrm_out.get_num_labels()):
        yi_pred = RealNumber.obtain_from_generic(p3bmrm_out.get_label(i))
        if yi_pred.value == label_train_multiclass[i]:
            count = count + 1

    #print("P3BMRM: Correct classification rate: %0.2f" % ( 100.0*count/p3bmrm_out.get_num_labels() ))
    return bmrm_out, ppbmrm_out, p3bmrm_out
def structure_multiclass_bmrm(fm_train_real=traindat,label_train_multiclass=label_traindat):
	from modshogun import MulticlassSOLabels, LabelsFactory
	from modshogun import RealFeatures
	from modshogun import SOSVMHelper
	from modshogun import BMRM, PPBMRM, P3BMRM
	from modshogun import MulticlassModel, DualLibQPBMSOSVM, RealNumber

	labels = MulticlassSOLabels(label_train_multiclass)
	features = RealFeatures(fm_train_real.T)

	model = MulticlassModel(features, labels)
	sosvm = DualLibQPBMSOSVM(model, labels, 1.0)

	# BMRM
	sosvm.set_solver(BMRM)
	sosvm.set_verbose(True)
	sosvm.train()

	bmrm_out = LabelsFactory.to_multiclass_structured(sosvm.apply())
	count = 0
	for i in range(bmrm_out.get_num_labels()):
		yi_pred = RealNumber.obtain_from_generic(bmrm_out.get_label(i))
		if yi_pred.value == label_train_multiclass[i]:
			count = count + 1

	#print("BMRM: Correct classification rate: %0.2f" % ( 100.0*count/bmrm_out.get_num_labels() ))
	#hp = sosvm.get_helper()
	#print hp.get_primal_values()
	#print hp.get_train_errors()

	# PPBMRM
	w = np.zeros(model.get_dim())
	sosvm.set_w(w)
	sosvm.set_solver(PPBMRM)
	sosvm.set_verbose(True)
	sosvm.train()

	ppbmrm_out = LabelsFactory.to_multiclass_structured(sosvm.apply())
	count = 0
	for i in range(ppbmrm_out.get_num_labels()):
		yi_pred = RealNumber.obtain_from_generic(ppbmrm_out.get_label(i))
		if yi_pred.value == label_train_multiclass[i]:
			count = count + 1

	#print("PPBMRM: Correct classification rate: %0.2f" % ( 100.0*count/ppbmrm_out.get_num_labels() ))

	# P3BMRM
	w = np.zeros(model.get_dim())
	sosvm.set_w(w)
	sosvm.set_solver(P3BMRM)
	sosvm.set_verbose(True)
	sosvm.train()

	p3bmrm_out = LabelsFactory.to_multiclass_structured(sosvm.apply())
	count = 0
	for i in range(p3bmrm_out.get_num_labels()):
		yi_pred = RealNumber.obtain_from_generic(p3bmrm_out.get_label(i))
		if yi_pred.value == label_train_multiclass[i]:
			count = count + 1

	#print("P3BMRM: Correct classification rate: %0.2f" % ( 100.0*count/p3bmrm_out.get_num_labels() ))
	return bmrm_out, ppbmrm_out, p3bmrm_out