def structure_discrete_hmsvm_bmrm (m_data_dict=data_dict):
	from modshogun import RealMatrixFeatures, SequenceLabels, HMSVMModel, Sequence, TwoStateModel
	from modshogun import StructuredAccuracy, SMT_TWO_STATE
	try:
		from modshogun import DualLibQPBMSOSVM
	except ImportError:
		print("DualLibQPBMSOSVM not available")
		exit(0)

	labels_array = m_data_dict['label'][0]

	idxs = numpy.nonzero(labels_array == -1)
	labels_array[idxs] = 0

	labels = SequenceLabels(labels_array, 250, 500, 2)
	features = RealMatrixFeatures(m_data_dict['signal'].astype(float), 250, 500)

	num_obs = 4	# given by the data file used
	model = HMSVMModel(features, labels, SMT_TWO_STATE, num_obs)

	sosvm = DualLibQPBMSOSVM(model, labels, 5000.0)
	sosvm.train()
	#print sosvm.get_w()

	predicted = sosvm.apply(features)
	evaluator = StructuredAccuracy()
	acc = evaluator.evaluate(predicted, labels)
def structure_discrete_hmsvm_bmrm(m_data_dict=data_dict):
    from modshogun import RealMatrixFeatures, SequenceLabels, HMSVMModel, Sequence, TwoStateModel
    from modshogun import StructuredAccuracy, SMT_TWO_STATE
    try:
        from modshogun import DualLibQPBMSOSVM
    except ImportError:
        print("DualLibQPBMSOSVM not available")
        exit(0)

    labels_array = m_data_dict['label'][0]

    idxs = numpy.nonzero(labels_array == -1)
    labels_array[idxs] = 0

    labels = SequenceLabels(labels_array, 250, 500, 2)
    features = RealMatrixFeatures(m_data_dict['signal'].astype(float), 250,
                                  500)

    num_obs = 4  # given by the data file used
    model = HMSVMModel(features, labels, SMT_TWO_STATE, num_obs)

    sosvm = DualLibQPBMSOSVM(model, labels, 5000.0)
    sosvm.train()
    #print sosvm.get_w()

    predicted = sosvm.apply(features)
    evaluator = StructuredAccuracy()
    acc = evaluator.evaluate(predicted, labels)
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()
Exemplo n.º 4
0
def structure_plif_hmsvm_bmrm (num_examples, example_length, num_features, num_noise_features):
	from modshogun import RealMatrixFeatures, TwoStateModel, DualLibQPBMSOSVM, StructuredAccuracy

	model = TwoStateModel.simulate_data(num_examples, example_length, num_features, num_noise_features)
	sosvm = DualLibQPBMSOSVM(model, model.get_labels(), 5000.0)

	sosvm.train()
	#print sosvm.get_w()

	predicted = sosvm.apply(model.get_features())
	evaluator = StructuredAccuracy()
	acc = evaluator.evaluate(predicted, model.get_labels())
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()
Exemplo n.º 6
0
def structure_plif_hmsvm_bmrm (num_examples, example_length, num_features, num_noise_features):
	from modshogun import RealMatrixFeatures, TwoStateModel, StructuredAccuracy
	try:
		from modshogun import DualLibQPBMSOSVM
	except ImportError:
		print("DualLibQPBMSOSVM not available")
		exit(0)

	model = TwoStateModel.simulate_data(num_examples, example_length, num_features, num_noise_features)
	sosvm = DualLibQPBMSOSVM(model, model.get_labels(), 5000.0)
	sosvm.set_store_train_info(False)

	sosvm.train()
	#print sosvm.get_w()

	predicted = sosvm.apply(model.get_features())
	evaluator = StructuredAccuracy()
	acc = evaluator.evaluate(predicted, model.get_labels())
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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)

sosvm.train()

res = sosvm.get_result()
Fps = np.array(res.get_hist_Fp_vector())
Fds = np.array(res.get_hist_Fp_vector())
wdists = np.array(res.get_hist_wdist_vector())

plt.figure()
plt.subplot(221)
plt.title("Fp and Fd history")
plt.plot(xrange(res.get_n_iters()), Fps, hold=True)
plt.plot(xrange(res.get_n_iters()), Fds, hold=True)
plt.subplot(222)
plt.title("w dist history")
plt.plot(xrange(res.get_n_iters()), wdists)
Exemplo n.º 10
0
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)

sosvm.train()

res = sosvm.get_result()
Fps = np.array(res.get_hist_Fp_vector())
Fds = np.array(res.get_hist_Fp_vector())
wdists = np.array(res.get_hist_wdist_vector())

plt.figure()
plt.subplot(221)
plt.title('Fp and Fd history')
plt.plot(xrange(res.get_n_iters()), Fps, hold=True)
plt.plot(xrange(res.get_n_iters()), Fds, hold=True)
plt.subplot(222)
plt.title('w dist history')
plt.plot(xrange(res.get_n_iters()), wdists)
Exemplo n.º 11
0
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