Пример #1
0
def structure_hmsvm_bmrm(m_data_dict=data_dict):
    from shogun.Features import RealMatrixFeatures
    from shogun.Loss import HingeLoss
    from shogun.Structure import HMSVMLabels, HMSVMModel, Sequence, TwoStateModel, SMT_TWO_STATE
    from shogun.Evaluation import StructuredAccuracy
    from shogun.Structure import DualLibQPBMSOSVM

    labels_array = m_data_dict['label'][0]

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

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

    loss = HingeLoss()
    model = HMSVMModel(features, labels, SMT_TWO_STATE, 4)

    sosvm = DualLibQPBMSOSVM(model, loss, labels, 5000.0)
    sosvm.train()

    print sosvm.get_w()

    predicted = sosvm.apply()
    evaluator = StructuredAccuracy()
    acc = evaluator.evaluate(predicted, labels)

    print('Accuracy = %.4f' % acc)
Пример #2
0
def so_multiclass(fm_train_real=traindat,
                  label_train_multiclass=label_traindat):
    try:
        from shogun.Features import RealFeatures
        from shogun.Loss import HingeLoss
        from shogun.Structure 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)
    loss = HingeLoss()
    sosvm = PrimalMosekSOSVM(model, loss, 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()))
Пример #3
0
def structure_hmsvm_mosek(m_data_dict=data_dict):
    from shogun.Features import RealMatrixFeatures
    from shogun.Loss import HingeLoss
    from shogun.Structure import HMSVMLabels, HMSVMModel, Sequence, TwoStateModel, SMT_TWO_STATE
    from shogun.Evaluation import StructuredAccuracy

    try:
        from shogun.Structure import PrimalMosekSOSVM
    except ImportError:
        print "Mosek not available"
        import sys
        sys.exit(0)

    labels_array = m_data_dict['label'][0]

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

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

    loss = HingeLoss()
    model = HMSVMModel(features, labels, SMT_TWO_STATE, 4)

    sosvm = PrimalMosekSOSVM(model, loss, labels)
    sosvm.train()
    print sosvm.get_w()

    predicted = sosvm.apply()
    evaluator = StructuredAccuracy()
    acc = evaluator.evaluate(predicted, labels)

    print('Accuracy = %.4f' % acc)
Пример #4
0
def so_multiclass(fm_train_real=traindat,
                  label_train_multiclass=label_traindat):
    labels = MulticlassSOLabels(label_train_multiclass)
    features = RealFeatures(fm_train_real.T)

    model = MulticlassModel(features, labels)
    loss = HingeLoss()
    sosvm = PrimalMosekSOSVM(model, loss, 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())
Пример #5
0
# 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)
loss = HingeLoss()

lambda_ = 1e1
sosvm = DualLibQPBMSOSVM(model, loss, 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)
Пример #6
0
import toy_datasets as toy
import matplotlib.pyplot as plt
import pdb
import sys

from modshogun import MSG_DEBUG, MSG_INFO
from shogun.Loss import HingeLoss
from shogun.Structure import StructuredAccuracy
from subgradient_sosvm import SubgradientSOSVM, StochasticSubgradientSOSVM

Plot = True
SaveFigs = False
SaveLatexFigs = False

dummy = HingeLoss()
if len(sys.argv) > 1:
    if sys.argv[1] == 'info':
        print 'Info mode set'
        dummy.io.set_loglevel(MSG_INFO)
    elif sys.argv[1] == 'debug':
        print 'Debug mode set'
        dummy.io.set_loglevel(MSG_DEBUG)
    elif sys.argv[1] == 'silent':
        print 'Silent mode set'
    else:
        print 'Option %s not available, silent mode activated.'

# Training data formed by n_samples examples
n_samples = 20
# Square grid CRFs of size times size