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
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() ))
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()))
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())
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
model = MulticlassModel(features, labels) loss = HingeLoss() risk = MulticlassRiskFunction() risk_data = MulticlassRiskData(features, labels, model.get_dim(), features.get_num_vectors()) lambda_ = 1e3 sosvm = DualLibQPBMSOSVM(model, loss, labels, features, lambda_, risk, risk_data) 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() 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 == y[i]: count = count + 1 print "Correct classification rate: %0.2f" % ( 100.0*count/out.get_num_labels() )
np.dot(np.random.randn(N, dim), covs[1]) + np.array([-10, -10]), np.dot(np.random.randn(N, dim), covs[2]) + np.array([10, -10])]; Y = np.hstack((np.zeros(N), np.ones(N), 2*np.ones(N))) return X, Y # Number of classes M = 3 # Number of samples of each class N = 50 # Dimension of the data dim = 2 X, y = gen_data() labels = MulticlassSOLabels(y) features = RealFeatures(X.T) model = MulticlassModel(features, labels) loss = HingeLoss() sosvm = PrimalMosekSOSVM(model, loss, labels, features) 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 == y[i]: count = count + 1 print "Correct classification rate: %0.2f" % ( 100.0*count/out.get_num_labels() )