def liblinear (): print 'LibLinear' from shogun.Features import RealFeatures, SparseRealFeatures, Labels from shogun.Classifier import LibLinear realfeat=RealFeatures(fm_train_real) feats_train=SparseRealFeatures() feats_train.obtain_from_simple(realfeat) realfeat=RealFeatures(fm_test_real) feats_test=SparseRealFeatures() feats_test.obtain_from_simple(realfeat) C=0.9 epsilon=1e-5 num_threads=1 labels=Labels(label_train_twoclass) svm=LibLinear(C, feats_train, labels) svm.set_epsilon(epsilon) svm.parallel.set_num_threads(num_threads) svm.set_bias_enabled(True) svm.train() svm.set_features(feats_test) print svm.classify().get_labels()
def classifier_liblinear_modular(fm_train_real, fm_test_real, label_train_twoclass, C, epsilon): from shogun.Features import RealFeatures, SparseRealFeatures, Labels from shogun.Classifier import LibLinear, L2R_L2LOSS_SVC_DUAL from shogun.Library import Math_init_random Math_init_random(17) feats_train=RealFeatures(fm_train_real) feats_test=RealFeatures(fm_test_real) labels=Labels(label_train_twoclass) svm=LibLinear(C, feats_train, labels) svm.set_liblinear_solver_type(L2R_L2LOSS_SVC_DUAL) svm.set_epsilon(epsilon) svm.set_bias_enabled(True) svm.train() svm.set_features(feats_test) svm.classify().get_labels() predictions = svm.classify() return predictions, svm, predictions.get_labels()
alpha = old_svm.get_alpha(j) inner_sum = inner_sum + alpha * kv.kernel(sv_id, idx) inner.append(inner_sum) #general case linterm_manual[idx] = B * tmp_lab[idx] * inner_sum - 1.0 ################ # compare pre-svms assert (presvm_liblinear.get_bias() == 0.0) assert (presvm_libsvm.get_bias() == 0.0) tmp_out = presvm_liblinear.classify(feat).get_labels() tmp_out2 = presvm_libsvm.classify(feat).get_labels() # compare outputs for i in xrange(N): try: assert (abs(inner[i] - tmp_out[i]) <= 0.001) assert (abs(inner[i] - tmp_out2[i]) <= 0.001) except Exception, message: print "difference in outputs: (%.4f, %.4f, %.4f)" % (tmp_out[i], tmp_out2[i]) ############### # compare to LibSVM
def SVMLinear(traindata, trainlabs, testdata, C=1.0, eps=1e-5, threads=1, getw=False, useLibLinear=False, useL1R=False): """ Does efficient linear SVM using the OCAS subgradient solver (as interfaced by shogun). Handles multiclass problems using a one-versus-all approach. NOTE: the training and testing data should both be scaled such that each dimension ranges from 0 to 1 traindata = n by d training data array trainlabs = n-length training data label vector (should be normalized so labels range from 0 to c-1, where c is the number of classes) testdata = m by d array of data to test C = SVM regularization constant eps = precision parameter used by OCAS threads = number of threads to use getw = whether or not to return the learned weight vector from the SVM (note: only works for 2-class problems) returns: m-length vector containing the predicted labels of the instances in testdata if problem is 2-class and getw == True, then a d-length weight vector is also returned """ numc = trainlabs.max() + 1 # # when using an L1 solver, we need the data transposed # # trainfeats = wrapFeatures(traindata, sparse=True) # testfeats = wrapFeatures(testdata, sparse=True) if not useL1R: ### traindata directly here for LR2_L2LOSS_SVC trainfeats = wrapFeatures(traindata, sparse=False) else: ### traindata.T here for L1R_LR trainfeats = wrapFeatures(traindata.T, sparse=False) testfeats = wrapFeatures(testdata, sparse=False) if numc > 2: preds = np.zeros(testdata.shape[0], dtype=np.int32) predprobs = np.zeros(testdata.shape[0]) predprobs[:] = -np.inf for i in xrange(numc): # set up svm tlabs = np.int32(trainlabs == i) tlabs[tlabs == 0] = -1 # print tlabs # print i, ' ', np.sum(tlabs==-1), ' ', np.sum(tlabs==1) labels = BinaryLabels(np.float64(tlabs)) if useLibLinear: # Use LibLinear and set the solver type svm = LibLinear(C, trainfeats, labels) if useL1R: # this is L1 regularization on logistic loss svm.set_liblinear_solver_type(L1R_LR) else: # most of my results were computed with this (ucf50) svm.set_liblinear_solver_type(L2R_L2LOSS_SVC) else: # Or Use SVMOcas svm = SVMOcas(C, trainfeats, labels) svm.set_epsilon(eps) svm.parallel.set_num_threads(threads) svm.set_bias_enabled(True) # train svm.train() # test res = svm.apply(testfeats).get_labels() thisclass = res > predprobs preds[thisclass] = i predprobs[thisclass] = res[thisclass] return preds else: tlabs = trainlabs.copy() tlabs[tlabs == 0] = -1 labels = Labels(np.float64(tlabs)) svm = SVMOcas(C, trainfeats, labels) svm.set_epsilon(eps) svm.parallel.set_num_threads(threads) svm.set_bias_enabled(True) # train svm.train() # test res = svm.classify(testfeats).get_labels() res[res > 0] = 1 res[res <= 0] = 0 if getw == True: return res, svm.get_w() else: return res
inner_sum = inner_sum + alpha * kv.kernel(sv_id, idx) inner.append(inner_sum) #general case linterm_manual[idx] = B *tmp_lab[idx] * inner_sum - 1.0 ################ # compare pre-svms assert(presvm_liblinear.get_bias() == 0.0) assert(presvm_libsvm.get_bias() == 0.0) tmp_out = presvm_liblinear.classify(feat).get_labels() tmp_out2 = presvm_libsvm.classify(feat).get_labels() # compare outputs for i in xrange(N): try: assert(abs(inner[i]-tmp_out[i])<= 0.001) assert(abs(inner[i]-tmp_out2[i])<= 0.001) except Exception, message: print "difference in outputs: (%.4f, %.4f, %.4f)" % (tmp_out[i], tmp_out2[i]) ###############