def structure_multiclass_bmrm(fm_train_real=traindat,
                              label_train_multiclass=label_traindat):
    from modshogun import RealFeatures
    from modshogun import SOSVMHelper
    from modshogun import BMRM, PPBMRM, P3BMRM
    from modshogun import MulticlassModel, MulticlassSOLabels, 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()

    out = sosvm.apply()
    count = 0
    for i in xrange(out.get_num_labels()):
        yi_pred = RealNumber.obtain_from_generic(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/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()

    out = sosvm.apply()
    count = 0
    for i in xrange(out.get_num_labels()):
        yi_pred = RealNumber.obtain_from_generic(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/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()

    out = sosvm.apply()
    count = 0
    for i in xrange(out.get_num_labels()):
        yi_pred = RealNumber.obtain_from_generic(out.get_label(i))
        if yi_pred.value == label_train_multiclass[i]:
            count = count + 1
def structure_multiclass_bmrm(fm_train_real=traindat,label_train_multiclass=label_traindat):
	from modshogun  import RealFeatures
	from modshogun  import SOSVMHelper
	from modshogun  import BMRM, PPBMRM, P3BMRM
	from modshogun	import MulticlassModel, MulticlassSOLabels, 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()

	out = sosvm.apply()
	count = 0
	for i in xrange(out.get_num_labels()):
		yi_pred = RealNumber.obtain_from_generic(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/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()

	out = sosvm.apply()
	count = 0
	for i in xrange(out.get_num_labels()):
		yi_pred = RealNumber.obtain_from_generic(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/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()

	out = sosvm.apply()
	count = 0
	for i in xrange(out.get_num_labels()):
		yi_pred = RealNumber.obtain_from_generic(out.get_label(i))
		if yi_pred.value == label_train_multiclass[i]:
			count = count + 1
Example #3
0
def so_multiclass(fm_train_real=traindat,
                  label_train_multiclass=label_traindat):
    try:
        from modshogun import RealFeatures
        from modshogun import MulticlassModel, MulticlassSOLabels, PrimalMosekSOSVM, RealNumber
    except ImportError:
        print("Mosek not available")
        return

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

    model = MulticlassModel(features, labels)
    sosvm = PrimalMosekSOSVM(model, labels)
    sosvm.train()

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

    print("Correct classification rate: %0.2f" %
          (100.0 * count / out.get_num_labels()))
Example #4
0
M = 3
# Number of samples of each class
N = 1000
# Dimension of the data
dim = 2

X, y = gen_data()

cnt = 250

X2, y2 = fill_data(cnt, np.min(X), np.max(X))

labels = MulticlassSOLabels(y)
features = RealFeatures(X.T)

model = MulticlassModel(features, labels)

lambda_ = 1e1
sosvm = DualLibQPBMSOSVM(model, labels, lambda_)

sosvm.set_cleanAfter(
    10
)  # number of iterations that cutting plane has to be inactive for to be removed
sosvm.set_cleanICP(True)  # enables inactive cutting plane removal feature
sosvm.set_TolRel(0.001)  # set relative tolerance
sosvm.set_verbose(True)  # enables verbosity of the solver
sosvm.set_cp_models(16)  # set number of cutting plane models
sosvm.set_solver(BMRM)  # select training algorithm
#sosvm.set_solver(PPBMRM)
#sosvm.set_solver(P3BMRM)
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