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
    def argmax(self, w, feat_idx, training):
        feature_vector = self.get_features().get_feature_vector(feat_idx)
        label = None
        if training == True:
            label = int(
                RealNumber.obtain_from_generic(
                    self.get_labels().get_label(feat_idx)).value)
        ypred = 0
        max_score = -1e10
        for c in xrange(self.n_classes):
            score = 0.0
            for i in xrange(self.n_feats):
                score += w[i + self.n_feats * c] * feature_vector[i]
            if training == True:
                score += (c != label)
            if score > max_score:
                max_score = score
                ypred = c

        res = ResultSet()
        res.score = max_score
        res.psi_pred = RealVector(self.dim)
        res.psi_pred.zero()
        for i in xrange(self.n_feats):
            res.psi_pred[i + self.n_feats * ypred] = feature_vector[i]
        res.argmax = RealNumber(ypred)
        if training == True:
            res.delta = (label != ypred)
            res.psi_truth = RealVector(self.dim)
            res.psi_truth.zero()
            for i in xrange(self.n_feats):
                res.psi_truth[i + self.n_feats * label] = feature_vector[i]
            for i in xrange(self.n_feats):
                res.score -= w[i + self.n_feats * label] * feature_vector[i]
        return res
	def argmax(self,w,feat_idx,training):
		feature_vector = self.get_features().get_feature_vector(feat_idx)
		label = None
		if training == True:
			label = int(RealNumber.obtain_from_generic(self.get_labels().get_label(feat_idx)).value)
		ypred = 0
		max_score = -1e10
		for c in xrange(self.n_classes):
			score = 0.0
			for i in xrange(self.n_feats):
				score += w[i+self.n_feats*c]*feature_vector[i]
			if training == True:
				score += (c!=label)
			if score > max_score:
				max_score = score
				ypred = c

		res = ResultSet()
		res.score = max_score
		res.psi_pred = RealVector(self.dim)
		res.psi_pred.zero()
		for i in xrange(self.n_feats):
			res.psi_pred[i+self.n_feats*ypred] = feature_vector[i]
		res.argmax = RealNumber(ypred)
		if training == True:
			res.delta = (label!=ypred)
			res.psi_truth = RealVector(self.dim)
			res.psi_truth.zero()
			for i in xrange(self.n_feats):
				res.psi_truth[i+self.n_feats*label] = feature_vector[i]
			for i in xrange(self.n_feats):
				res.score -= w[i+self.n_feats*label]*feature_vector[i]
		return res
Example #5
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()))
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() ))
def get_so_labels(out):
    N = out.get_num_labels()
    l = np.zeros(N)
    for i in xrange(N):
        l[i] = RealNumber.obtain_from_generic(out.get_label(i)).value
    return l
Example #8
0
def get_so_labels(out):
    N = out.get_num_labels()
    l = np.zeros(N)
    for i in xrange(N):
        l[i] = RealNumber.obtain_from_generic(out.get_label(i)).value
    return l
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