Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3281 from sanuj/cookbook_svm
add kernel svm cookbook
- Loading branch information
Showing
18 changed files
with
98 additions
and
593 deletions.
There are no files selected for viewing
Submodule data
updated
2 files
+18 −0 | testsuite/meta/classifier/kernel_svm.dat | |
+10 −0 | testsuite/meta/classifier/relaxed_tree.dat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
============================= | ||
Kernel Support Vector Machine | ||
============================= | ||
|
||
Kernel Support Vector Machine is a binary classifier which finds a data-separating hyperplane in a Hilbert space induced by a positive definite kernel. The hyperplane is chosen to maximize the margins between the two classes. The loss function that needs to be minimized is: | ||
|
||
.. math:: | ||
\max_{\bf \alpha} \sum_{i=0}^{N-1} \alpha_i - \sum_{i=0}^{N-1}\sum_{j=0}^{N-1} \alpha_i y_i \alpha_j y_j k({\bf x}_i, {\bf x}_j) | ||
subject to: | ||
|
||
.. math:: | ||
0 \leq \alpha_i \leq C, \sum_{i=0}^{N-1} \alpha_i y_i = 0 | ||
where :math:`N` is the number of training samples, :math:`{\bf x}_i` are training samples, :math:`k` is a kernel, :math:`\alpha_i` are the weights, :math:`y_i` is the corresponding label where :math:`y_i \in \{-1,+1\}` and :math:`C` is a pre-specified regularization parameter. | ||
|
||
Implementation of Kernel SVM in Shogun uses LibSVM :cite:`chang2011libsvm`. | ||
|
||
See :cite:`scholkopf2002learning` and Chapter 6 in :cite:`cristianini2000introduction` for a detailed introduction. | ||
|
||
------- | ||
Example | ||
------- | ||
|
||
Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CBinaryLabels` as | ||
|
||
.. sgexample:: kernel_svm.sg:create_features | ||
|
||
In order to run :sgclass:`CLibSVM`, we need to initialize a kernel like :sgclass:`CGaussianKernel` with training features and some parameters like :math:`C` and epsilon i.e. residual convergence parameter which is optional. | ||
|
||
.. sgexample:: kernel_svm.sg:set_parameters | ||
|
||
We create an instance of the :sgclass:`CLibSVM` classifier by passing it regularization coefficient, kernel and labels. | ||
|
||
.. sgexample:: kernel_svm.sg:create_instance | ||
|
||
Then we train and apply it to test data, which here gives :sgclass:`CBinaryLabels`. | ||
|
||
.. sgexample:: kernel_svm.sg:train_and_apply | ||
|
||
We can extract :math:`\alpha` and :math:`b`. | ||
|
||
.. sgexample:: kernel_svm.sg:extract_weights_bias | ||
|
||
Finally, we can evaluate test performance via e.g. :sgclass:`CAccuracyMeasure`. | ||
|
||
.. sgexample:: kernel_svm.sg:evaluate_accuracy | ||
|
||
---------- | ||
References | ||
---------- | ||
:wiki:`Support_vector_machine` | ||
|
||
.. bibliography:: ../../references.bib | ||
:filter: docname in docnames |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
CSVFile f_feats_train("../../data/classifier_binary_2d_nonlinear_features_train.dat") | ||
CSVFile f_feats_test("../../data/classifier_binary_2d_nonlinear_features_test.dat") | ||
CSVFile f_labels_train("../../data/classifier_binary_2d_nonlinear_labels_train.dat") | ||
CSVFile f_labels_test("../../data/classifier_binary_2d_nonlinear_labels_test.dat") | ||
|
||
#![create_features] | ||
RealFeatures features_train(f_feats_train) | ||
RealFeatures features_test(f_feats_test) | ||
BinaryLabels labels_train(f_labels_train) | ||
BinaryLabels labels_test(f_labels_test) | ||
#![create_features] | ||
|
||
#![set_parameters] | ||
real C = 1 | ||
real epsilon = 0.001 | ||
GaussianKernel gauss_kernel(features_train, features_train, 15) | ||
#![set_parameters] | ||
|
||
#![create_instance] | ||
LibSVM svm(C, gauss_kernel, labels_train) | ||
svm.set_epsilon(epsilon) | ||
#![create_instance] | ||
|
||
#![train_and_apply] | ||
svm.train() | ||
BinaryLabels labels_predict = svm.apply_binary(features_test) | ||
#![train_and_apply] | ||
|
||
#![extract_weights_bias] | ||
RealVector alphas = svm.get_alphas() | ||
real b = svm.get_bias() | ||
#![extract_weights_bias] | ||
|
||
#![evaluate_accuracy] | ||
AccuracyMeasure eval() | ||
real accuracy = eval.evaluate(labels_predict, labels_test) | ||
#![evaluate_accuracy] | ||
|
||
# additional integration testing variables | ||
RealVector output = labels_predict.get_labels() |
65 changes: 0 additions & 65 deletions
65
examples/undocumented/csharp_modular/classifier_libsvm_minimal_modular.cs
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
examples/undocumented/csharp_modular/classifier_libsvm_modular.cs
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
examples/undocumented/java_modular/classifier_libsvm_minimal_modular.java
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
examples/undocumented/java_modular/classifier_libsvm_modular.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.