Skip to content

Commit

Permalink
remove LibSVM, LibLinear, LibSVR from SWIG
Browse files Browse the repository at this point in the history
  • Loading branch information
karlnapf committed Jan 31, 2019
1 parent ab0aa3c commit 4cef4d1
Show file tree
Hide file tree
Showing 37 changed files with 99 additions and 116 deletions.
4 changes: 2 additions & 2 deletions examples/meta/src/base_api/put_get_add.sg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNN knn()
Machine knn = machine("KNN")
knn.put("k", 2)

Kernel k = kernel("GaussianKernel")
Expand Down Expand Up @@ -29,7 +29,7 @@ matrix[1,1] = 0.4
Distance dist = distance("EuclideanDistance")
knn.put("distance", dist)

LibSVM svm()
Machine svm = machine("LibSVM")
svm.put("kernel", k)
Kernel k2 = kernel(svm.get("kernel"))
svm.put("kernel", k2)
Expand Down
4 changes: 2 additions & 2 deletions examples/meta/src/binary/linear_support_vector_machine.sg
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Machine svm = machine("LibLinear", C1=C, C2=C, labels=labels_train, epsilon=epsi

#![train_and_apply]
svm.train(features_train)
BinaryLabels labels_predict = svm.apply_binary(features_test)
Labels labels_predict = svm.apply(features_test)
#![train_and_apply]

#![extract_weights_bias]
Expand All @@ -35,4 +35,4 @@ real accuracy = eval.evaluate(labels_predict, labels_test)
#![evaluate_accuracy]

# additional integration testing variables
RealVector output = labels_predict.get_labels()
RealVector output = labels_predict.get_real_vector("labels")
2 changes: 1 addition & 1 deletion examples/meta/src/evaluation/cross_validation.sg
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ real epsilon = 0.001
#![set_parameters]

#![create_instance]
Machine svm = machine("LibLinear", labels=labels_train, epsilon=epsilon, C1=C, liblinear_solver_type=enum LIBLINEAR_SOLVER_TYPE.L2R_L2LOSS_SVC)
Machine svm = machine("LibLinear", labels=labels_train, epsilon=epsilon, C1=C, C2=C, liblinear_solver_type=enum LIBLINEAR_SOLVER_TYPE.L2R_L2LOSS_SVC)
#![create_instance]

#![create_cross_validation]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ File f_labels = csv_file("../../data/label_train_twoclass.dat")
Features feats = features(f_feats)
Labels labs = labels(f_labels)

Machine svm = machine("LibLinear")
svm.put("liblinear_solver_type", enum LIBLINEAR_SOLVER_TYPE.L2R_L2LOSS_SVC)
Machine svm = machine("LibLinear", liblinear_solver_type=enum LIBLINEAR_SOLVER_TYPE.L2R_L2LOSS_SVC)

StratifiedCrossValidationSplitting splitting_strategy(labs, 5)
Evaluation evaluation_criterion = evaluation("AccuracyMeasure")
Expand Down
2 changes: 1 addition & 1 deletion examples/meta/src/gaussian_process/regression.sg
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ real marg_ll = inference_method.get_negative_log_marginal_likelihood()
#![evaluate_error_and_marginal_likelihood]

# integration testing variables
RealVector output = labels_test.get_labels()
RealVector output = labels_test.get_real_vector("labels")
3 changes: 1 addition & 2 deletions examples/meta/src/meta_api/enums.sg
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Machine m = machine("LibLinear")
m.put("liblinear_solver_type", enum LIBLINEAR_SOLVER_TYPE.L2R_LR)
Machine m = machine("LibLinear", liblinear_solver_type=enum LIBLINEAR_SOLVER_TYPE.L2R_LR)
m.put("liblinear_solver_type", enum LIBLINEAR_SOLVER_TYPE.L2R_L2LOSS_SVC_DUAL)
2 changes: 1 addition & 1 deletion examples/meta/src/multiclass/relaxed_tree.sg
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ MulticlassLabels labels_test(f_labels_test)
#![create_features]

#![set_parameters]
MulticlassLibLinear mll()
Machine mll = machine("MulticlassLibLinear")
Kernel k = kernel("GaussianKernel")
#![set_parameters]

Expand Down
12 changes: 5 additions & 7 deletions examples/meta/src/multiclass/support_vector_machine.sg
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,22 @@ CSVFile f_labels_test("../../data/classifier_4class_2d_linear_labels_test.dat")
#![create_features]
Features features_train = features(f_feats_train)
Features features_test = features(f_feats_test)
MulticlassLabels labels_train(f_labels_train)
MulticlassLabels labels_test(f_labels_test)
Labels labels_train = labels(f_labels_train)
Labels labels_test = labels(f_labels_test)
#![create_features]

#![set_parameters]
real C = 1.0
real epsilon = 0.0001
Kernel gauss_kernel = kernel("GaussianKernel", log_width=0.0)
#![set_parameters]

#![create_instance]
MulticlassLibSVM svm(C, gauss_kernel, labels_train)
svm.set_epsilon(epsilon)
Machine svm = machine("MulticlassLibSVM", C=C, kernel=gauss_kernel, labels=labels_train)
#![create_instance]

#![train_and_apply]
svm.train(features_train)
MulticlassLabels labels_predict = svm.apply_multiclass(features_test)
Labels labels_predict = svm.apply(features_test)
#![train_and_apply]

#![evaluate_accuracy]
Expand All @@ -32,4 +30,4 @@ real accuracy = accu.evaluate(labels_predict, labels_test)
#![evaluate_accuracy]

# additional integration testing variables
RealVector output = labels_predict.get_labels()
RealVector output = labels_predict.get_real_vector("labels")
7 changes: 3 additions & 4 deletions examples/undocumented/python/classifier_libsvmoneclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
parameter_list = [[traindat,testdat,2.2,1,1e-7],[traindat,testdat,2.1,1,1e-5]]

def classifier_libsvmoneclass (train_fname=traindat,test_fname=testdat,width=2.1,C=1,epsilon=1e-5):
from shogun import RealFeatures, LibSVMOneClass, CSVFile
from shogun import RealFeatures, CSVFile
import shogun as sg

feats_train=RealFeatures(CSVFile(train_fname))
feats_test=RealFeatures(CSVFile(test_fname))

kernel=sg.kernel("GaussianKernel", log_width=width)

svm=LibSVMOneClass(C, kernel)
svm.set_epsilon(epsilon)
svm=sg.machine("LibSVMOneClass", C1=C, C2=C, kernel=kernel, epsilon=epsilon)
svm.train(feats_train)

predictions = svm.apply(feats_test)
return predictions, svm, predictions.get_labels()
return predictions, svm, predictions.get("labels")

if __name__=='__main__':
print('LibSVMOneClass')
Expand Down
8 changes: 5 additions & 3 deletions examples/undocumented/python/classifier_multiclass_ecoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
def classifier_multiclass_ecoc (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,label_test_multiclass=label_testdat,lawidth=2.1,C=1,epsilon=1e-5):

import shogun
from shogun import ECOCStrategy, LibLinear, L2R_L2LOSS_SVC, LinearMulticlassMachine
from shogun import ECOCStrategy, L2R_L2LOSS_SVC, LinearMulticlassMachine
from shogun import MulticlassAccuracy
from shogun import RealFeatures, MulticlassLabels
import shogun as sg

def nonabstract_class(name):
try:
Expand All @@ -37,8 +38,9 @@ def nonabstract_class(name):
else:
gnd_test = MulticlassLabels(label_test_multiclass)

base_classifier = LibLinear(L2R_L2LOSS_SVC)
base_classifier.set_bias_enabled(True)
base_classifier = sg.machine("LibLinear",
liblinear_solver_type=L2R_L2LOSS_SVC,
use_bias=True)

#print('Testing with %d encoders and %d decoders' % (len(encoders), len(decoders)))
#print('-' * 70)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@

def classifier_multiclassliblinear (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,label_test_multiclass=label_testdat,width=2.1,C=1,epsilon=1e-5):
from shogun import RealFeatures, MulticlassLabels
from shogun import MulticlassLibLinear
import shogun as sg

feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)

labels=MulticlassLabels(label_train_multiclass)

classifier = MulticlassLibLinear(C,feats_train,labels)
classifier.train()
classifier = sg.machine("MulticlassLibLinear", C=C, labels=labels)
classifier.train(feats_train)

label_pred = classifier.apply(feats_test)
out = label_pred.get_labels()
out = label_pred.get("labels")

if label_test_multiclass is not None:
from shogun import MulticlassAccuracy
Expand Down
5 changes: 2 additions & 3 deletions examples/undocumented/python/classifier_multiclassmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def classifier_multiclassmachine (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,width=2.1,C=1,epsilon=1e-5):
from shogun import RealFeatures, MulticlassLabels
from shogun import LibSVM, KernelMulticlassMachine, MulticlassOneVsRestStrategy
from shogun import KernelMulticlassMachine, MulticlassOneVsRestStrategy
import shogun as sg

feats_train=RealFeatures(fm_train_real)
Expand All @@ -16,8 +16,7 @@ def classifier_multiclassmachine (fm_train_real=traindat,fm_test_real=testdat,la

labels=MulticlassLabels(label_train_multiclass)

classifier = LibSVM()
classifier.set_epsilon(epsilon)
classifier = sg.machine("LibSVM", epsilon=epsilon)
#print labels.get_labels()
mc_classifier = KernelMulticlassMachine(MulticlassOneVsRestStrategy(),kernel,classifier,labels)
mc_classifier.train(feats_train)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@

def classifier_multilabeloutputliblinear (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,label_test_multiclass=label_testdat,width=2.1,C=1,epsilon=1e-5):
from shogun import RealFeatures, MulticlassLabels, MultilabelLabels
from shogun import MulticlassLibLinear

import shogun as sg
feats_train=RealFeatures(fm_train_real)
feats_test=RealFeatures(fm_test_real)

labels=MulticlassLabels(label_train_multiclass)

classifier = MulticlassLibLinear(C,feats_train,labels)
classifier.train()
classifier = sg.machine("MulticlassLibLinear", C1=C, C2=C, labels=labels)
classifier.train(feats_train)

label_pred = classifier.apply_multilabel_output(feats_test,2)
out = label_pred.get_labels()
# TODO: figure out the new style API for the below call, disabling for now
#label_pred = classifier.apply_multilabel_output(feats_test,2)
#out = label_pred.get_labels()
#print out
return out
#return out

if __name__=='__main__':
print('MultilabelOutputLibLinear')
Expand Down
7 changes: 4 additions & 3 deletions examples/undocumented/python/classifier_ssk.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
def classifier_ssk (fm_train_dna=traindat,fm_test_dna=testdat,
label_train_dna=label_traindat,C=1,maxlen=1,decay=1):
from shogun import StringCharFeatures, BinaryLabels
from shogun import LibSVM, SubsequenceStringKernel, DNA
from shogun import SubsequenceStringKernel, DNA
from shogun import ErrorRateMeasure
import shogun as sg

feats_train=StringCharFeatures(fm_train_dna, DNA)
feats_test=StringCharFeatures(fm_test_dna, DNA)
labels=BinaryLabels(label_train_dna)
kernel=SubsequenceStringKernel(feats_train, feats_train, maxlen, decay);

svm=LibSVM(C, kernel, labels);
svm=sg.machine("LibSVM", C1=C, C2=C, kernel=kernel, labels=labels);
svm.train();

out=svm.apply(feats_train);
Expand All @@ -32,7 +33,7 @@ def classifier_ssk (fm_train_dna=traindat,fm_test_dna=testdat,
# print(trainerr)

kernel.init(feats_train, feats_test)
predicted_labels=svm.apply(feats_test).get_labels()
predicted_labels=svm.apply(feats_test).get("labels")
# print predicted_labels

return predicted_labels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def evaluation_cross_validation_mkl_weight_storage(traindat=traindat, label_trai
from shogun import BinaryLabels
from shogun import RealFeatures, CombinedFeatures
from shogun import CombinedKernel
from shogun import LibSVM, MKLClassification
from shogun import MKLClassification
import shogun as sg
import numpy as np

Expand All @@ -43,7 +43,8 @@ def evaluation_cross_validation_mkl_weight_storage(traindat=traindat, label_trai
kernel.append_kernel(sg.kernel("GaussianKernel", log_width=np.log(2)))

# create mkl using libsvm, due to a mem-bug, interleaved is not possible
svm=MKLClassification(LibSVM());
svm=MKLClassification()
svm.put("svm", sg.svm("LibSVM"))
svm.set_interleaved_optimization_enabled(False);
svm.set_kernel(kernel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

def evaluation_multiclassovrevaluation(train_fname=traindat, label_fname=label_traindat):
from shogun import MulticlassOVREvaluation,ROCEvaluation
from shogun import MulticlassLibLinear,RealFeatures,ContingencyTableEvaluation,ACCURACY
from shogun import RealFeatures,ContingencyTableEvaluation,ACCURACY
from shogun import MulticlassLabels, Math, CSVFile
import shogun as sg

Math.init_random(1)
ground_truth_labels = MulticlassLabels(CSVFile(label_fname))
svm = MulticlassLibLinear(1.0,RealFeatures(CSVFile(train_fname)),ground_truth_labels)
svm = sg.machine("MulticlassLibLinear", C=1.0,
labels=ground_truth_labels)
svm.parallel.set_num_threads(1)
svm.train()
svm.train(RealFeatures(CSVFile(train_fname)))
predicted_labels = svm.apply()

binary_evaluator = ROCEvaluation()
Expand Down
7 changes: 4 additions & 3 deletions examples/undocumented/python/kernel_combined_custom_poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
def kernel_combined_custom_poly (train_fname = traindat,test_fname = testdat,train_label_fname=label_traindat):
from shogun import CombinedFeatures, RealFeatures, BinaryLabels
from shogun import CombinedKernel, PolyKernel, CustomKernel
from shogun import LibSVM, CSVFile
from shogun import CSVFile
import shogun as sg

kernel = CombinedKernel()
feats_train = CombinedFeatures()
Expand All @@ -29,7 +30,7 @@ def kernel_combined_custom_poly (train_fname = traindat,test_fname = testdat,tra
kernel.init(feats_train, feats_train)

labels = BinaryLabels(CSVFile(train_label_fname))
svm = LibSVM(1.0, kernel, labels)
svm = sg.machine("LibSVM", C1=1.0, C2=1.0, kernel=kernel, labels=labels)
svm.train()

kernel = CombinedKernel()
Expand All @@ -47,7 +48,7 @@ def kernel_combined_custom_poly (train_fname = traindat,test_fname = testdat,tra
kernel.append_kernel(subkernel)
kernel.init(feats_train, feats_pred)

svm.set_kernel(kernel)
svm.put("kernel", kernel)
svm.apply()
km_train=kernel.get_kernel_matrix()
return km_train,kernel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from shogun import BinaryLabels
from shogun import RealFeatures
from shogun import PowerKernel
from shogun import LibSVM
from shogun import MinkowskiMetric
from shogun import GridSearchModelSelection
from shogun import ModelSelectionParameters, R_EXP, R_LINEAR
Expand Down Expand Up @@ -94,7 +93,7 @@ def modelselection_grid_search_kernel (num_subsets, num_vectors, dim_vectors):
labels.set_label(i, 1 if i%2==0 else -1)

# create svm
classifier=LibSVM()
classifier=sg.machine("LibSVM")

# splitting strategy
splitting_strategy=StratifiedCrossValidationSplitting(labels, num_subsets)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def modelselection_grid_search_liblinear (traindat=traindat, label_traindat=labe
from shogun import ParameterCombination
from shogun import BinaryLabels
from shogun import RealFeatures
from shogun import LibLinear, L2R_L2LOSS_SVC
from shogun import L2R_L2LOSS_SVC
import shogun as sg

# build parameter tree to select C1 and C2
param_tree_root=ModelSelectionParameters()
Expand All @@ -42,7 +43,7 @@ def modelselection_grid_search_liblinear (traindat=traindat, label_traindat=labe
labels=BinaryLabels(label_traindat)

# classifier
classifier=LibLinear(L2R_L2LOSS_SVC)
classifier=sg.machine("LibLinear", liblinear_solver_type=L2R_L2LOSS_SVC)

# print all parameter available for modelselection
# Dont worry if yours is not included but, write to the mailing list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def modelselection_grid_search_libsvr (fm_train=traindat,fm_test=testdat,label_t
from shogun import CrossValidationSplitting
from shogun import RegressionLabels
from shogun import RealFeatures
from shogun import LibSVR
from shogun import GridSearchModelSelection
from shogun import ModelSelectionParameters, R_EXP
from shogun import ParameterCombination
Expand All @@ -45,8 +44,10 @@ def modelselection_grid_search_libsvr (fm_train=traindat,fm_test=testdat,label_t
labels=RegressionLabels(label_train)

# predictor
predictor=LibSVR(C, tube_epsilon, kernel, labels)
predictor.set_epsilon(epsilon)
predictor=sg.machine("LibSVR", C1=C, C2=C, tube_epsilon=tube_epsilon,
kernel=kernel,
labels=labels,
epsilon=epsilon)

# splitting strategy for 5 fold cross-validation (for classification its better
# to use "StratifiedCrossValidation", but the standard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def modelselection_random_search_liblinear (traindat=traindat, label_traindat=la
from shogun import ParameterCombination
from shogun import BinaryLabels
from shogun import RealFeatures
from shogun import LibLinear, L2R_L2LOSS_SVC
from shogun import L2R_L2LOSS_SVC
import shogun as sg

# build parameter tree to select C1 and C2
param_tree_root=ModelSelectionParameters()
Expand All @@ -42,7 +43,7 @@ def modelselection_random_search_liblinear (traindat=traindat, label_traindat=la
labels=BinaryLabels(label_traindat)

# classifier
classifier=LibLinear(L2R_L2LOSS_SVC)
classifier=sg.machine("LibLinear", liblinear_solver_type=L2R_L2LOSS_SVC)

# print all parameter available for modelselection
# Dont worry if yours is not included but, write to the mailing list
Expand Down
Loading

0 comments on commit 4cef4d1

Please sign in to comment.