Printer optimized version of this page
[keine Deutsche Seite verfügbar]
|
Library Overview
The LIBSVMTL could be accessed on several layers,
which are
- Command line programs to use it directly from your shell
- SVMApplication class to write own command line programs
- Factory classes to use the library in your own
programs, where you have to select the algorithms at runtime
- Low level Classes to use the library in your own
programs, where you can select the algorithms already at
compile-time
These layers are described in more details below:
The Command line programs
The easiest way to use libsvmtl, is to call svmtl, svmtl_nc
or svmtl_sparse from your shell.
Parameters are passed via command
line, the feature vectors and Models could be stored in ASCII- or
NetCDF-Files.
Command Line Example: Training
A training data set with four classes and two feature vectors for
each class may look like this, if you want to use it with the svmtl
program:
5 0 0 0
2 1 0 0
1 0 1 0
-1 0 0 1
5 0.1 0 0
2 1.1 0 0
1 0.1 1 0
-1 0.1 0 1
Each line is one training vector, where the first number is the class
label (could be any positive or negative integer number, needs not to
be sorted, needs not to be sequential) and the rest are the features.
If this data is stored in the file "example.train", you may do a
training with default parameters (one_vs_one, c-svc, rbf-kernel) calling
svmtl train --modelfile example.model example.train
This will write a file "example.model", that contains the trained model.
Command Line Example: Classification
A test data set with some feature vectors may look like this:
0 0.2 0 1.3
0 0.9 1 0
0 1.2 3 42
0 -1 0 0
Each line is one test vector, where the first number (the class
label) is ignored and the rest are the features.
If this data is stored in the file "example.test" you may do the
classification with the model "example.model" from the former example
by calling
svmtl classify --details 0 --modelfile example.model --outfile example.predict example.test
this will write a file "example.predict" that contains the predicted
classes for each test vector, one per line, corresponding to the lines
in test data set, e.g.
-1
1
-1
5
SVMApplication class
The SVMApplication class provides a full command line application, that
can be modified by several policies (you may want to read "Modern C++ Design" of Andrei
Alexandrescu if you don't know what a Policy is.
the source code of the above used svmtl - program looks just like
this:
// ... some includes ...
typedef svt::SVMApplication< svt::BasicFV,
svt::AlgorithmLists< svt::DefaultMultiClassList,
svt::DefaultTwoClassList,
svt::DefaultOneClassList,
svt::DefaultKernelList>,
svt::LoadSaveASCII> SVMApplicationDense;
int main( int argc, const char** argv)
{
SVMApplicationDense sa;
return sa.main( argc, argv, std::cout);
}
As you can see, the SVMApplication-class needs a
Feature-Vector-Policy an Algorithm-Lists-Policy and a
Load-Save-Policy.
By exchanging these Policies, the other above
mentioned programs are created, e.g. for the svmtl_sparse program,
just a different Feature-Vector-Policy was used. The Source looks like
this (the changed parts are marked red):
// ... some includes ...
typedef svt::SVMApplication< svt::SparseFV,
svt::AlgorithmLists< svt::DefaultMultiClassList,
svt::DefaultTwoClassList,
svt::DefaultOneClassList,
svt::DefaultKernelList>,
svt::LoadSaveASCII> SVMApplicationSparse;
int main( int argc, const char** argv)
{
SVMApplicationSparse sa;
return sa.main( argc, argv, std::cout);
}
If you want to create an own command line program, that for example
only accepts the rbf, scaled_rbf, kmatrix_rbf and kmatrix_scaled_rbf
kernels, you may write something like
// ... same includes as above...
// define your own kernel list
typedef TLIST_4( svt::Kernel_RBF,
svt::Kernel_SCALE< svt::Kernel_RBF> >,
svt::Kernel_MATRIX< svt::Kernel_RBF>,
svt::Kernel_MATRIX< svt::Kernel_SCALE< svt::Kernel_RBF> >)
MyKernelList;
typedef svt::SVMApplication< svt::BasicFV,
svt::AlgorithmLists< svt::DefaultMultiClassList,
svt::DefaultTwoClassList,
svt::DefaultOneClassList,
MyKernelList>,
svt::LoadSaveASCII> SVMApplicationDense;
int main( int argc, const char** argv)
{
SVMApplicationDense sa;
return sa.main( argc, argv, std::cout);
}
Of course you may add your own Kernel-class to the type list. For
this, your Kernel class must only provide a k_function() method, the
methods loadParameters() and saveParameters(), that will be used to
pass the parameters from commandline, and from/to the model file, and
some description in name(), description() and getParamInfos(), that
are used for the help text when calling the application with "--help",
and last not least a method updateCache() that could
be empty if your kernel does not cache its results.
Factory classes
...
Low level classes
...
|