rsvm 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +6 -0
- data/Gemfile +6 -0
- data/README.md +89 -0
- data/Rakefile +41 -0
- data/ext/libsvm/COPYRIGHT +31 -0
- data/ext/libsvm/FAQ.html +1837 -0
- data/ext/libsvm/README +748 -0
- data/ext/libsvm/extconf.rb +3 -0
- data/ext/libsvm/svm.cpp +3213 -0
- data/ext/libsvm/svm.h +102 -0
- data/lib/svm.rb +119 -0
- data/lib/svm/cross_validation.rb +39 -0
- data/lib/svm/debug.rb +12 -0
- data/lib/svm/model.rb +68 -0
- data/lib/svm/options.rb +69 -0
- data/lib/svm/problem.rb +151 -0
- data/lib/svm/scaler.rb +82 -0
- data/lib/svm/version.rb +3 -0
- data/rsvm.gemspec +25 -0
- data/test/fixtures/heart_scale.csv +270 -0
- data/test/fixtures/unbalanced.csv +164 -0
- data/test/lib/cross_validation_test.rb +35 -0
- data/test/lib/model_test.rb +84 -0
- data/test/lib/problem_test.rb +39 -0
- data/test/lib/scaler_test.rb +57 -0
- data/test/test_helper.rb +3 -0
- metadata +101 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# RSVM
|
2
|
+
|
3
|
+
RSVM is a Ruby gem to perform Support Vector Machine classification and regresion
|
4
|
+
in Ruby. It is FFI wrapper of libsvm.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
problem = Svm::Problem.new
|
10
|
+
|
11
|
+
# These are the training samples. The first element in each array is the label
|
12
|
+
# for the sample, the rest is the sample coordinates.
|
13
|
+
|
14
|
+
problem.data = [
|
15
|
+
[1, 1, 0, 1],
|
16
|
+
[-1, -1, 0, -1]
|
17
|
+
]
|
18
|
+
|
19
|
+
# Generate a model from this problem
|
20
|
+
|
21
|
+
model = problem.generate_model(:kernel_type => :linear, :c => 10)
|
22
|
+
|
23
|
+
# And make predictions
|
24
|
+
|
25
|
+
model.predict([-1, 0, -1]) # - 1
|
26
|
+
model.predict([1, 0, 1]) # 1
|
27
|
+
|
28
|
+
# Models can be saved to a file
|
29
|
+
|
30
|
+
model.save(file.path)
|
31
|
+
|
32
|
+
loaded_model = Svm::Model.load(file.path)
|
33
|
+
|
34
|
+
loaded_model.predict([-1, 0, -1]) # -1
|
35
|
+
loaded_model.predict([1, 0, 1]) # 1
|
36
|
+
|
37
|
+
## Load data from csv
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
csv_path = File.join(File.dirname(__FILE__), '..', 'fixtures', 'heart_scale.csv')
|
41
|
+
problem = Svm::Problem.load_from_csv(csv_path)
|
42
|
+
```
|
43
|
+
|
44
|
+
For the Support Vector Machine to perform well the features in the samples data must
|
45
|
+
be of the same order of magnitude. RSVM can scale your data linearly to the [-1, 1] range.
|
46
|
+
|
47
|
+
## Scaling data
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
data = [
|
51
|
+
[1, 12.0, -7.6, 100_000, 0],
|
52
|
+
[2, 30.0, 0, -100_000, 0],
|
53
|
+
[3, 36.0, 7.6, 0, 0]
|
54
|
+
]
|
55
|
+
|
56
|
+
problem = Svm::Problem.new(data, scale: true)
|
57
|
+
```
|
58
|
+
|
59
|
+
## Estimate probabilities
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
|
63
|
+
problem.estimate_probabilities = true
|
64
|
+
model = problem.generate_model
|
65
|
+
|
66
|
+
sample = [60.0, 1.0, 3.0, 140.0, 185.0, 0.0, 2.0, 155.0, 0.0, 3.0, 2.0, 0.0, 3.0]
|
67
|
+
probs = model.predict_probabilities(sample)
|
68
|
+
|
69
|
+
# Return a hash with the probabilities associated with the sample
|
70
|
+
# {1=>0.4443737921739047, -1=>0.5556262078260953}
|
71
|
+
```
|
72
|
+
|
73
|
+
## Find parameters doing grid search
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
problem = Svm::Problem.load_from_csv(UNBALANCED_CSV)
|
77
|
+
n_folds = 3
|
78
|
+
|
79
|
+
# This will perform a grid search using each combination with c from 2^1 up to 2^14
|
80
|
+
# and gamma from 2^-13 up to 2^-1. For each combination it will use crossvalidation
|
81
|
+
# using 3 folds.
|
82
|
+
|
83
|
+
options = problem.find_best_parameters(n_folds)
|
84
|
+
|
85
|
+
# Result:
|
86
|
+
# {:c=>64, :gamma=>(1/128)}
|
87
|
+
```
|
88
|
+
|
89
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/clean'
|
5
|
+
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
LIB_EXT = RbConfig::CONFIG['DLEXT']
|
9
|
+
|
10
|
+
desc "Run unit tests"
|
11
|
+
Rake::TestTask.new do |test|
|
12
|
+
test.libs << "test"
|
13
|
+
test.test_files = Dir[ "test/**/*_test.rb"]
|
14
|
+
test.verbose = false
|
15
|
+
end
|
16
|
+
|
17
|
+
# rule to build the extension: this says
|
18
|
+
# that the extension should be rebuilt
|
19
|
+
# after any change to the files in ext
|
20
|
+
file "lib/libsvm/libsvm.#{LIB_EXT}" =>
|
21
|
+
Dir.glob("ext/libsvm/*{.rb,.c}") do
|
22
|
+
Dir.chdir("ext/libsvm") do
|
23
|
+
# this does essentially the same thing
|
24
|
+
# as what rubygems does
|
25
|
+
ruby "extconf.rb"
|
26
|
+
sh "make"
|
27
|
+
end
|
28
|
+
|
29
|
+
cp "ext/libsvm/libsvm.#{LIB_EXT}", "lib/libsvm/libsvm.#{LIB_EXT}"
|
30
|
+
end
|
31
|
+
|
32
|
+
# make the :test task depend on the shared
|
33
|
+
# object, so it will be built automatically
|
34
|
+
# before running the tests
|
35
|
+
task :test => "lib/libsvm/libsvm.#{LIB_EXT}"
|
36
|
+
|
37
|
+
# use 'rake clean' and 'rake clobber' to
|
38
|
+
# easily delete generated files
|
39
|
+
CLEAN.include("ext/**/*{.o,.log,.#{LIB_EXT}}")
|
40
|
+
CLEAN.include('ext/**/Makefile')
|
41
|
+
CLOBBER.include("lib/**/*.#{LIB_EXT}")
|
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
Copyright (c) 2000-2011 Chih-Chung Chang and Chih-Jen Lin
|
3
|
+
All rights reserved.
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
6
|
+
modification, are permitted provided that the following conditions
|
7
|
+
are met:
|
8
|
+
|
9
|
+
1. Redistributions of source code must retain the above copyright
|
10
|
+
notice, this list of conditions and the following disclaimer.
|
11
|
+
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright
|
13
|
+
notice, this list of conditions and the following disclaimer in the
|
14
|
+
documentation and/or other materials provided with the distribution.
|
15
|
+
|
16
|
+
3. Neither name of copyright holders nor the names of its contributors
|
17
|
+
may be used to endorse or promote products derived from this software
|
18
|
+
without specific prior written permission.
|
19
|
+
|
20
|
+
|
21
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
22
|
+
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
23
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
24
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
|
25
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
26
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
27
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
28
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
29
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
30
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
31
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/ext/libsvm/FAQ.html
ADDED
@@ -0,0 +1,1837 @@
|
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
<title>LIBSVM FAQ</title>
|
4
|
+
</head>
|
5
|
+
<body bgcolor="#ffffcc">
|
6
|
+
|
7
|
+
<a name="_TOP"><b><h1><a
|
8
|
+
href=http://www.csie.ntu.edu.tw/~cjlin/libsvm>LIBSVM</a> FAQ </h1></b></a>
|
9
|
+
<b>last modified : </b>
|
10
|
+
Tue, 11 Oct 2011 13:35:12 GMT
|
11
|
+
<class="categories">
|
12
|
+
<li><a
|
13
|
+
href="#_TOP">All Questions</a>(75)</li>
|
14
|
+
<ul><b>
|
15
|
+
<li><a
|
16
|
+
href="#/Q1:_Some_sample_uses_of_libsvm">Q1:_Some_sample_uses_of_libsvm</a>(2)</li>
|
17
|
+
<li><a
|
18
|
+
href="#/Q2:_Installation_and_running_the_program">Q2:_Installation_and_running_the_program</a>(12)</li>
|
19
|
+
<li><a
|
20
|
+
href="#/Q3:_Data_preparation">Q3:_Data_preparation</a>(7)</li>
|
21
|
+
<li><a
|
22
|
+
href="#/Q4:_Training_and_prediction">Q4:_Training_and_prediction</a>(33)</li>
|
23
|
+
<li><a
|
24
|
+
href="#/Q5:_Probability_outputs">Q5:_Probability_outputs</a>(3)</li>
|
25
|
+
<li><a
|
26
|
+
href="#/Q6:_Graphic_interface">Q6:_Graphic_interface</a>(3)</li>
|
27
|
+
<li><a
|
28
|
+
href="#/Q7:_Java_version_of_libsvm">Q7:_Java_version_of_libsvm</a>(4)</li>
|
29
|
+
<li><a
|
30
|
+
href="#/Q8:_Python_interface">Q8:_Python_interface</a>(1)</li>
|
31
|
+
<li><a
|
32
|
+
href="#/Q9:_MATLAB_interface">Q9:_MATLAB_interface</a>(10)</li>
|
33
|
+
</b></ul>
|
34
|
+
</li>
|
35
|
+
|
36
|
+
<ul><ul class="headlines">
|
37
|
+
<li class="headlines_item"><a href="#faq101">Some courses which have used libsvm as a tool</a></li>
|
38
|
+
<li class="headlines_item"><a href="#faq102">Some applications/tools which have used libsvm </a></li>
|
39
|
+
<li class="headlines_item"><a href="#f201">Where can I find documents/videos of libsvm ?</a></li>
|
40
|
+
<li class="headlines_item"><a href="#f202">Where are change log and earlier versions?</a></li>
|
41
|
+
<li class="headlines_item"><a href="#f203">How to cite LIBSVM?</a></li>
|
42
|
+
<li class="headlines_item"><a href="#f204">I would like to use libsvm in my software. Is there any license problem?</a></li>
|
43
|
+
<li class="headlines_item"><a href="#f205">Is there a repository of additional tools based on libsvm?</a></li>
|
44
|
+
<li class="headlines_item"><a href="#f206">On unix machines, I got "error in loading shared libraries" or "cannot open shared object file." What happened ? </a></li>
|
45
|
+
<li class="headlines_item"><a href="#f207">I have modified the source and would like to build the graphic interface "svm-toy" on MS windows. How should I do it ?</a></li>
|
46
|
+
<li class="headlines_item"><a href="#f208">I am an MS windows user but why only one (svm-toy) of those precompiled .exe actually runs ? </a></li>
|
47
|
+
<li class="headlines_item"><a href="#f209">What is the difference between "." and "*" outputed during training? </a></li>
|
48
|
+
<li class="headlines_item"><a href="#f210">Why occasionally the program (including MATLAB or other interfaces) crashes and gives a segmentation fault?</a></li>
|
49
|
+
<li class="headlines_item"><a href="#f211">How to build a dynamic library (.dll file) on MS windows?</a></li>
|
50
|
+
<li class="headlines_item"><a href="#f212">On some systems (e.g., Ubuntu), compiling LIBSVM gives many warning messages. Is this a problem and how to disable the warning message?</a></li>
|
51
|
+
<li class="headlines_item"><a href="#f301">Why sometimes not all attributes of a data appear in the training/model files ?</a></li>
|
52
|
+
<li class="headlines_item"><a href="#f302">What if my data are non-numerical ?</a></li>
|
53
|
+
<li class="headlines_item"><a href="#f303">Why do you consider sparse format ? Will the training of dense data be much slower ?</a></li>
|
54
|
+
<li class="headlines_item"><a href="#f304">Why sometimes the last line of my data is not read by svm-train?</a></li>
|
55
|
+
<li class="headlines_item"><a href="#f305">Is there a program to check if my data are in the correct format?</a></li>
|
56
|
+
<li class="headlines_item"><a href="#f306">May I put comments in data files?</a></li>
|
57
|
+
<li class="headlines_item"><a href="#f307">How to convert other data formats to LIBSVM format?</a></li>
|
58
|
+
<li class="headlines_item"><a href="#f401">The output of training C-SVM is like the following. What do they mean?</a></li>
|
59
|
+
<li class="headlines_item"><a href="#f402">Can you explain more about the model file?</a></li>
|
60
|
+
<li class="headlines_item"><a href="#f403">Should I use float or double to store numbers in the cache ?</a></li>
|
61
|
+
<li class="headlines_item"><a href="#f404">How do I choose the kernel?</a></li>
|
62
|
+
<li class="headlines_item"><a href="#f405">Does libsvm have special treatments for linear SVM?</a></li>
|
63
|
+
<li class="headlines_item"><a href="#f406">The number of free support vectors is large. What should I do?</a></li>
|
64
|
+
<li class="headlines_item"><a href="#f407">Should I scale training and testing data in a similar way?</a></li>
|
65
|
+
<li class="headlines_item"><a href="#f408">Does it make a big difference if I scale each attribute to [0,1] instead of [-1,1]?</a></li>
|
66
|
+
<li class="headlines_item"><a href="#f409">The prediction rate is low. How could I improve it?</a></li>
|
67
|
+
<li class="headlines_item"><a href="#f410">My data are unbalanced. Could libsvm handle such problems?</a></li>
|
68
|
+
<li class="headlines_item"><a href="#f411">What is the difference between nu-SVC and C-SVC?</a></li>
|
69
|
+
<li class="headlines_item"><a href="#f412">The program keeps running (without showing any output). What should I do?</a></li>
|
70
|
+
<li class="headlines_item"><a href="#f413">The program keeps running (with output, i.e. many dots). What should I do?</a></li>
|
71
|
+
<li class="headlines_item"><a href="#f414">The training time is too long. What should I do?</a></li>
|
72
|
+
<li class="headlines_item"><a href="#f4141">Does shrinking always help?</a></li>
|
73
|
+
<li class="headlines_item"><a href="#f415">How do I get the decision value(s)?</a></li>
|
74
|
+
<li class="headlines_item"><a href="#f4151">How do I get the distance between a point and the hyperplane?</a></li>
|
75
|
+
<li class="headlines_item"><a href="#f416">On 32-bit machines, if I use a large cache (i.e. large -m) on a linux machine, why sometimes I get "segmentation fault ?"</a></li>
|
76
|
+
<li class="headlines_item"><a href="#f417">How do I disable screen output of svm-train?</a></li>
|
77
|
+
<li class="headlines_item"><a href="#f418">I would like to use my own kernel. Any example? In svm.cpp, there are two subroutines for kernel evaluations: k_function() and kernel_function(). Which one should I modify ?</a></li>
|
78
|
+
<li class="headlines_item"><a href="#f419">What method does libsvm use for multi-class SVM ? Why don't you use the "1-against-the rest" method ?</a></li>
|
79
|
+
<li class="headlines_item"><a href="#f420">After doing cross validation, why there is no model file outputted ?</a></li>
|
80
|
+
<li class="headlines_item"><a href="#f4201">Why my cross-validation results are different from those in the Practical Guide?</a></li>
|
81
|
+
<li class="headlines_item"><a href="#f421">On some systems CV accuracy is the same in several runs. How could I use different data partitions? In other words, how do I set random seed in LIBSVM?</a></li>
|
82
|
+
<li class="headlines_item"><a href="#f422">I would like to solve L2-loss SVM (i.e., error term is quadratic). How should I modify the code ?</a></li>
|
83
|
+
<li class="headlines_item"><a href="#f424">How do I choose parameters for one-class svm as training data are in only one class?</a></li>
|
84
|
+
<li class="headlines_item"><a href="#f427">Why the code gives NaN (not a number) results?</a></li>
|
85
|
+
<li class="headlines_item"><a href="#f428">Why on windows sometimes grid.py fails?</a></li>
|
86
|
+
<li class="headlines_item"><a href="#f429">Why grid.py/easy.py sometimes generates the following warning message?</a></li>
|
87
|
+
<li class="headlines_item"><a href="#f430">Why the sign of predicted labels and decision values are sometimes reversed?</a></li>
|
88
|
+
<li class="headlines_item"><a href="#f431">I don't know class labels of test data. What should I put in the first column of the test file?</a></li>
|
89
|
+
<li class="headlines_item"><a href="#f432">How can I use OpenMP to parallelize LIBSVM on a multicore/shared-memory computer?</a></li>
|
90
|
+
<li class="headlines_item"><a href="#f433">How could I know which training instances are support vectors?</a></li>
|
91
|
+
<li class="headlines_item"><a href="#f425">Why training a probability model (i.e., -b 1) takes a longer time?</a></li>
|
92
|
+
<li class="headlines_item"><a href="#f426">Why using the -b option does not give me better accuracy?</a></li>
|
93
|
+
<li class="headlines_item"><a href="#f427">Why using svm-predict -b 0 and -b 1 gives different accuracy values?</a></li>
|
94
|
+
<li class="headlines_item"><a href="#f501">How can I save images drawn by svm-toy?</a></li>
|
95
|
+
<li class="headlines_item"><a href="#f502">I press the "load" button to load data points but why svm-toy does not draw them ?</a></li>
|
96
|
+
<li class="headlines_item"><a href="#f503">I would like svm-toy to handle more than three classes of data, what should I do ?</a></li>
|
97
|
+
<li class="headlines_item"><a href="#f601">What is the difference between Java version and C++ version of libsvm?</a></li>
|
98
|
+
<li class="headlines_item"><a href="#f602">Is the Java version significantly slower than the C++ version?</a></li>
|
99
|
+
<li class="headlines_item"><a href="#f603">While training I get the following error message: java.lang.OutOfMemoryError. What is wrong?</a></li>
|
100
|
+
<li class="headlines_item"><a href="#f604">Why you have the main source file svm.m4 and then transform it to svm.java?</a></li>
|
101
|
+
<li class="headlines_item"><a href="#f704">Except the python-C++ interface provided, could I use Jython to call libsvm ?</a></li>
|
102
|
+
<li class="headlines_item"><a href="#f801">I compile the MATLAB interface without problem, but why errors occur while running it?</a></li>
|
103
|
+
<li class="headlines_item"><a href="#f8011">On 64bit Windows I compile the MATLAB interface without problem, but why errors occur while running it?</a></li>
|
104
|
+
<li class="headlines_item"><a href="#f802">Does the MATLAB interface provide a function to do scaling?</a></li>
|
105
|
+
<li class="headlines_item"><a href="#f803">How could I use MATLAB interface for parameter selection?</a></li>
|
106
|
+
<li class="headlines_item"><a href="#f8031">I use MATLAB parallel programming toolbox on a multi-core environment for parameter selection. Why the program is even slower?</a></li>
|
107
|
+
<li class="headlines_item"><a href="#f8032">How do I use LIBSVM with OpenMP under MATLAB?</a></li>
|
108
|
+
<li class="headlines_item"><a href="#f804">How could I generate the primal variable w of linear SVM?</a></li>
|
109
|
+
<li class="headlines_item"><a href="#f805">Is there an OCTAVE interface for libsvm?</a></li>
|
110
|
+
<li class="headlines_item"><a href="#f806">How to handle the name conflict between svmtrain in the libsvm matlab interface and that in MATLAB bioinformatics toolbox?</a></li>
|
111
|
+
<li class="headlines_item"><a href="#f807">On Windows I got an error message "Invalid MEX-file: Specific module not found" when running the pre-built MATLAB interface in the windows sub-directory. What should I do?</a></li>
|
112
|
+
</ul></ul>
|
113
|
+
|
114
|
+
|
115
|
+
<hr size="5" noshade />
|
116
|
+
<p/>
|
117
|
+
|
118
|
+
<a name="/Q1:_Some_sample_uses_of_libsvm"></a>
|
119
|
+
<a name="faq101"><b>Q: Some courses which have used libsvm as a tool</b></a>
|
120
|
+
<br/>
|
121
|
+
<ul>
|
122
|
+
<li><a href=http://lmb.informatik.uni-freiburg.de/lectures/svm_seminar/>Institute for Computer Science,
|
123
|
+
Faculty of Applied Science, University of Freiburg, Germany
|
124
|
+
</a>
|
125
|
+
<li> <a href=http://www.cs.vu.nl/~elena/ml.html>
|
126
|
+
Division of Mathematics and Computer Science.
|
127
|
+
Faculteit der Exacte Wetenschappen
|
128
|
+
Vrije Universiteit, The Netherlands. </a>
|
129
|
+
<li>
|
130
|
+
<a href=http://www.cae.wisc.edu/~ece539/matlab/>
|
131
|
+
Electrical and Computer Engineering Department,
|
132
|
+
University of Wisconsin-Madison
|
133
|
+
</a>
|
134
|
+
<li>
|
135
|
+
<a href=http://www.hpl.hp.com/personal/Carl_Staelin/cs236601/project.html>
|
136
|
+
Technion (Israel Institute of Technology), Israel.
|
137
|
+
<li>
|
138
|
+
<a href=http://www.cise.ufl.edu/~fu/learn.html>
|
139
|
+
Computer and Information Sciences Dept., University of Florida</a>
|
140
|
+
<li>
|
141
|
+
<a href=http://www.uonbi.ac.ke/acad_depts/ics/course_material/machine_learning/ML_and_DM_Resources.html>
|
142
|
+
The Institute of Computer Science,
|
143
|
+
University of Nairobi, Kenya.</a>
|
144
|
+
<li>
|
145
|
+
<a href=http://cerium.raunvis.hi.is/~tpr/courseware/svm/hugbunadur.html>
|
146
|
+
Applied Mathematics and Computer Science, University of Iceland.
|
147
|
+
<li>
|
148
|
+
<a href=http://chicago05.mlss.cc/tiki/tiki-read_article.php?articleId=2>
|
149
|
+
SVM tutorial in machine learning
|
150
|
+
summer school, University of Chicago, 2005.
|
151
|
+
</a>
|
152
|
+
</ul>
|
153
|
+
<p align="right">
|
154
|
+
<a href="#_TOP">[Go Top]</a>
|
155
|
+
<hr/>
|
156
|
+
<a name="/Q1:_Some_sample_uses_of_libsvm"></a>
|
157
|
+
<a name="faq102"><b>Q: Some applications/tools which have used libsvm </b></a>
|
158
|
+
<br/>
|
159
|
+
(and maybe liblinear).
|
160
|
+
<ul>
|
161
|
+
<li>
|
162
|
+
<a href=http://people.csail.mit.edu/jjl/libpmk/>LIBPMK: A Pyramid Match Toolkit</a>
|
163
|
+
</li>
|
164
|
+
<li><a href=http://maltparser.org/>Maltparser</a>:
|
165
|
+
a system for data-driven dependency parsing
|
166
|
+
</li>
|
167
|
+
<li>
|
168
|
+
<a href=http://www.pymvpa.org/>PyMVPA: python tool for classifying neuroimages</a>
|
169
|
+
</li>
|
170
|
+
<li>
|
171
|
+
<a href=http://solpro.proteomics.ics.uci.edu/>
|
172
|
+
SOLpro: protein solubility predictor
|
173
|
+
</a>
|
174
|
+
</li>
|
175
|
+
<li>
|
176
|
+
<a href=http://bdval.campagnelab.org>
|
177
|
+
BDVal</a>: biomarker discovery in high-throughput datasets.
|
178
|
+
</li>
|
179
|
+
<li><a href=http://johel.m.free.fr/demo_045.htm>
|
180
|
+
Realtime object recognition</a>
|
181
|
+
</li>
|
182
|
+
<li><a href=http://scikit-learn.sourceforge.net/>
|
183
|
+
scikits.learn: machine learning in Python</a>
|
184
|
+
</li>
|
185
|
+
</ul>
|
186
|
+
<p align="right">
|
187
|
+
<a href="#_TOP">[Go Top]</a>
|
188
|
+
<hr/>
|
189
|
+
<a name="/Q2:_Installation_and_running_the_program"></a>
|
190
|
+
<a name="f201"><b>Q: Where can I find documents/videos of libsvm ?</b></a>
|
191
|
+
<br/>
|
192
|
+
<p>
|
193
|
+
|
194
|
+
<ul>
|
195
|
+
<li>
|
196
|
+
Official implementation document:
|
197
|
+
<br>
|
198
|
+
C.-C. Chang and
|
199
|
+
C.-J. Lin.
|
200
|
+
LIBSVM
|
201
|
+
: a library for support vector machines.
|
202
|
+
ACM Transactions on Intelligent
|
203
|
+
Systems and Technology, 2:27:1--27:27, 2011.
|
204
|
+
<a href="http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf">pdf</a>, <a href=http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.ps.gz>ps.gz</a>,
|
205
|
+
<a href=http://portal.acm.org/citation.cfm?id=1961199&CFID=29950432&CFTOKEN=30974232>ACM digital lib</a>.
|
206
|
+
|
207
|
+
|
208
|
+
<li> Instructions for using LIBSVM are in the README files in the main directory and some sub-directories.
|
209
|
+
<br>
|
210
|
+
README in the main directory: details all options, data format, and library calls.
|
211
|
+
<br>
|
212
|
+
tools/README: parameter selection and other tools
|
213
|
+
<li>
|
214
|
+
A guide for beginners:
|
215
|
+
<br>
|
216
|
+
C.-W. Hsu, C.-C. Chang, and
|
217
|
+
C.-J. Lin.
|
218
|
+
<A HREF="http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf">
|
219
|
+
A practical guide to support vector classification
|
220
|
+
</A>
|
221
|
+
<li> An <a href=http://www.youtube.com/watch?v=gePWtNAQcK8>introductory video</a>
|
222
|
+
for windows users.
|
223
|
+
|
224
|
+
</ul>
|
225
|
+
<p align="right">
|
226
|
+
<a href="#_TOP">[Go Top]</a>
|
227
|
+
<hr/>
|
228
|
+
<a name="/Q2:_Installation_and_running_the_program"></a>
|
229
|
+
<a name="f202"><b>Q: Where are change log and earlier versions?</b></a>
|
230
|
+
<br/>
|
231
|
+
<p>See <a href="http://www.csie.ntu.edu.tw/~cjlin/libsvm/log">the change log</a>.
|
232
|
+
|
233
|
+
<p> You can download earlier versions
|
234
|
+
<a href="http://www.csie.ntu.edu.tw/~cjlin/libsvm/oldfiles">here</a>.
|
235
|
+
<p align="right">
|
236
|
+
<a href="#_TOP">[Go Top]</a>
|
237
|
+
<hr/>
|
238
|
+
<a name="/Q2:_Installation_and_running_the_program"></a>
|
239
|
+
<a name="f203"><b>Q: How to cite LIBSVM?</b></a>
|
240
|
+
<br/>
|
241
|
+
<p>
|
242
|
+
Please cite the following paper:
|
243
|
+
<p>
|
244
|
+
Chih-Chung Chang and Chih-Jen Lin, LIBSVM
|
245
|
+
: a library for support vector machines.
|
246
|
+
ACM Transactions on Intelligent Systems and Technology, 2:27:1--27:27, 2011.
|
247
|
+
Software available at http://www.csie.ntu.edu.tw/~cjlin/libsvm
|
248
|
+
<p>
|
249
|
+
The bibtex format is
|
250
|
+
<pre>
|
251
|
+
@article{CC01a,
|
252
|
+
author = {Chang, Chih-Chung and Lin, Chih-Jen},
|
253
|
+
title = {{LIBSVM}: A library for support vector machines},
|
254
|
+
journal = {ACM Transactions on Intelligent Systems and Technology},
|
255
|
+
volume = {2},
|
256
|
+
issue = {3},
|
257
|
+
year = {2011},
|
258
|
+
pages = {27:1--27:27},
|
259
|
+
note = {Software available at \url{http://www.csie.ntu.edu.tw/~cjlin/libsvm}}
|
260
|
+
}
|
261
|
+
</pre>
|
262
|
+
<p align="right">
|
263
|
+
<a href="#_TOP">[Go Top]</a>
|
264
|
+
<hr/>
|
265
|
+
<a name="/Q2:_Installation_and_running_the_program"></a>
|
266
|
+
<a name="f204"><b>Q: I would like to use libsvm in my software. Is there any license problem?</b></a>
|
267
|
+
<br/>
|
268
|
+
<p>
|
269
|
+
The libsvm license ("the modified BSD license")
|
270
|
+
is compatible with many
|
271
|
+
free software licenses such as GPL. Hence, it is very easy to
|
272
|
+
use libsvm in your software.
|
273
|
+
Please check the COPYRIGHT file in detail. Basically
|
274
|
+
you need to
|
275
|
+
<ol>
|
276
|
+
<li>
|
277
|
+
Clearly indicate that LIBSVM is used.
|
278
|
+
</li>
|
279
|
+
<li>
|
280
|
+
Retain the LIBSVM COPYRIGHT file in your software.
|
281
|
+
</li>
|
282
|
+
</ol>
|
283
|
+
It can also be used in commercial products.
|
284
|
+
<p align="right">
|
285
|
+
<a href="#_TOP">[Go Top]</a>
|
286
|
+
<hr/>
|
287
|
+
<a name="/Q2:_Installation_and_running_the_program"></a>
|
288
|
+
<a name="f205"><b>Q: Is there a repository of additional tools based on libsvm?</b></a>
|
289
|
+
<br/>
|
290
|
+
<p>
|
291
|
+
Yes, see <a href="http://www.csie.ntu.edu.tw/~cjlin/libsvmtools">libsvm
|
292
|
+
tools</a>
|
293
|
+
<p align="right">
|
294
|
+
<a href="#_TOP">[Go Top]</a>
|
295
|
+
<hr/>
|
296
|
+
<a name="/Q2:_Installation_and_running_the_program"></a>
|
297
|
+
<a name="f206"><b>Q: On unix machines, I got "error in loading shared libraries" or "cannot open shared object file." What happened ? </b></a>
|
298
|
+
<br/>
|
299
|
+
|
300
|
+
<p>
|
301
|
+
This usually happens if you compile the code
|
302
|
+
on one machine and run it on another which has incompatible
|
303
|
+
libraries.
|
304
|
+
Try to recompile the program on that machine or use static linking.
|
305
|
+
<p align="right">
|
306
|
+
<a href="#_TOP">[Go Top]</a>
|
307
|
+
<hr/>
|
308
|
+
<a name="/Q2:_Installation_and_running_the_program"></a>
|
309
|
+
<a name="f207"><b>Q: I have modified the source and would like to build the graphic interface "svm-toy" on MS windows. How should I do it ?</b></a>
|
310
|
+
<br/>
|
311
|
+
|
312
|
+
<p>
|
313
|
+
Build it as a project by choosing "Win32 Project."
|
314
|
+
On the other hand, for "svm-train" and "svm-predict"
|
315
|
+
you want to choose "Win32 Console Project."
|
316
|
+
After libsvm 2.5, you can also use the file Makefile.win.
|
317
|
+
See details in README.
|
318
|
+
|
319
|
+
|
320
|
+
<p>
|
321
|
+
If you are not using Makefile.win and see the following
|
322
|
+
link error
|
323
|
+
<pre>
|
324
|
+
LIBCMTD.lib(wwincrt0.obj) : error LNK2001: unresolved external symbol
|
325
|
+
_wWinMain@16
|
326
|
+
</pre>
|
327
|
+
you may have selected a wrong project type.
|
328
|
+
<p align="right">
|
329
|
+
<a href="#_TOP">[Go Top]</a>
|
330
|
+
<hr/>
|
331
|
+
<a name="/Q2:_Installation_and_running_the_program"></a>
|
332
|
+
<a name="f208"><b>Q: I am an MS windows user but why only one (svm-toy) of those precompiled .exe actually runs ? </b></a>
|
333
|
+
<br/>
|
334
|
+
|
335
|
+
<p>
|
336
|
+
You need to open a command window
|
337
|
+
and type svmtrain.exe to see all options.
|
338
|
+
Some examples are in README file.
|
339
|
+
<p align="right">
|
340
|
+
<a href="#_TOP">[Go Top]</a>
|
341
|
+
<hr/>
|
342
|
+
<a name="/Q2:_Installation_and_running_the_program"></a>
|
343
|
+
<a name="f209"><b>Q: What is the difference between "." and "*" outputed during training? </b></a>
|
344
|
+
<br/>
|
345
|
+
|
346
|
+
<p>
|
347
|
+
"." means every 1,000 iterations (or every #data
|
348
|
+
iterations is your #data is less than 1,000).
|
349
|
+
"*" means that after iterations of using
|
350
|
+
a smaller shrunk problem,
|
351
|
+
we reset to use the whole set. See the
|
352
|
+
<a href=../papers/libsvm.pdf>implementation document</a> for details.
|
353
|
+
<p align="right">
|
354
|
+
<a href="#_TOP">[Go Top]</a>
|
355
|
+
<hr/>
|
356
|
+
<a name="/Q2:_Installation_and_running_the_program"></a>
|
357
|
+
<a name="f210"><b>Q: Why occasionally the program (including MATLAB or other interfaces) crashes and gives a segmentation fault?</b></a>
|
358
|
+
<br/>
|
359
|
+
|
360
|
+
<p>
|
361
|
+
Very likely the program consumes too much memory than what the
|
362
|
+
operating system can provide. Try a smaller data and see if the
|
363
|
+
program still crashes.
|
364
|
+
<p align="right">
|
365
|
+
<a href="#_TOP">[Go Top]</a>
|
366
|
+
<hr/>
|
367
|
+
<a name="/Q2:_Installation_and_running_the_program"></a>
|
368
|
+
<a name="f211"><b>Q: How to build a dynamic library (.dll file) on MS windows?</b></a>
|
369
|
+
<br/>
|
370
|
+
<p>
|
371
|
+
|
372
|
+
The easiest way is to use Makefile.win.
|
373
|
+
See details in README.
|
374
|
+
|
375
|
+
Alternatively, you can use Visual C++. Here is
|
376
|
+
the example using Visual Studio .Net 2008:
|
377
|
+
<ol>
|
378
|
+
<li>Create a Win32 empty DLL project and set (in Project->$Project_Name
|
379
|
+
Properties...->Configuration) to "Release."
|
380
|
+
About how to create a new dynamic link library, please refer to
|
381
|
+
<a href=http://msdn2.microsoft.com/en-us/library/ms235636(VS.80).aspx>http://msdn2.microsoft.com/en-us/library/ms235636(VS.80).aspx</a>
|
382
|
+
|
383
|
+
<li> Add svm.cpp, svm.h to your project.
|
384
|
+
<li> Add __WIN32__ and _CRT_SECURE_NO_DEPRECATE to Preprocessor definitions (in
|
385
|
+
Project->$Project_Name Properties...->C/C++->Preprocessor)
|
386
|
+
<li> Set Create/Use Precompiled Header to Not Using Precompiled Headers
|
387
|
+
(in Project->$Project_Name Properties...->C/C++->Precompiled Headers)
|
388
|
+
<li> Set the path for the Modulation Definition File svm.def (in
|
389
|
+
Project->$Project_Name Properties...->Linker->input
|
390
|
+
<li> Build the DLL.
|
391
|
+
<li> Rename the dll file to libsvm.dll and move it to the correct path.
|
392
|
+
</ol>
|
393
|
+
|
394
|
+
|
395
|
+
<p align="right">
|
396
|
+
<a href="#_TOP">[Go Top]</a>
|
397
|
+
<hr/>
|
398
|
+
<a name="/Q2:_Installation_and_running_the_program"></a>
|
399
|
+
<a name="f212"><b>Q: On some systems (e.g., Ubuntu), compiling LIBSVM gives many warning messages. Is this a problem and how to disable the warning message?</b></a>
|
400
|
+
<br/>
|
401
|
+
|
402
|
+
<p>
|
403
|
+
The warning message is like
|
404
|
+
<pre>
|
405
|
+
svm.cpp:2730: warning: ignoring return value of int fscanf(FILE*, const char*, ...), declared with attribute warn_unused_result
|
406
|
+
</pre>
|
407
|
+
but this is not a problem. In the future we may modify the code
|
408
|
+
so that these messages do not appear.
|
409
|
+
At this moment, to disable the warning message you can replace
|
410
|
+
<pre>
|
411
|
+
CFLAGS = -Wall -Wconversion -O3 -fPIC
|
412
|
+
</pre>
|
413
|
+
with
|
414
|
+
<pre>
|
415
|
+
CFLAGS = -Wall -Wconversion -O3 -fPIC -U_FORTIFY_SOURCE
|
416
|
+
</pre>
|
417
|
+
in Makefile.
|
418
|
+
<p align="right">
|
419
|
+
<a href="#_TOP">[Go Top]</a>
|
420
|
+
<hr/>
|
421
|
+
<a name="/Q3:_Data_preparation"></a>
|
422
|
+
<a name="f301"><b>Q: Why sometimes not all attributes of a data appear in the training/model files ?</b></a>
|
423
|
+
<br/>
|
424
|
+
<p>
|
425
|
+
libsvm uses the so called "sparse" format where zero
|
426
|
+
values do not need to be stored. Hence a data with attributes
|
427
|
+
<pre>
|
428
|
+
1 0 2 0
|
429
|
+
</pre>
|
430
|
+
is represented as
|
431
|
+
<pre>
|
432
|
+
1:1 3:2
|
433
|
+
</pre>
|
434
|
+
<p align="right">
|
435
|
+
<a href="#_TOP">[Go Top]</a>
|
436
|
+
<hr/>
|
437
|
+
<a name="/Q3:_Data_preparation"></a>
|
438
|
+
<a name="f302"><b>Q: What if my data are non-numerical ?</b></a>
|
439
|
+
<br/>
|
440
|
+
<p>
|
441
|
+
Currently libsvm supports only numerical data.
|
442
|
+
You may have to change non-numerical data to
|
443
|
+
numerical. For example, you can use several
|
444
|
+
binary attributes to represent a categorical
|
445
|
+
attribute.
|
446
|
+
<p align="right">
|
447
|
+
<a href="#_TOP">[Go Top]</a>
|
448
|
+
<hr/>
|
449
|
+
<a name="/Q3:_Data_preparation"></a>
|
450
|
+
<a name="f303"><b>Q: Why do you consider sparse format ? Will the training of dense data be much slower ?</b></a>
|
451
|
+
<br/>
|
452
|
+
<p>
|
453
|
+
This is a controversial issue. The kernel
|
454
|
+
evaluation (i.e. inner product) of sparse vectors is slower
|
455
|
+
so the total training time can be at least twice or three times
|
456
|
+
of that using the dense format.
|
457
|
+
However, we cannot support only dense format as then we CANNOT
|
458
|
+
handle extremely sparse cases. Simplicity of the code is another
|
459
|
+
concern. Right now we decide to support
|
460
|
+
the sparse format only.
|
461
|
+
<p align="right">
|
462
|
+
<a href="#_TOP">[Go Top]</a>
|
463
|
+
<hr/>
|
464
|
+
<a name="/Q3:_Data_preparation"></a>
|
465
|
+
<a name="f304"><b>Q: Why sometimes the last line of my data is not read by svm-train?</b></a>
|
466
|
+
<br/>
|
467
|
+
|
468
|
+
<p>
|
469
|
+
We assume that you have '\n' in the end of
|
470
|
+
each line. So please press enter in the end
|
471
|
+
of your last line.
|
472
|
+
<p align="right">
|
473
|
+
<a href="#_TOP">[Go Top]</a>
|
474
|
+
<hr/>
|
475
|
+
<a name="/Q3:_Data_preparation"></a>
|
476
|
+
<a name="f305"><b>Q: Is there a program to check if my data are in the correct format?</b></a>
|
477
|
+
<br/>
|
478
|
+
|
479
|
+
<p>
|
480
|
+
The svm-train program in libsvm conducts only a simple check of the input data. To do a
|
481
|
+
detailed check, after libsvm 2.85, you can use the python script tools/checkdata.py. See tools/README for details.
|
482
|
+
<p align="right">
|
483
|
+
<a href="#_TOP">[Go Top]</a>
|
484
|
+
<hr/>
|
485
|
+
<a name="/Q3:_Data_preparation"></a>
|
486
|
+
<a name="f306"><b>Q: May I put comments in data files?</b></a>
|
487
|
+
<br/>
|
488
|
+
|
489
|
+
<p>
|
490
|
+
We don't officially support this. But, cureently LIBSVM
|
491
|
+
is able to process data in the following
|
492
|
+
format:
|
493
|
+
<pre>
|
494
|
+
1 1:2 2:1 # your comments
|
495
|
+
</pre>
|
496
|
+
Note that the character ":" should not appear in your
|
497
|
+
comments.
|
498
|
+
<!--
|
499
|
+
No, for simplicity we don't support that.
|
500
|
+
However, you can easily preprocess your data before
|
501
|
+
using libsvm. For example,
|
502
|
+
if you have the following data
|
503
|
+
<pre>
|
504
|
+
test.txt
|
505
|
+
1 1:2 2:1 # proten A
|
506
|
+
</pre>
|
507
|
+
then on unix machines you can do
|
508
|
+
<pre>
|
509
|
+
cut -d '#' -f 1 < test.txt > test.features
|
510
|
+
cut -d '#' -f 2 < test.txt > test.comments
|
511
|
+
svm-predict test.feature train.model test.predicts
|
512
|
+
paste -d '#' test.predicts test.comments | sed 's/#/ #/' > test.results
|
513
|
+
</pre>
|
514
|
+
-->
|
515
|
+
<p align="right">
|
516
|
+
<a href="#_TOP">[Go Top]</a>
|
517
|
+
<hr/>
|
518
|
+
<a name="/Q3:_Data_preparation"></a>
|
519
|
+
<a name="f307"><b>Q: How to convert other data formats to LIBSVM format?</b></a>
|
520
|
+
<br/>
|
521
|
+
|
522
|
+
<p>
|
523
|
+
It depends on your data format. A simple way is to use
|
524
|
+
libsvmwrite in the libsvm matlab/octave interface.
|
525
|
+
|
526
|
+
Take a CSV (comma-separated values) file
|
527
|
+
in UCI machine learning repository as an example.
|
528
|
+
We download <a href=http://archive.ics.uci.edu/ml/machine-learning-databases/spect/SPECTF.train>SPECTF.train</a>.
|
529
|
+
Labels are in the first column. The following steps produce
|
530
|
+
a file in the libsvm format.
|
531
|
+
<pre>
|
532
|
+
matlab> SPECTF = csvread('SPECTF.train'); % read a csv file
|
533
|
+
matlab> labels = SPECTF(:, 1); % labels from the 1st column
|
534
|
+
matlab> features = SPECTF(:, 2:end);
|
535
|
+
matlab> features_sparse = sparse(features); % features must be in a sparse matrix
|
536
|
+
matlab> libsvmwrite('SPECTFlibsvm.train', labels, features_sparse);
|
537
|
+
</pre>
|
538
|
+
The tranformed data are stored in SPECTFlibsvm.train.
|
539
|
+
|
540
|
+
<p>
|
541
|
+
Alternatively, you can use <a href="./faqfiles/convert.c">convert.c</a>
|
542
|
+
to convert CSV format to libsvm format.
|
543
|
+
<p align="right">
|
544
|
+
<a href="#_TOP">[Go Top]</a>
|
545
|
+
<hr/>
|
546
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
547
|
+
<a name="f401"><b>Q: The output of training C-SVM is like the following. What do they mean?</b></a>
|
548
|
+
<br/>
|
549
|
+
<br>optimization finished, #iter = 219
|
550
|
+
<br>nu = 0.431030
|
551
|
+
<br>obj = -100.877286, rho = 0.424632
|
552
|
+
<br>nSV = 132, nBSV = 107
|
553
|
+
<br>Total nSV = 132
|
554
|
+
<p>
|
555
|
+
obj is the optimal objective value of the dual SVM problem.
|
556
|
+
rho is the bias term in the decision function
|
557
|
+
sgn(w^Tx - rho).
|
558
|
+
nSV and nBSV are number of support vectors and bounded support
|
559
|
+
vectors (i.e., alpha_i = C). nu-svm is a somewhat equivalent
|
560
|
+
form of C-SVM where C is replaced by nu. nu simply shows the
|
561
|
+
corresponding parameter. More details are in
|
562
|
+
<a href="http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf">
|
563
|
+
libsvm document</a>.
|
564
|
+
<p align="right">
|
565
|
+
<a href="#_TOP">[Go Top]</a>
|
566
|
+
<hr/>
|
567
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
568
|
+
<a name="f402"><b>Q: Can you explain more about the model file?</b></a>
|
569
|
+
<br/>
|
570
|
+
|
571
|
+
<p>
|
572
|
+
After the parameters, each line represents a support vector.
|
573
|
+
Support vectors are listed in the order of "labels" listed earlier.
|
574
|
+
(i.e., those from the first class in the "labels" list are
|
575
|
+
grouped first, and so on.)
|
576
|
+
If k is the total number of classes,
|
577
|
+
in front of a support vector in class j, there are
|
578
|
+
k-1 coefficients
|
579
|
+
y*alpha where alpha are dual solution of the
|
580
|
+
following two class problems:
|
581
|
+
<br>
|
582
|
+
1 vs j, 2 vs j, ..., j-1 vs j, j vs j+1, j vs j+2, ..., j vs k
|
583
|
+
<br>
|
584
|
+
and y=1 in first j-1 coefficients, y=-1 in the remaining
|
585
|
+
k-j coefficients.
|
586
|
+
|
587
|
+
For example, if there are 4 classes, the file looks like:
|
588
|
+
|
589
|
+
<pre>
|
590
|
+
+-+-+-+--------------------+
|
591
|
+
|1|1|1| |
|
592
|
+
|v|v|v| SVs from class 1 |
|
593
|
+
|2|3|4| |
|
594
|
+
+-+-+-+--------------------+
|
595
|
+
|1|2|2| |
|
596
|
+
|v|v|v| SVs from class 2 |
|
597
|
+
|2|3|4| |
|
598
|
+
+-+-+-+--------------------+
|
599
|
+
|1|2|3| |
|
600
|
+
|v|v|v| SVs from class 3 |
|
601
|
+
|3|3|4| |
|
602
|
+
+-+-+-+--------------------+
|
603
|
+
|1|2|3| |
|
604
|
+
|v|v|v| SVs from class 4 |
|
605
|
+
|4|4|4| |
|
606
|
+
+-+-+-+--------------------+
|
607
|
+
</pre>
|
608
|
+
See also
|
609
|
+
<a href="#f804"> an illustration using
|
610
|
+
MATLAB/OCTAVE.</a>
|
611
|
+
<p align="right">
|
612
|
+
<a href="#_TOP">[Go Top]</a>
|
613
|
+
<hr/>
|
614
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
615
|
+
<a name="f403"><b>Q: Should I use float or double to store numbers in the cache ?</b></a>
|
616
|
+
<br/>
|
617
|
+
|
618
|
+
<p>
|
619
|
+
We have float as the default as you can store more numbers
|
620
|
+
in the cache.
|
621
|
+
In general this is good enough but for few difficult
|
622
|
+
cases (e.g. C very very large) where solutions are huge
|
623
|
+
numbers, it might be possible that the numerical precision is not
|
624
|
+
enough using only float.
|
625
|
+
<p align="right">
|
626
|
+
<a href="#_TOP">[Go Top]</a>
|
627
|
+
<hr/>
|
628
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
629
|
+
<a name="f404"><b>Q: How do I choose the kernel?</b></a>
|
630
|
+
<br/>
|
631
|
+
|
632
|
+
<p>
|
633
|
+
In general we suggest you to try the RBF kernel first.
|
634
|
+
A recent result by Keerthi and Lin
|
635
|
+
(<a href=http://www.csie.ntu.edu.tw/~cjlin/papers/limit.pdf>
|
636
|
+
download paper here</a>)
|
637
|
+
shows that if RBF is used with model selection,
|
638
|
+
then there is no need to consider the linear kernel.
|
639
|
+
The kernel matrix using sigmoid may not be positive definite
|
640
|
+
and in general it's accuracy is not better than RBF.
|
641
|
+
(see the paper by Lin and Lin
|
642
|
+
(<a href=http://www.csie.ntu.edu.tw/~cjlin/papers/tanh.pdf>
|
643
|
+
download paper here</a>).
|
644
|
+
Polynomial kernels are ok but if a high degree is used,
|
645
|
+
numerical difficulties tend to happen
|
646
|
+
(thinking about dth power of (<1) goes to 0
|
647
|
+
and (>1) goes to infinity).
|
648
|
+
<p align="right">
|
649
|
+
<a href="#_TOP">[Go Top]</a>
|
650
|
+
<hr/>
|
651
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
652
|
+
<a name="f405"><b>Q: Does libsvm have special treatments for linear SVM?</b></a>
|
653
|
+
<br/>
|
654
|
+
|
655
|
+
<p>
|
656
|
+
|
657
|
+
No, libsvm solves linear/nonlinear SVMs by the
|
658
|
+
same way.
|
659
|
+
Some tricks may save training/testing time if the
|
660
|
+
linear kernel is used,
|
661
|
+
so libsvm is <b>NOT</b> particularly efficient for linear SVM,
|
662
|
+
especially when
|
663
|
+
C is large and
|
664
|
+
the number of data is much larger
|
665
|
+
than the number of attributes.
|
666
|
+
You can either
|
667
|
+
<ul>
|
668
|
+
<li>
|
669
|
+
Use small C only. We have shown in the following paper
|
670
|
+
that after C is larger than a certain threshold,
|
671
|
+
the decision function is the same.
|
672
|
+
<p>
|
673
|
+
<a href="http://guppy.mpe.nus.edu.sg/~mpessk/">S. S. Keerthi</a>
|
674
|
+
and
|
675
|
+
<B>C.-J. Lin</B>.
|
676
|
+
<A HREF="papers/limit.pdf">
|
677
|
+
Asymptotic behaviors of support vector machines with
|
678
|
+
Gaussian kernel
|
679
|
+
</A>
|
680
|
+
.
|
681
|
+
<I><A HREF="http://mitpress.mit.edu/journal-home.tcl?issn=08997667">Neural Computation</A></I>, 15(2003), 1667-1689.
|
682
|
+
|
683
|
+
|
684
|
+
<li>
|
685
|
+
Check <a href=http://www.csie.ntu.edu.tw/~cjlin/liblinear>liblinear</a>,
|
686
|
+
which is designed for large-scale linear classification.
|
687
|
+
</ul>
|
688
|
+
|
689
|
+
<p> Please also see our <a href=../papers/guide/guide.pdf>SVM guide</a>
|
690
|
+
on the discussion of using RBF and linear
|
691
|
+
kernels.
|
692
|
+
<p align="right">
|
693
|
+
<a href="#_TOP">[Go Top]</a>
|
694
|
+
<hr/>
|
695
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
696
|
+
<a name="f406"><b>Q: The number of free support vectors is large. What should I do?</b></a>
|
697
|
+
<br/>
|
698
|
+
<p>
|
699
|
+
This usually happens when the data are overfitted.
|
700
|
+
If attributes of your data are in large ranges,
|
701
|
+
try to scale them. Then the region
|
702
|
+
of appropriate parameters may be larger.
|
703
|
+
Note that there is a scale program
|
704
|
+
in libsvm.
|
705
|
+
<p align="right">
|
706
|
+
<a href="#_TOP">[Go Top]</a>
|
707
|
+
<hr/>
|
708
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
709
|
+
<a name="f407"><b>Q: Should I scale training and testing data in a similar way?</b></a>
|
710
|
+
<br/>
|
711
|
+
<p>
|
712
|
+
Yes, you can do the following:
|
713
|
+
<pre>
|
714
|
+
> svm-scale -s scaling_parameters train_data > scaled_train_data
|
715
|
+
> svm-scale -r scaling_parameters test_data > scaled_test_data
|
716
|
+
</pre>
|
717
|
+
<p align="right">
|
718
|
+
<a href="#_TOP">[Go Top]</a>
|
719
|
+
<hr/>
|
720
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
721
|
+
<a name="f408"><b>Q: Does it make a big difference if I scale each attribute to [0,1] instead of [-1,1]?</b></a>
|
722
|
+
<br/>
|
723
|
+
|
724
|
+
<p>
|
725
|
+
For the linear scaling method, if the RBF kernel is
|
726
|
+
used and parameter selection is conducted, there
|
727
|
+
is no difference. Assume Mi and mi are
|
728
|
+
respectively the maximal and minimal values of the
|
729
|
+
ith attribute. Scaling to [0,1] means
|
730
|
+
<pre>
|
731
|
+
x'=(x-mi)/(Mi-mi)
|
732
|
+
</pre>
|
733
|
+
For [-1,1],
|
734
|
+
<pre>
|
735
|
+
x''=2(x-mi)/(Mi-mi)-1.
|
736
|
+
</pre>
|
737
|
+
In the RBF kernel,
|
738
|
+
<pre>
|
739
|
+
x'-y'=(x-y)/(Mi-mi), x''-y''=2(x-y)/(Mi-mi).
|
740
|
+
</pre>
|
741
|
+
Hence, using (C,g) on the [0,1]-scaled data is the
|
742
|
+
same as (C,g/2) on the [-1,1]-scaled data.
|
743
|
+
|
744
|
+
<p> Though the performance is the same, the computational
|
745
|
+
time may be different. For data with many zero entries,
|
746
|
+
[0,1]-scaling keeps the sparsity of input data and hence
|
747
|
+
may save the time.
|
748
|
+
<p align="right">
|
749
|
+
<a href="#_TOP">[Go Top]</a>
|
750
|
+
<hr/>
|
751
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
752
|
+
<a name="f409"><b>Q: The prediction rate is low. How could I improve it?</b></a>
|
753
|
+
<br/>
|
754
|
+
<p>
|
755
|
+
Try to use the model selection tool grid.py in the python
|
756
|
+
directory find
|
757
|
+
out good parameters. To see the importance of model selection,
|
758
|
+
please
|
759
|
+
see my talk:
|
760
|
+
<A HREF="http://www.csie.ntu.edu.tw/~cjlin/talks/freiburg.pdf">
|
761
|
+
A practical guide to support vector
|
762
|
+
classification
|
763
|
+
</A>
|
764
|
+
<p align="right">
|
765
|
+
<a href="#_TOP">[Go Top]</a>
|
766
|
+
<hr/>
|
767
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
768
|
+
<a name="f410"><b>Q: My data are unbalanced. Could libsvm handle such problems?</b></a>
|
769
|
+
<br/>
|
770
|
+
<p>
|
771
|
+
Yes, there is a -wi options. For example, if you use
|
772
|
+
<pre>
|
773
|
+
> svm-train -s 0 -c 10 -w1 1 -w-1 5 data_file
|
774
|
+
</pre>
|
775
|
+
<p>
|
776
|
+
the penalty for class "-1" is larger.
|
777
|
+
Note that this -w option is for C-SVC only.
|
778
|
+
<p align="right">
|
779
|
+
<a href="#_TOP">[Go Top]</a>
|
780
|
+
<hr/>
|
781
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
782
|
+
<a name="f411"><b>Q: What is the difference between nu-SVC and C-SVC?</b></a>
|
783
|
+
<br/>
|
784
|
+
<p>
|
785
|
+
Basically they are the same thing but with different
|
786
|
+
parameters. The range of C is from zero to infinity
|
787
|
+
but nu is always between [0,1]. A nice property
|
788
|
+
of nu is that it is related to the ratio of
|
789
|
+
support vectors and the ratio of the training
|
790
|
+
error.
|
791
|
+
<p align="right">
|
792
|
+
<a href="#_TOP">[Go Top]</a>
|
793
|
+
<hr/>
|
794
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
795
|
+
<a name="f412"><b>Q: The program keeps running (without showing any output). What should I do?</b></a>
|
796
|
+
<br/>
|
797
|
+
<p>
|
798
|
+
You may want to check your data. Each training/testing
|
799
|
+
data must be in one line. It cannot be separated.
|
800
|
+
In addition, you have to remove empty lines.
|
801
|
+
<p align="right">
|
802
|
+
<a href="#_TOP">[Go Top]</a>
|
803
|
+
<hr/>
|
804
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
805
|
+
<a name="f413"><b>Q: The program keeps running (with output, i.e. many dots). What should I do?</b></a>
|
806
|
+
<br/>
|
807
|
+
<p>
|
808
|
+
In theory libsvm guarantees to converge.
|
809
|
+
Therefore, this means you are
|
810
|
+
handling ill-conditioned situations
|
811
|
+
(e.g. too large/small parameters) so numerical
|
812
|
+
difficulties occur.
|
813
|
+
<p>
|
814
|
+
You may get better numerical stability by replacing
|
815
|
+
<pre>
|
816
|
+
typedef float Qfloat;
|
817
|
+
</pre>
|
818
|
+
in svm.cpp with
|
819
|
+
<pre>
|
820
|
+
typedef double Qfloat;
|
821
|
+
</pre>
|
822
|
+
That is, elements in the kernel cache are stored
|
823
|
+
in double instead of single. However, this means fewer elements
|
824
|
+
can be put in the kernel cache.
|
825
|
+
<p align="right">
|
826
|
+
<a href="#_TOP">[Go Top]</a>
|
827
|
+
<hr/>
|
828
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
829
|
+
<a name="f414"><b>Q: The training time is too long. What should I do?</b></a>
|
830
|
+
<br/>
|
831
|
+
<p>
|
832
|
+
For large problems, please specify enough cache size (i.e.,
|
833
|
+
-m).
|
834
|
+
Slow convergence may happen for some difficult cases (e.g. -c is large).
|
835
|
+
You can try to use a looser stopping tolerance with -e.
|
836
|
+
If that still doesn't work, you may train only a subset of the data.
|
837
|
+
You can use the program subset.py in the directory "tools"
|
838
|
+
to obtain a random subset.
|
839
|
+
|
840
|
+
<p>
|
841
|
+
If you have extremely large data and face this difficulty, please
|
842
|
+
contact us. We will be happy to discuss possible solutions.
|
843
|
+
|
844
|
+
<p> When using large -e, you may want to check if -h 0 (no shrinking) or -h 1 (shrinking) is faster.
|
845
|
+
See a related question below.
|
846
|
+
|
847
|
+
<p align="right">
|
848
|
+
<a href="#_TOP">[Go Top]</a>
|
849
|
+
<hr/>
|
850
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
851
|
+
<a name="f4141"><b>Q: Does shrinking always help?</b></a>
|
852
|
+
<br/>
|
853
|
+
<p>
|
854
|
+
If the number of iterations is high, then shrinking
|
855
|
+
often helps.
|
856
|
+
However, if the number of iterations is small
|
857
|
+
(e.g., you specify a large -e), then
|
858
|
+
probably using -h 0 (no shrinking) is better.
|
859
|
+
See the
|
860
|
+
<a href=../papers/libsvm.pdf>implementation document</a> for details.
|
861
|
+
<p align="right">
|
862
|
+
<a href="#_TOP">[Go Top]</a>
|
863
|
+
<hr/>
|
864
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
865
|
+
<a name="f415"><b>Q: How do I get the decision value(s)?</b></a>
|
866
|
+
<br/>
|
867
|
+
<p>
|
868
|
+
We print out decision values for regression. For classification,
|
869
|
+
we solve several binary SVMs for multi-class cases. You
|
870
|
+
can obtain values by easily calling the subroutine
|
871
|
+
svm_predict_values. Their corresponding labels
|
872
|
+
can be obtained from svm_get_labels.
|
873
|
+
Details are in
|
874
|
+
README of libsvm package.
|
875
|
+
|
876
|
+
<p>
|
877
|
+
We do not recommend the following. But if you would
|
878
|
+
like to get values for
|
879
|
+
TWO-class classification with labels +1 and -1
|
880
|
+
(note: +1 and -1 but not things like 5 and 10)
|
881
|
+
in the easiest way, simply add
|
882
|
+
<pre>
|
883
|
+
printf("%f\n", dec_values[0]*model->label[0]);
|
884
|
+
</pre>
|
885
|
+
after the line
|
886
|
+
<pre>
|
887
|
+
svm_predict_values(model, x, dec_values);
|
888
|
+
</pre>
|
889
|
+
of the file svm.cpp.
|
890
|
+
Positive (negative)
|
891
|
+
decision values correspond to data predicted as +1 (-1).
|
892
|
+
|
893
|
+
|
894
|
+
<p align="right">
|
895
|
+
<a href="#_TOP">[Go Top]</a>
|
896
|
+
<hr/>
|
897
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
898
|
+
<a name="f4151"><b>Q: How do I get the distance between a point and the hyperplane?</b></a>
|
899
|
+
<br/>
|
900
|
+
<p>
|
901
|
+
The distance is |decision_value| / |w|.
|
902
|
+
We have |w|^2 = w^Tw = alpha^T Q alpha = 2*(dual_obj + sum alpha_i).
|
903
|
+
Thus in svm.cpp please find the place
|
904
|
+
where we calculate the dual objective value
|
905
|
+
(i.e., the subroutine Solve())
|
906
|
+
and add a statement to print w^Tw.
|
907
|
+
|
908
|
+
<p align="right">
|
909
|
+
<a href="#_TOP">[Go Top]</a>
|
910
|
+
<hr/>
|
911
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
912
|
+
<a name="f416"><b>Q: On 32-bit machines, if I use a large cache (i.e. large -m) on a linux machine, why sometimes I get "segmentation fault ?"</b></a>
|
913
|
+
<br/>
|
914
|
+
<p>
|
915
|
+
|
916
|
+
On 32-bit machines, the maximum addressable
|
917
|
+
memory is 4GB. The Linux kernel uses 3:1
|
918
|
+
split which means user space is 3G and
|
919
|
+
kernel space is 1G. Although there are
|
920
|
+
3G user space, the maximum dynamic allocation
|
921
|
+
memory is 2G. So, if you specify -m near 2G,
|
922
|
+
the memory will be exhausted. And svm-train
|
923
|
+
will fail when it asks more memory.
|
924
|
+
For more details, please read
|
925
|
+
<a href=http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=3BA164F6.BAFA4FB%40daimi.au.dk>
|
926
|
+
this article</a>.
|
927
|
+
<p>
|
928
|
+
The easiest solution is to switch to a
|
929
|
+
64-bit machine.
|
930
|
+
Otherwise, there are two ways to solve this. If your
|
931
|
+
machine supports Intel's PAE (Physical Address
|
932
|
+
Extension), you can turn on the option HIGHMEM64G
|
933
|
+
in Linux kernel which uses 4G:4G split for
|
934
|
+
kernel and user space. If you don't, you can
|
935
|
+
try a software `tub' which can eliminate the 2G
|
936
|
+
boundary for dynamic allocated memory. The `tub'
|
937
|
+
is available at
|
938
|
+
<a href=http://www.bitwagon.com/tub.html>http://www.bitwagon.com/tub.html</a>.
|
939
|
+
|
940
|
+
|
941
|
+
<!--
|
942
|
+
|
943
|
+
This may happen only when the cache is large, but each cached row is
|
944
|
+
not large enough. <b>Note:</b> This problem is specific to
|
945
|
+
gnu C library which is used in linux.
|
946
|
+
The solution is as follows:
|
947
|
+
|
948
|
+
<p>
|
949
|
+
In our program we have malloc() which uses two methods
|
950
|
+
to allocate memory from kernel. One is
|
951
|
+
sbrk() and another is mmap(). sbrk is faster, but mmap
|
952
|
+
has a larger address
|
953
|
+
space. So malloc uses mmap only if the wanted memory size is larger
|
954
|
+
than some threshold (default 128k).
|
955
|
+
In the case where each row is not large enough (#elements < 128k/sizeof(float)) but we need a large cache ,
|
956
|
+
the address space for sbrk can be exhausted. The solution is to
|
957
|
+
lower the threshold to force malloc to use mmap
|
958
|
+
and increase the maximum number of chunks to allocate
|
959
|
+
with mmap.
|
960
|
+
|
961
|
+
<p>
|
962
|
+
Therefore, in the main program (i.e. svm-train.c) you want
|
963
|
+
to have
|
964
|
+
<pre>
|
965
|
+
#include <malloc.h>
|
966
|
+
</pre>
|
967
|
+
and then in main():
|
968
|
+
<pre>
|
969
|
+
mallopt(M_MMAP_THRESHOLD, 32768);
|
970
|
+
mallopt(M_MMAP_MAX,1000000);
|
971
|
+
</pre>
|
972
|
+
You can also set the environment variables instead
|
973
|
+
of writing them in the program:
|
974
|
+
<pre>
|
975
|
+
$ M_MMAP_MAX=1000000 M_MMAP_THRESHOLD=32768 ./svm-train .....
|
976
|
+
</pre>
|
977
|
+
More information can be found by
|
978
|
+
<pre>
|
979
|
+
$ info libc "Malloc Tunable Parameters"
|
980
|
+
</pre>
|
981
|
+
-->
|
982
|
+
<p align="right">
|
983
|
+
<a href="#_TOP">[Go Top]</a>
|
984
|
+
<hr/>
|
985
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
986
|
+
<a name="f417"><b>Q: How do I disable screen output of svm-train?</b></a>
|
987
|
+
<br/>
|
988
|
+
<p>
|
989
|
+
For commend-line users, use the -q option:
|
990
|
+
<pre>
|
991
|
+
> ./svm-train -q heart_scale
|
992
|
+
</pre>
|
993
|
+
<p>
|
994
|
+
For library users, set the global variable
|
995
|
+
<pre>
|
996
|
+
extern void (*svm_print_string) (const char *);
|
997
|
+
</pre>
|
998
|
+
to specify the output format. You can disable the output by the following steps:
|
999
|
+
<ol>
|
1000
|
+
<li>
|
1001
|
+
Declare a function to output nothing:
|
1002
|
+
<pre>
|
1003
|
+
void print_null(const char *s) {}
|
1004
|
+
</pre>
|
1005
|
+
</li>
|
1006
|
+
<li>
|
1007
|
+
Assign the output function of libsvm by
|
1008
|
+
<pre>
|
1009
|
+
svm_print_string = &print_null;
|
1010
|
+
</pre>
|
1011
|
+
</li>
|
1012
|
+
</ol>
|
1013
|
+
Finally, a way used in earlier libsvm
|
1014
|
+
is by updating svm.cpp from
|
1015
|
+
<pre>
|
1016
|
+
#if 1
|
1017
|
+
void info(const char *fmt,...)
|
1018
|
+
</pre>
|
1019
|
+
to
|
1020
|
+
<pre>
|
1021
|
+
#if 0
|
1022
|
+
void info(const char *fmt,...)
|
1023
|
+
</pre>
|
1024
|
+
<p align="right">
|
1025
|
+
<a href="#_TOP">[Go Top]</a>
|
1026
|
+
<hr/>
|
1027
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1028
|
+
<a name="f418"><b>Q: I would like to use my own kernel. Any example? In svm.cpp, there are two subroutines for kernel evaluations: k_function() and kernel_function(). Which one should I modify ?</b></a>
|
1029
|
+
<br/>
|
1030
|
+
<p>
|
1031
|
+
An example is "LIBSVM for string data" in LIBSVM Tools.
|
1032
|
+
<p>
|
1033
|
+
The reason why we have two functions is as follows.
|
1034
|
+
For the RBF kernel exp(-g |xi - xj|^2), if we calculate
|
1035
|
+
xi - xj first and then the norm square, there are 3n operations.
|
1036
|
+
Thus we consider exp(-g (|xi|^2 - 2dot(xi,xj) +|xj|^2))
|
1037
|
+
and by calculating all |xi|^2 in the beginning,
|
1038
|
+
the number of operations is reduced to 2n.
|
1039
|
+
This is for the training. For prediction we cannot
|
1040
|
+
do this so a regular subroutine using that 3n operations is
|
1041
|
+
needed.
|
1042
|
+
|
1043
|
+
The easiest way to have your own kernel is
|
1044
|
+
to put the same code in these two
|
1045
|
+
subroutines by replacing any kernel.
|
1046
|
+
<p align="right">
|
1047
|
+
<a href="#_TOP">[Go Top]</a>
|
1048
|
+
<hr/>
|
1049
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1050
|
+
<a name="f419"><b>Q: What method does libsvm use for multi-class SVM ? Why don't you use the "1-against-the rest" method ?</b></a>
|
1051
|
+
<br/>
|
1052
|
+
<p>
|
1053
|
+
It is one-against-one. We chose it after doing the following
|
1054
|
+
comparison:
|
1055
|
+
C.-W. Hsu and C.-J. Lin.
|
1056
|
+
<A HREF="http://www.csie.ntu.edu.tw/~cjlin/papers/multisvm.pdf">
|
1057
|
+
A comparison of methods
|
1058
|
+
for multi-class support vector machines
|
1059
|
+
</A>,
|
1060
|
+
<I>IEEE Transactions on Neural Networks</A></I>, 13(2002), 415-425.
|
1061
|
+
|
1062
|
+
<p>
|
1063
|
+
"1-against-the rest" is a good method whose performance
|
1064
|
+
is comparable to "1-against-1." We do the latter
|
1065
|
+
simply because its training time is shorter.
|
1066
|
+
<p align="right">
|
1067
|
+
<a href="#_TOP">[Go Top]</a>
|
1068
|
+
<hr/>
|
1069
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1070
|
+
<a name="f420"><b>Q: After doing cross validation, why there is no model file outputted ?</b></a>
|
1071
|
+
<br/>
|
1072
|
+
<p>
|
1073
|
+
Cross validation is used for selecting good parameters.
|
1074
|
+
After finding them, you want to re-train the whole
|
1075
|
+
data without the -v option.
|
1076
|
+
<p align="right">
|
1077
|
+
<a href="#_TOP">[Go Top]</a>
|
1078
|
+
<hr/>
|
1079
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1080
|
+
<a name="f4201"><b>Q: Why my cross-validation results are different from those in the Practical Guide?</b></a>
|
1081
|
+
<br/>
|
1082
|
+
<p>
|
1083
|
+
|
1084
|
+
Due to random partitions of
|
1085
|
+
the data, on different systems CV accuracy values
|
1086
|
+
may be different.
|
1087
|
+
<p align="right">
|
1088
|
+
<a href="#_TOP">[Go Top]</a>
|
1089
|
+
<hr/>
|
1090
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1091
|
+
<a name="f421"><b>Q: On some systems CV accuracy is the same in several runs. How could I use different data partitions? In other words, how do I set random seed in LIBSVM?</b></a>
|
1092
|
+
<br/>
|
1093
|
+
<p>
|
1094
|
+
If you use GNU C library,
|
1095
|
+
the default seed 1 is considered. Thus you always
|
1096
|
+
get the same result of running svm-train -v.
|
1097
|
+
To have different seeds, you can add the following code
|
1098
|
+
in svm-train.c:
|
1099
|
+
<pre>
|
1100
|
+
#include <time.h>
|
1101
|
+
</pre>
|
1102
|
+
and in the beginning of main(),
|
1103
|
+
<pre>
|
1104
|
+
srand(time(0));
|
1105
|
+
</pre>
|
1106
|
+
Alternatively, if you are not using GNU C library
|
1107
|
+
and would like to use a fixed seed, you can have
|
1108
|
+
<pre>
|
1109
|
+
srand(1);
|
1110
|
+
</pre>
|
1111
|
+
|
1112
|
+
<p>
|
1113
|
+
For Java, the random number generator
|
1114
|
+
is initialized using the time information.
|
1115
|
+
So results of two CV runs are different.
|
1116
|
+
To fix the seed, after version 3.1 (released
|
1117
|
+
in mid 2011), you can add
|
1118
|
+
<pre>
|
1119
|
+
svm.rand.setSeed(0);
|
1120
|
+
</pre>
|
1121
|
+
in the main() function of svm_train.java.
|
1122
|
+
<p align="right">
|
1123
|
+
<a href="#_TOP">[Go Top]</a>
|
1124
|
+
<hr/>
|
1125
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1126
|
+
<a name="f422"><b>Q: I would like to solve L2-loss SVM (i.e., error term is quadratic). How should I modify the code ?</b></a>
|
1127
|
+
<br/>
|
1128
|
+
<p>
|
1129
|
+
It is extremely easy. Taking c-svc for example, to solve
|
1130
|
+
<p>
|
1131
|
+
min_w w^Tw/2 + C \sum max(0, 1- (y_i w^Tx_i+b))^2,
|
1132
|
+
<p>
|
1133
|
+
only two
|
1134
|
+
places of svm.cpp have to be changed.
|
1135
|
+
First, modify the following line of
|
1136
|
+
solve_c_svc from
|
1137
|
+
<pre>
|
1138
|
+
s.Solve(l, SVC_Q(*prob,*param,y), minus_ones, y,
|
1139
|
+
alpha, Cp, Cn, param->eps, si, param->shrinking);
|
1140
|
+
</pre>
|
1141
|
+
to
|
1142
|
+
<pre>
|
1143
|
+
s.Solve(l, SVC_Q(*prob,*param,y), minus_ones, y,
|
1144
|
+
alpha, INF, INF, param->eps, si, param->shrinking);
|
1145
|
+
</pre>
|
1146
|
+
Second, in the class of SVC_Q, declare C as
|
1147
|
+
a private variable:
|
1148
|
+
<pre>
|
1149
|
+
double C;
|
1150
|
+
</pre>
|
1151
|
+
In the constructor replace
|
1152
|
+
<pre>
|
1153
|
+
for(int i=0;i<prob.l;i++)
|
1154
|
+
QD[i]= (Qfloat)(this->*kernel_function)(i,i);
|
1155
|
+
</pre>
|
1156
|
+
with
|
1157
|
+
<pre>
|
1158
|
+
this->C = param.C;
|
1159
|
+
for(int i=0;i<prob.l;i++)
|
1160
|
+
QD[i]= (Qfloat)(this->*kernel_function)(i,i)+0.5/C;
|
1161
|
+
</pre>
|
1162
|
+
Then in the subroutine get_Q, after the for loop, add
|
1163
|
+
<pre>
|
1164
|
+
if(i >= start && i < len)
|
1165
|
+
data[i] += 0.5/C;
|
1166
|
+
</pre>
|
1167
|
+
|
1168
|
+
<p>
|
1169
|
+
For one-class svm, the modification is exactly the same. For SVR, you don't need an if statement like the above. Instead, you only need a simple assignment:
|
1170
|
+
<pre>
|
1171
|
+
data[real_i] += 0.5/C;
|
1172
|
+
</pre>
|
1173
|
+
|
1174
|
+
|
1175
|
+
<p>
|
1176
|
+
For large linear L2-loss SVM, please use
|
1177
|
+
<a href=../liblinear>LIBLINEAR</a>.
|
1178
|
+
<p align="right">
|
1179
|
+
<a href="#_TOP">[Go Top]</a>
|
1180
|
+
<hr/>
|
1181
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1182
|
+
<a name="f424"><b>Q: How do I choose parameters for one-class svm as training data are in only one class?</b></a>
|
1183
|
+
<br/>
|
1184
|
+
<p>
|
1185
|
+
You have pre-specified true positive rate in mind and then search for
|
1186
|
+
parameters which achieve similar cross-validation accuracy.
|
1187
|
+
<p align="right">
|
1188
|
+
<a href="#_TOP">[Go Top]</a>
|
1189
|
+
<hr/>
|
1190
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1191
|
+
<a name="f427"><b>Q: Why the code gives NaN (not a number) results?</b></a>
|
1192
|
+
<br/>
|
1193
|
+
<p>
|
1194
|
+
This rarely happens, but few users reported the problem.
|
1195
|
+
It seems that their
|
1196
|
+
computers for training libsvm have the VPN client
|
1197
|
+
running. The VPN software has some bugs and causes this
|
1198
|
+
problem. Please try to close or disconnect the VPN client.
|
1199
|
+
<p align="right">
|
1200
|
+
<a href="#_TOP">[Go Top]</a>
|
1201
|
+
<hr/>
|
1202
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1203
|
+
<a name="f428"><b>Q: Why on windows sometimes grid.py fails?</b></a>
|
1204
|
+
<br/>
|
1205
|
+
<p>
|
1206
|
+
|
1207
|
+
This problem shouldn't happen after version
|
1208
|
+
2.85. If you are using earlier versions,
|
1209
|
+
please download the latest one.
|
1210
|
+
|
1211
|
+
<!--
|
1212
|
+
<p>
|
1213
|
+
If you are using earlier
|
1214
|
+
versions, the error message is probably
|
1215
|
+
<pre>
|
1216
|
+
Traceback (most recent call last):
|
1217
|
+
File "grid.py", line 349, in ?
|
1218
|
+
main()
|
1219
|
+
File "grid.py", line 344, in main
|
1220
|
+
redraw(db)
|
1221
|
+
File "grid.py", line 132, in redraw
|
1222
|
+
gnuplot.write("set term windows\n")
|
1223
|
+
IOError: [Errno 22] Invalid argument
|
1224
|
+
</pre>
|
1225
|
+
|
1226
|
+
<p>Please try to close gnuplot windows and rerun.
|
1227
|
+
If the problem still occurs, comment the following
|
1228
|
+
two lines in grid.py by inserting "#" in the beginning:
|
1229
|
+
<pre>
|
1230
|
+
redraw(db)
|
1231
|
+
redraw(db,1)
|
1232
|
+
</pre>
|
1233
|
+
Then you get accuracy only but not cross validation contours.
|
1234
|
+
-->
|
1235
|
+
<p align="right">
|
1236
|
+
<a href="#_TOP">[Go Top]</a>
|
1237
|
+
<hr/>
|
1238
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1239
|
+
<a name="f429"><b>Q: Why grid.py/easy.py sometimes generates the following warning message?</b></a>
|
1240
|
+
<br/>
|
1241
|
+
<pre>
|
1242
|
+
Warning: empty z range [62.5:62.5], adjusting to [61.875:63.125]
|
1243
|
+
Notice: cannot contour non grid data!
|
1244
|
+
</pre>
|
1245
|
+
<p>Nothing is wrong and please disregard the
|
1246
|
+
message. It is from gnuplot when drawing
|
1247
|
+
the contour.
|
1248
|
+
<p align="right">
|
1249
|
+
<a href="#_TOP">[Go Top]</a>
|
1250
|
+
<hr/>
|
1251
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1252
|
+
<a name="f430"><b>Q: Why the sign of predicted labels and decision values are sometimes reversed?</b></a>
|
1253
|
+
<br/>
|
1254
|
+
<p>Nothing is wrong. Very likely you have two labels +1/-1 and the first instance in your data
|
1255
|
+
has -1.
|
1256
|
+
Think about the case of labels +5/+10. Since
|
1257
|
+
SVM needs to use +1/-1, internally
|
1258
|
+
we map +5/+10 to +1/-1 according to which
|
1259
|
+
label appears first.
|
1260
|
+
Hence a positive decision value implies
|
1261
|
+
that we should predict the "internal" +1,
|
1262
|
+
which may not be the +1 in the input file.
|
1263
|
+
|
1264
|
+
<p align="right">
|
1265
|
+
<a href="#_TOP">[Go Top]</a>
|
1266
|
+
<hr/>
|
1267
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1268
|
+
<a name="f431"><b>Q: I don't know class labels of test data. What should I put in the first column of the test file?</b></a>
|
1269
|
+
<br/>
|
1270
|
+
<p>Any value is ok. In this situation, what you will use is the output file of svm-predict, which gives predicted class labels.
|
1271
|
+
|
1272
|
+
|
1273
|
+
<p align="right">
|
1274
|
+
<a href="#_TOP">[Go Top]</a>
|
1275
|
+
<hr/>
|
1276
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1277
|
+
<a name="f432"><b>Q: How can I use OpenMP to parallelize LIBSVM on a multicore/shared-memory computer?</b></a>
|
1278
|
+
<br/>
|
1279
|
+
|
1280
|
+
<p>It is very easy if you are using GCC 4.2
|
1281
|
+
or after.
|
1282
|
+
|
1283
|
+
<p> In Makefile, add -fopenmp to CFLAGS.
|
1284
|
+
|
1285
|
+
<p> In class SVC_Q of svm.cpp, modify the for loop
|
1286
|
+
of get_Q to:
|
1287
|
+
<pre>
|
1288
|
+
#pragma omp parallel for private(j)
|
1289
|
+
for(j=start;j<len;j++)
|
1290
|
+
</pre>
|
1291
|
+
<p> In the subroutine svm_predict_values of svm.cpp, add one line to the for loop:
|
1292
|
+
<pre>
|
1293
|
+
#pragma omp parallel for private(i)
|
1294
|
+
for(i=0;i<l;i++)
|
1295
|
+
kvalue[i] = Kernel::k_function(x,model->SV[i],model->param);
|
1296
|
+
</pre>
|
1297
|
+
For regression, you need a reduction clause for the variable sum:
|
1298
|
+
<pre>
|
1299
|
+
#pragma omp parallel for private(i) reduction(+:sum)
|
1300
|
+
for(i=0;i<model->l;i++)
|
1301
|
+
sum += sv_coef[i] * Kernel::k_function(x,model>SV[i],model>param);
|
1302
|
+
</pre>
|
1303
|
+
|
1304
|
+
<p> Then rebuild the package. Kernel evaluations in training/testing will be parallelized. An example of running this modification on
|
1305
|
+
an 8-core machine using the data set
|
1306
|
+
<a href=../libsvmtools/datasets/binary/ijcnn1.bz2>ijcnn1</a>:
|
1307
|
+
|
1308
|
+
<p> 8 cores:
|
1309
|
+
<pre>
|
1310
|
+
%setenv OMP_NUM_THREADS 8
|
1311
|
+
%time svm-train -c 16 -g 4 -m 400 ijcnn1
|
1312
|
+
27.1sec
|
1313
|
+
</pre>
|
1314
|
+
1 core:
|
1315
|
+
<pre>
|
1316
|
+
%setenv OMP_NUM_THREADS 1
|
1317
|
+
%time svm-train -c 16 -g 4 -m 400 ijcnn1
|
1318
|
+
79.8sec
|
1319
|
+
</pre>
|
1320
|
+
For this data, kernel evaluations take 80% of training time. In the above example, we assume you use csh. For bash, use
|
1321
|
+
<pre>
|
1322
|
+
export OMP_NUM_THREADS=8
|
1323
|
+
</pre>
|
1324
|
+
instead.
|
1325
|
+
|
1326
|
+
<p> For Python interface, you need to add the -lgomp link option:
|
1327
|
+
<pre>
|
1328
|
+
$(CXX) -lgomp -shared -dynamiclib svm.o -o libsvm.so.$(SHVER)
|
1329
|
+
</pre>
|
1330
|
+
<p align="right">
|
1331
|
+
<a href="#_TOP">[Go Top]</a>
|
1332
|
+
<hr/>
|
1333
|
+
<a name="/Q4:_Training_and_prediction"></a>
|
1334
|
+
<a name="f433"><b>Q: How could I know which training instances are support vectors?</b></a>
|
1335
|
+
<br/>
|
1336
|
+
|
1337
|
+
<p>
|
1338
|
+
It's very simple. Please replace
|
1339
|
+
<pre>
|
1340
|
+
if(nonzero[i]) model->SV[p++] = x[i];
|
1341
|
+
</pre>
|
1342
|
+
in svm_train() of svm.cpp with
|
1343
|
+
<pre>
|
1344
|
+
if(nonzero[i])
|
1345
|
+
{
|
1346
|
+
model->SV[p++] = x[i];
|
1347
|
+
info("%d\n", perm[i]);
|
1348
|
+
}
|
1349
|
+
</pre>
|
1350
|
+
If there are many requests, we may
|
1351
|
+
provide a function to return indices
|
1352
|
+
of support vectors. In the mean time,
|
1353
|
+
if you need such information in your code,
|
1354
|
+
you can add the array nonzero to the model
|
1355
|
+
structure. This array has the same size as
|
1356
|
+
the number of data, so alternatively you can
|
1357
|
+
store only indices of support vectors.
|
1358
|
+
|
1359
|
+
<p> If you use matlab interface, you can easily
|
1360
|
+
compare support vectors and training data to know
|
1361
|
+
the indices:
|
1362
|
+
<pre>
|
1363
|
+
[tmp index]=ismember(model.SVs, training_data,'rows');
|
1364
|
+
</pre>
|
1365
|
+
<p align="right">
|
1366
|
+
<a href="#_TOP">[Go Top]</a>
|
1367
|
+
<hr/>
|
1368
|
+
<a name="/Q5:_Probability_outputs"></a>
|
1369
|
+
<a name="f425"><b>Q: Why training a probability model (i.e., -b 1) takes a longer time?</b></a>
|
1370
|
+
<br/>
|
1371
|
+
<p>
|
1372
|
+
To construct this probability model, we internally conduct a
|
1373
|
+
cross validation, which is more time consuming than
|
1374
|
+
a regular training.
|
1375
|
+
Hence, in general you do parameter selection first without
|
1376
|
+
-b 1. You only use -b 1 when good parameters have been
|
1377
|
+
selected. In other words, you avoid using -b 1 and -v
|
1378
|
+
together.
|
1379
|
+
<p align="right">
|
1380
|
+
<a href="#_TOP">[Go Top]</a>
|
1381
|
+
<hr/>
|
1382
|
+
<a name="/Q5:_Probability_outputs"></a>
|
1383
|
+
<a name="f426"><b>Q: Why using the -b option does not give me better accuracy?</b></a>
|
1384
|
+
<br/>
|
1385
|
+
<p>
|
1386
|
+
There is absolutely no reason the probability outputs guarantee
|
1387
|
+
you better accuracy. The main purpose of this option is
|
1388
|
+
to provide you the probability estimates, but not to boost
|
1389
|
+
prediction accuracy. From our experience,
|
1390
|
+
after proper parameter selections, in general with
|
1391
|
+
and without -b have similar accuracy. Occasionally there
|
1392
|
+
are some differences.
|
1393
|
+
It is not recommended to compare the two under
|
1394
|
+
just a fixed parameter
|
1395
|
+
set as more differences will be observed.
|
1396
|
+
<p align="right">
|
1397
|
+
<a href="#_TOP">[Go Top]</a>
|
1398
|
+
<hr/>
|
1399
|
+
<a name="/Q5:_Probability_outputs"></a>
|
1400
|
+
<a name="f427"><b>Q: Why using svm-predict -b 0 and -b 1 gives different accuracy values?</b></a>
|
1401
|
+
<br/>
|
1402
|
+
<p>
|
1403
|
+
Let's just consider two-class classification here. After probability information is obtained in training,
|
1404
|
+
we do not have
|
1405
|
+
<p>
|
1406
|
+
prob > = 0.5 if and only if decision value >= 0.
|
1407
|
+
<p>
|
1408
|
+
So predictions may be different with -b 0 and 1.
|
1409
|
+
<p align="right">
|
1410
|
+
<a href="#_TOP">[Go Top]</a>
|
1411
|
+
<hr/>
|
1412
|
+
<a name="/Q6:_Graphic_interface"></a>
|
1413
|
+
<a name="f501"><b>Q: How can I save images drawn by svm-toy?</b></a>
|
1414
|
+
<br/>
|
1415
|
+
<p>
|
1416
|
+
For Microsoft windows, first press the "print screen" key on the keyboard.
|
1417
|
+
Open "Microsoft Paint"
|
1418
|
+
(included in Windows)
|
1419
|
+
and press "ctrl-v." Then you can clip
|
1420
|
+
the part of picture which you want.
|
1421
|
+
For X windows, you can
|
1422
|
+
use the program "xv" or "import" to grab the picture of the svm-toy window.
|
1423
|
+
<p align="right">
|
1424
|
+
<a href="#_TOP">[Go Top]</a>
|
1425
|
+
<hr/>
|
1426
|
+
<a name="/Q6:_Graphic_interface"></a>
|
1427
|
+
<a name="f502"><b>Q: I press the "load" button to load data points but why svm-toy does not draw them ?</b></a>
|
1428
|
+
<br/>
|
1429
|
+
<p>
|
1430
|
+
The program svm-toy assumes both attributes (i.e. x-axis and y-axis
|
1431
|
+
values) are in (0,1). Hence you want to scale your
|
1432
|
+
data to between a small positive number and
|
1433
|
+
a number less than but very close to 1.
|
1434
|
+
Moreover, class labels must be 1, 2, or 3
|
1435
|
+
(not 1.0, 2.0 or anything else).
|
1436
|
+
<p align="right">
|
1437
|
+
<a href="#_TOP">[Go Top]</a>
|
1438
|
+
<hr/>
|
1439
|
+
<a name="/Q6:_Graphic_interface"></a>
|
1440
|
+
<a name="f503"><b>Q: I would like svm-toy to handle more than three classes of data, what should I do ?</b></a>
|
1441
|
+
<br/>
|
1442
|
+
<p>
|
1443
|
+
Taking windows/svm-toy.cpp as an example, you need to
|
1444
|
+
modify it and the difference
|
1445
|
+
from the original file is as the following: (for five classes of
|
1446
|
+
data)
|
1447
|
+
<pre>
|
1448
|
+
30,32c30
|
1449
|
+
< RGB(200,0,200),
|
1450
|
+
< RGB(0,160,0),
|
1451
|
+
< RGB(160,0,0)
|
1452
|
+
---
|
1453
|
+
> RGB(200,0,200)
|
1454
|
+
39c37
|
1455
|
+
< HBRUSH brush1, brush2, brush3, brush4, brush5;
|
1456
|
+
---
|
1457
|
+
> HBRUSH brush1, brush2, brush3;
|
1458
|
+
113,114d110
|
1459
|
+
< brush4 = CreateSolidBrush(colors[7]);
|
1460
|
+
< brush5 = CreateSolidBrush(colors[8]);
|
1461
|
+
155,157c151
|
1462
|
+
< else if(v==3) return brush3;
|
1463
|
+
< else if(v==4) return brush4;
|
1464
|
+
< else return brush5;
|
1465
|
+
---
|
1466
|
+
> else return brush3;
|
1467
|
+
325d318
|
1468
|
+
< int colornum = 5;
|
1469
|
+
327c320
|
1470
|
+
< svm_node *x_space = new svm_node[colornum * prob.l];
|
1471
|
+
---
|
1472
|
+
> svm_node *x_space = new svm_node[3 * prob.l];
|
1473
|
+
333,338c326,331
|
1474
|
+
< x_space[colornum * i].index = 1;
|
1475
|
+
< x_space[colornum * i].value = q->x;
|
1476
|
+
< x_space[colornum * i + 1].index = 2;
|
1477
|
+
< x_space[colornum * i + 1].value = q->y;
|
1478
|
+
< x_space[colornum * i + 2].index = -1;
|
1479
|
+
< prob.x[i] = &x_space[colornum * i];
|
1480
|
+
---
|
1481
|
+
> x_space[3 * i].index = 1;
|
1482
|
+
> x_space[3 * i].value = q->x;
|
1483
|
+
> x_space[3 * i + 1].index = 2;
|
1484
|
+
> x_space[3 * i + 1].value = q->y;
|
1485
|
+
> x_space[3 * i + 2].index = -1;
|
1486
|
+
> prob.x[i] = &x_space[3 * i];
|
1487
|
+
397c390
|
1488
|
+
< if(current_value > 5) current_value = 1;
|
1489
|
+
---
|
1490
|
+
> if(current_value > 3) current_value = 1;
|
1491
|
+
</pre>
|
1492
|
+
<p align="right">
|
1493
|
+
<a href="#_TOP">[Go Top]</a>
|
1494
|
+
<hr/>
|
1495
|
+
<a name="/Q7:_Java_version_of_libsvm"></a>
|
1496
|
+
<a name="f601"><b>Q: What is the difference between Java version and C++ version of libsvm?</b></a>
|
1497
|
+
<br/>
|
1498
|
+
<p>
|
1499
|
+
They are the same thing. We just rewrote the C++ code
|
1500
|
+
in Java.
|
1501
|
+
<p align="right">
|
1502
|
+
<a href="#_TOP">[Go Top]</a>
|
1503
|
+
<hr/>
|
1504
|
+
<a name="/Q7:_Java_version_of_libsvm"></a>
|
1505
|
+
<a name="f602"><b>Q: Is the Java version significantly slower than the C++ version?</b></a>
|
1506
|
+
<br/>
|
1507
|
+
<p>
|
1508
|
+
This depends on the VM you used. We have seen good
|
1509
|
+
VM which leads the Java version to be quite competitive with
|
1510
|
+
the C++ code. (though still slower)
|
1511
|
+
<p align="right">
|
1512
|
+
<a href="#_TOP">[Go Top]</a>
|
1513
|
+
<hr/>
|
1514
|
+
<a name="/Q7:_Java_version_of_libsvm"></a>
|
1515
|
+
<a name="f603"><b>Q: While training I get the following error message: java.lang.OutOfMemoryError. What is wrong?</b></a>
|
1516
|
+
<br/>
|
1517
|
+
<p>
|
1518
|
+
You should try to increase the maximum Java heap size.
|
1519
|
+
For example,
|
1520
|
+
<pre>
|
1521
|
+
java -Xmx2048m -classpath libsvm.jar svm_train ...
|
1522
|
+
</pre>
|
1523
|
+
sets the maximum heap size to 2048M.
|
1524
|
+
<p align="right">
|
1525
|
+
<a href="#_TOP">[Go Top]</a>
|
1526
|
+
<hr/>
|
1527
|
+
<a name="/Q7:_Java_version_of_libsvm"></a>
|
1528
|
+
<a name="f604"><b>Q: Why you have the main source file svm.m4 and then transform it to svm.java?</b></a>
|
1529
|
+
<br/>
|
1530
|
+
<p>
|
1531
|
+
Unlike C, Java does not have a preprocessor built-in.
|
1532
|
+
However, we need some macros (see first 3 lines of svm.m4).
|
1533
|
+
|
1534
|
+
</ul>
|
1535
|
+
<p align="right">
|
1536
|
+
<a href="#_TOP">[Go Top]</a>
|
1537
|
+
<hr/>
|
1538
|
+
<a name="/Q8:_Python_interface"></a>
|
1539
|
+
<a name="f704"><b>Q: Except the python-C++ interface provided, could I use Jython to call libsvm ?</b></a>
|
1540
|
+
<br/>
|
1541
|
+
<p> Yes, here are some examples:
|
1542
|
+
|
1543
|
+
<pre>
|
1544
|
+
$ export CLASSPATH=$CLASSPATH:~/libsvm-2.91/java/libsvm.jar
|
1545
|
+
$ ./jython
|
1546
|
+
Jython 2.1a3 on java1.3.0 (JIT: jitc)
|
1547
|
+
Type "copyright", "credits" or "license" for more information.
|
1548
|
+
>>> from libsvm import *
|
1549
|
+
>>> dir()
|
1550
|
+
['__doc__', '__name__', 'svm', 'svm_model', 'svm_node', 'svm_parameter',
|
1551
|
+
'svm_problem']
|
1552
|
+
>>> x1 = [svm_node(index=1,value=1)]
|
1553
|
+
>>> x2 = [svm_node(index=1,value=-1)]
|
1554
|
+
>>> param = svm_parameter(svm_type=0,kernel_type=2,gamma=1,cache_size=40,eps=0.001,C=1,nr_weight=0,shrinking=1)
|
1555
|
+
>>> prob = svm_problem(l=2,y=[1,-1],x=[x1,x2])
|
1556
|
+
>>> model = svm.svm_train(prob,param)
|
1557
|
+
*
|
1558
|
+
optimization finished, #iter = 1
|
1559
|
+
nu = 1.0
|
1560
|
+
obj = -1.018315639346838, rho = 0.0
|
1561
|
+
nSV = 2, nBSV = 2
|
1562
|
+
Total nSV = 2
|
1563
|
+
>>> svm.svm_predict(model,x1)
|
1564
|
+
1.0
|
1565
|
+
>>> svm.svm_predict(model,x2)
|
1566
|
+
-1.0
|
1567
|
+
>>> svm.svm_save_model("test.model",model)
|
1568
|
+
|
1569
|
+
</pre>
|
1570
|
+
|
1571
|
+
<p align="right">
|
1572
|
+
<a href="#_TOP">[Go Top]</a>
|
1573
|
+
<hr/>
|
1574
|
+
<a name="/Q9:_MATLAB_interface"></a>
|
1575
|
+
<a name="f801"><b>Q: I compile the MATLAB interface without problem, but why errors occur while running it?</b></a>
|
1576
|
+
<br/>
|
1577
|
+
<p>
|
1578
|
+
Your compiler version may not be supported/compatible for MATLAB.
|
1579
|
+
Please check <a href=http://www.mathworks.com/support/compilers/current_release>this MATLAB page</a> first and then specify the version
|
1580
|
+
number. For example, if g++ X.Y is supported, replace
|
1581
|
+
<pre>
|
1582
|
+
CXX = g++
|
1583
|
+
</pre>
|
1584
|
+
in the Makefile with
|
1585
|
+
<pre>
|
1586
|
+
CXX = g++-X.Y
|
1587
|
+
</pre>
|
1588
|
+
<p align="right">
|
1589
|
+
<a href="#_TOP">[Go Top]</a>
|
1590
|
+
<hr/>
|
1591
|
+
<a name="/Q9:_MATLAB_interface"></a>
|
1592
|
+
<a name="f8011"><b>Q: On 64bit Windows I compile the MATLAB interface without problem, but why errors occur while running it?</b></a>
|
1593
|
+
<br/>
|
1594
|
+
<p>
|
1595
|
+
|
1596
|
+
|
1597
|
+
Please make sure that you use
|
1598
|
+
the -largeArrayDims option in make.m. For example,
|
1599
|
+
<pre>
|
1600
|
+
mex -largeArrayDims -O -c svm.cpp
|
1601
|
+
</pre>
|
1602
|
+
|
1603
|
+
Moreover, if you use Microsoft Visual Studio,
|
1604
|
+
probabally it is not properly installed.
|
1605
|
+
See the explanation
|
1606
|
+
<a href=http://www.mathworks.com/support/compilers/current_release/win64.html#n7>here</a>.
|
1607
|
+
<p align="right">
|
1608
|
+
<a href="#_TOP">[Go Top]</a>
|
1609
|
+
<hr/>
|
1610
|
+
<a name="/Q9:_MATLAB_interface"></a>
|
1611
|
+
<a name="f802"><b>Q: Does the MATLAB interface provide a function to do scaling?</b></a>
|
1612
|
+
<br/>
|
1613
|
+
<p>
|
1614
|
+
It is extremely easy to do scaling under MATLAB.
|
1615
|
+
The following one-line code scale each feature to the range
|
1616
|
+
of [0,1]:
|
1617
|
+
<pre>
|
1618
|
+
(data - repmat(min(data,[],1),size(data,1),1))*spdiags(1./(max(data,[],1)-min(data,[],1))',0,size(data,2),size(data,2))
|
1619
|
+
</pre>
|
1620
|
+
<p align="right">
|
1621
|
+
<a href="#_TOP">[Go Top]</a>
|
1622
|
+
<hr/>
|
1623
|
+
<a name="/Q9:_MATLAB_interface"></a>
|
1624
|
+
<a name="f803"><b>Q: How could I use MATLAB interface for parameter selection?</b></a>
|
1625
|
+
<br/>
|
1626
|
+
<p>
|
1627
|
+
One can do this by a simple loop.
|
1628
|
+
See the following example:
|
1629
|
+
<pre>
|
1630
|
+
bestcv = 0;
|
1631
|
+
for log2c = -1:3,
|
1632
|
+
for log2g = -4:1,
|
1633
|
+
cmd = ['-v 5 -c ', num2str(2^log2c), ' -g ', num2str(2^log2g)];
|
1634
|
+
cv = svmtrain(heart_scale_label, heart_scale_inst, cmd);
|
1635
|
+
if (cv >= bestcv),
|
1636
|
+
bestcv = cv; bestc = 2^log2c; bestg = 2^log2g;
|
1637
|
+
end
|
1638
|
+
fprintf('%g %g %g (best c=%g, g=%g, rate=%g)\n', log2c, log2g, cv, bestc, bestg, bestcv);
|
1639
|
+
end
|
1640
|
+
end
|
1641
|
+
</pre>
|
1642
|
+
You may adjust the parameter range in the above loops.
|
1643
|
+
<p align="right">
|
1644
|
+
<a href="#_TOP">[Go Top]</a>
|
1645
|
+
<hr/>
|
1646
|
+
<a name="/Q9:_MATLAB_interface"></a>
|
1647
|
+
<a name="f8031"><b>Q: I use MATLAB parallel programming toolbox on a multi-core environment for parameter selection. Why the program is even slower?</b></a>
|
1648
|
+
<br/>
|
1649
|
+
<p>
|
1650
|
+
Fabrizio Lacalandra of University of Pisa reported this issue.
|
1651
|
+
It seems the problem is caused by the screen output.
|
1652
|
+
If you disable the <b>info</b> function
|
1653
|
+
using <pre>#if 0,</pre> then the problem
|
1654
|
+
may be solved.
|
1655
|
+
<p align="right">
|
1656
|
+
<a href="#_TOP">[Go Top]</a>
|
1657
|
+
<hr/>
|
1658
|
+
<a name="/Q9:_MATLAB_interface"></a>
|
1659
|
+
<a name="f8032"><b>Q: How do I use LIBSVM with OpenMP under MATLAB?</b></a>
|
1660
|
+
<br/>
|
1661
|
+
<p>
|
1662
|
+
In Makefile,
|
1663
|
+
you need to add -fopenmp to CFLAGS and -lgomp to MEX_OPTION. For Octave, you need the same modification.
|
1664
|
+
|
1665
|
+
<p> However, a minor problem is that
|
1666
|
+
the number of threads cannot
|
1667
|
+
be specified in MATLAB. We tried Version 7.12 (R2011a) and gcc-4.6.1.
|
1668
|
+
|
1669
|
+
<pre>
|
1670
|
+
% export OMP_NUM_THREADS=4; matlab
|
1671
|
+
>> setenv('OMP_NUM_THREADS', '1');
|
1672
|
+
</pre>
|
1673
|
+
|
1674
|
+
Then OMP_NUM_THREADS is still 4 while running the program. Please contact us if you
|
1675
|
+
see how to solve this problem. You can, however,
|
1676
|
+
specify the number in the source code (thanks
|
1677
|
+
to comments from Ricardo Santiago-mozos):
|
1678
|
+
<pre>
|
1679
|
+
#pragma omp parallel for private(i) num_threads(4)
|
1680
|
+
</pre>
|
1681
|
+
<p align="right">
|
1682
|
+
<a href="#_TOP">[Go Top]</a>
|
1683
|
+
<hr/>
|
1684
|
+
<a name="/Q9:_MATLAB_interface"></a>
|
1685
|
+
<a name="f804"><b>Q: How could I generate the primal variable w of linear SVM?</b></a>
|
1686
|
+
<br/>
|
1687
|
+
<p>
|
1688
|
+
Let's start from the binary class and
|
1689
|
+
assume you have two labels -1 and +1.
|
1690
|
+
After obtaining the model from calling svmtrain,
|
1691
|
+
do the following to have w and b:
|
1692
|
+
<pre>
|
1693
|
+
w = model.SVs' * model.sv_coef;
|
1694
|
+
b = -model.rho;
|
1695
|
+
|
1696
|
+
if model.Label(1) == -1
|
1697
|
+
w = -w;
|
1698
|
+
b = -b;
|
1699
|
+
end
|
1700
|
+
</pre>
|
1701
|
+
If you do regression or one-class SVM, then the if statement is not needed.
|
1702
|
+
|
1703
|
+
<p> For multi-class SVM, we illustrate the setting
|
1704
|
+
in the following example of running the iris
|
1705
|
+
data, which have 3 classes
|
1706
|
+
<pre>
|
1707
|
+
> [y, x] = libsvmread('../../htdocs/libsvmtools/datasets/multiclass/iris.scale');
|
1708
|
+
> m = svmtrain(y, x, '-t 0')
|
1709
|
+
|
1710
|
+
m =
|
1711
|
+
|
1712
|
+
Parameters: [5x1 double]
|
1713
|
+
nr_class: 3
|
1714
|
+
totalSV: 42
|
1715
|
+
rho: [3x1 double]
|
1716
|
+
Label: [3x1 double]
|
1717
|
+
ProbA: []
|
1718
|
+
ProbB: []
|
1719
|
+
nSV: [3x1 double]
|
1720
|
+
sv_coef: [42x2 double]
|
1721
|
+
SVs: [42x4 double]
|
1722
|
+
</pre>
|
1723
|
+
sv_coef is like:
|
1724
|
+
<pre>
|
1725
|
+
+-+-+--------------------+
|
1726
|
+
|1|1| |
|
1727
|
+
|v|v| SVs from class 1 |
|
1728
|
+
|2|3| |
|
1729
|
+
+-+-+--------------------+
|
1730
|
+
|1|2| |
|
1731
|
+
|v|v| SVs from class 2 |
|
1732
|
+
|2|3| |
|
1733
|
+
+-+-+--------------------+
|
1734
|
+
|1|2| |
|
1735
|
+
|v|v| SVs from class 3 |
|
1736
|
+
|3|3| |
|
1737
|
+
+-+-+--------------------+
|
1738
|
+
</pre>
|
1739
|
+
so we need to see nSV of each classes.
|
1740
|
+
<pre>
|
1741
|
+
> m.nSV
|
1742
|
+
|
1743
|
+
ans =
|
1744
|
+
|
1745
|
+
3
|
1746
|
+
21
|
1747
|
+
18
|
1748
|
+
</pre>
|
1749
|
+
Suppose the goal is to find the vector w of classes
|
1750
|
+
1 vs 3. Then
|
1751
|
+
y_i alpha_i of training 1 vs 3 are
|
1752
|
+
<pre>
|
1753
|
+
> coef = [m.sv_coef(1:3,2); m.sv_coef(25:42,1)];
|
1754
|
+
</pre>
|
1755
|
+
and SVs are:
|
1756
|
+
<pre>
|
1757
|
+
> SVs = [m.SVs(1:3,:); m.SVs(25:42,:)];
|
1758
|
+
</pre>
|
1759
|
+
Hence, w is
|
1760
|
+
<pre>
|
1761
|
+
> w = SVs'*coef;
|
1762
|
+
</pre>
|
1763
|
+
For rho,
|
1764
|
+
<pre>
|
1765
|
+
> m.rho
|
1766
|
+
|
1767
|
+
ans =
|
1768
|
+
|
1769
|
+
1.1465
|
1770
|
+
0.3682
|
1771
|
+
-1.9969
|
1772
|
+
> b = -m.rho(2);
|
1773
|
+
</pre>
|
1774
|
+
because rho is arranged by 1vs2 1vs3 2vs3.
|
1775
|
+
|
1776
|
+
|
1777
|
+
|
1778
|
+
<p align="right">
|
1779
|
+
<a href="#_TOP">[Go Top]</a>
|
1780
|
+
<hr/>
|
1781
|
+
<a name="/Q9:_MATLAB_interface"></a>
|
1782
|
+
<a name="f805"><b>Q: Is there an OCTAVE interface for libsvm?</b></a>
|
1783
|
+
<br/>
|
1784
|
+
<p>
|
1785
|
+
Yes, after libsvm 2.86, the matlab interface
|
1786
|
+
works on OCTAVE as well. Please type
|
1787
|
+
<pre>
|
1788
|
+
make octave
|
1789
|
+
</pre>
|
1790
|
+
for installation.
|
1791
|
+
<p align="right">
|
1792
|
+
<a href="#_TOP">[Go Top]</a>
|
1793
|
+
<hr/>
|
1794
|
+
<a name="/Q9:_MATLAB_interface"></a>
|
1795
|
+
<a name="f806"><b>Q: How to handle the name conflict between svmtrain in the libsvm matlab interface and that in MATLAB bioinformatics toolbox?</b></a>
|
1796
|
+
<br/>
|
1797
|
+
<p>
|
1798
|
+
The easiest way is to rename the svmtrain binary
|
1799
|
+
file (e.g., svmtrain.mexw32 on 32-bit windows)
|
1800
|
+
to a different
|
1801
|
+
name (e.g., svmtrain2.mexw32).
|
1802
|
+
<p align="right">
|
1803
|
+
<a href="#_TOP">[Go Top]</a>
|
1804
|
+
<hr/>
|
1805
|
+
<a name="/Q9:_MATLAB_interface"></a>
|
1806
|
+
<a name="f807"><b>Q: On Windows I got an error message "Invalid MEX-file: Specific module not found" when running the pre-built MATLAB interface in the windows sub-directory. What should I do?</b></a>
|
1807
|
+
<br/>
|
1808
|
+
<p>
|
1809
|
+
|
1810
|
+
The error usually happens
|
1811
|
+
when there are missing runtime components
|
1812
|
+
such as MSVCR100.dll on your Windows platform.
|
1813
|
+
You can use tools such as
|
1814
|
+
<a href=http://www.dependencywalker.com/>Dependency
|
1815
|
+
Walker</a> to find missing library files.
|
1816
|
+
|
1817
|
+
<p>
|
1818
|
+
Because the pre-built MEX files are compiled by
|
1819
|
+
Visual C++ 2010,
|
1820
|
+
please make sure that you have installed
|
1821
|
+
Microsoft Visual C++ Redistributable Package 2010
|
1822
|
+
(vcredist_x86.exe). You can easily find the freely
|
1823
|
+
available file from Microsoft's web site.
|
1824
|
+
|
1825
|
+
<p>
|
1826
|
+
For 64bit Windows, the pre-built files are by
|
1827
|
+
Visual C++ 2008, so please install
|
1828
|
+
Microsoft Visual C++ Redistributable Package 2008
|
1829
|
+
(vcredist_x64.exe).
|
1830
|
+
<p align="right">
|
1831
|
+
<a href="#_TOP">[Go Top]</a>
|
1832
|
+
<hr/>
|
1833
|
+
<p align="middle">
|
1834
|
+
<a href="http://www.csie.ntu.edu.tw/~cjlin/libsvm">LIBSVM home page</a>
|
1835
|
+
</p>
|
1836
|
+
</body>
|
1837
|
+
</html>
|