-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 584886a
Showing
12 changed files
with
1,475 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
% Matlab Wrapper for SVMlight | ||
% Version 0.92, August 2002 | ||
% | ||
% Wrapper functions: | ||
% svml - Wrapper for SVMlight | ||
% svmltrain - Wrapper for SVMlight: Training | ||
% svmlfwd - Wrapper for SVMlight: Prediction | ||
% demsvml1 - Demo program for Matlab SVMlight wrapper | ||
% | ||
% Auxiliary functions to generate and read data files | ||
% svmlread - Read a data file generated by SVM light | ||
% svmlwrite - Write matrix into data file for SVM light | ||
% svmlopt - Generate/alter options structure for SVM light | ||
% consist - Check that arguments are consistent. | ||
% | ||
% Calling routines | ||
% svm_classify - Interface to SVM light, classification module | ||
% svm_learn - Interface to SVM light, learning module | ||
% | ||
% | ||
% Copyright (c) by Anton Schwaighofer (2002) | ||
% mailto:anton.schwaighofer@gmx.net | ||
% | ||
% This program is released unter the GNU General Public License. | ||
% | ||
|
||
% $Revision: 1.3 $ $Date: 2002/08/09 20:25:26 $ | ||
% | ||
% This program is free software; you can redistribute it and/or | ||
% modify it under the terms of the GNU General Public License | ||
% as published by the Free Software Foundation; either version 2 | ||
% of the License, or (at your option) any later version. | ||
% | ||
% This program is distributed in the hope that it will be useful, | ||
% but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
% GNU General Public License for more details. | ||
% | ||
% You should have received a copy of the GNU General Public License | ||
% along with this program; if not, write to the | ||
% Free Software Foundation, Inc., | ||
% 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
% | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
function errstring = consist(model, type, inputs, outputs) | ||
%CONSIST Check that arguments are consistent. | ||
% | ||
% Description | ||
% | ||
% ERRSTRING = CONSIST(NET, TYPE, INPUTS) takes a network data structure | ||
% NET together with a string TYPE containing the correct network type, | ||
% a matrix INPUTS of input vectors and checks that the data structure | ||
% is consistent with the other arguments. An empty string is returned | ||
% if there is no error, otherwise the string contains the relevant | ||
% error message. If the TYPE string is empty, then any type of network | ||
% is allowed. | ||
% | ||
% ERRSTRING = CONSIST(NET, TYPE) takes a network data structure NET | ||
% together with a string TYPE containing the correct network type, and | ||
% checks that the two types match. | ||
% | ||
% ERRSTRING = CONSIST(NET, TYPE, INPUTS, OUTPUTS) also checks that the | ||
% network has the correct number of outputs, and that the number of | ||
% patterns in the INPUTS and OUTPUTS is the same. The fields in NET | ||
% that are used are | ||
% type | ||
% nin | ||
% nout | ||
% | ||
% See also | ||
% MLPFWD | ||
% | ||
|
||
% Copyright (c) Ian T Nabney (1996-2001) | ||
|
||
% Assume that all is OK as default | ||
errstring = ''; | ||
|
||
% If type string is not empty | ||
if ~isempty(type) | ||
% First check that model has type field | ||
if ~isfield(model, 'type') | ||
errstring = 'Data structure does not contain type field'; | ||
return | ||
end | ||
% Check that model has the correct type | ||
s = model.type; | ||
if ~strcmp(s, type) | ||
errstring = ['Model type ''', s, ''' does not match expected type ''',... | ||
type, '''']; | ||
return | ||
end | ||
end | ||
|
||
% If inputs are present, check that they have correct dimension | ||
if nargin > 2 | ||
if ~isfield(model, 'nin') | ||
errstring = 'Data structure does not contain nin field'; | ||
return | ||
end | ||
|
||
data_nin = size(inputs, 2); | ||
if model.nin ~= data_nin | ||
errstring = ['Dimension of inputs ', num2str(data_nin), ... | ||
' does not match number of model inputs ', num2str(model.nin)]; | ||
return | ||
end | ||
end | ||
|
||
% If outputs are present, check that they have correct dimension | ||
if nargin > 3 | ||
if ~isfield(model, 'nout') | ||
errstring = 'Data structure does not conatin nout field'; | ||
return | ||
end | ||
data_nout = size(outputs, 2); | ||
if model.nout ~= data_nout | ||
errstring = ['Dimension of outputs ', num2str(data_nout), ... | ||
' does not match number of model outputs ', num2str(model.nout)]; | ||
return | ||
end | ||
|
||
% Also check that number of data points in inputs and outputs is the same | ||
num_in = size(inputs, 1); | ||
num_out = size(outputs, 1); | ||
if num_in ~= num_out | ||
errstring = ['Number of input patterns ', num2str(num_in), ... | ||
' does not match number of output patterns ', num2str(num_out)]; | ||
return | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
function demsvml1() | ||
% DEMSVML1 - Demo program for Matlab SVMlight wrapper | ||
% | ||
% This program shows how Thorsten Joachims SVMlight test examples can | ||
% be executed using the Matlab SVMlight wrapper, plus some additional | ||
% features. | ||
% In order to execute this demo, SVMlight must be installed (on the | ||
% execution path or in the current directory) and the two | ||
% subdirectories 'example1' and 'example2' must be present in the | ||
% current directory. | ||
% | ||
% Please check the source code for more info. | ||
% | ||
% See also SVML, SVMLTRAIN, SVMLFWD, SVMLWRITE, SVMLREAD, SVM_LEARN, | ||
% SVM_CLASSIFY | ||
% | ||
|
||
% | ||
% Copyright (c) by Anton Schwaighofer (2002) | ||
% $Revision: 1.3 $ $Date: 2002/08/09 20:27:33 $ | ||
% mailto:anton.schwaighofer@gmx.net | ||
% | ||
% This program is released unter the GNU General Public License. | ||
% | ||
|
||
fprintf('In order to execute this demo, SVMlight must be installed (on the\n'); | ||
fprintf('execution path or in the current directory) and the two\n'); | ||
fprintf('subdirectories ''example1'' and ''example2'' must be present in the\n'); | ||
fprintf('current directory.\n'); | ||
fprintf('Have a look at the source code for details...\n'); | ||
fprintf('\nPress the Return key to continue, or Control-C to cancel.\n'); | ||
|
||
% We do basically the same as Joachims example2, just that we load | ||
% the data into Matlab beforehand and use the SVML structure | ||
[Y, X] = svmlread('example1/train.dat'); | ||
net = svml('example1/model1', 'Kernel', 0, 'C', 1); | ||
net = svmltrain(net, X, Y); | ||
% Also read out Joachims' test data | ||
[Ytest, Xtest] = svmlread('example1/test.dat'); | ||
% Compute prediction on the test data | ||
Ypred = svmlfwd(net, Xtest); | ||
% We can also get SVMlight to print out accuracy and precision/recall, by | ||
% providing the test targets as well: | ||
Ypred = svmlfwd(net, Xtest, Ytest); | ||
|
||
|
||
% The SVMlight wrapper can also directly access data files in SVMlight's | ||
% format, both during training and testing. This is of course more | ||
% efficient than reading the files into Matlab and writing it back to | ||
% disk again, if the data are already available in SVMlights format. | ||
net = svml('example1/model2', 'Kernel', 0, 'C', 1); | ||
% directly access training data | ||
net = svmltrain(net, 'example1/train.dat'); | ||
% Compute prediction on the test data | ||
Ytest = svmlfwd(net, 'example1/test.dat'); | ||
|
||
|
||
% Now for the transductive SVM: | ||
net = svml('example2/model1', 'Kernel', 0, 'C', 1, 'TransLabelFile', ... | ||
'example2/trans_predictions'); | ||
% Transductive learner is automatically invoked | ||
% You may just as well load the data into Matlab variables | ||
% [Y, X] = svmlread('example2/train_transduction.dat'); | ||
% and go net = svmltrain(net, X, Y); | ||
net = svmltrain(net, 'example2/train_transduction.dat'); | ||
% Read out the labels for the unclassified training examples | ||
transLabels = svmlread('example2/trans_predictions'); | ||
|
||
|
||
% We now use only the simple calling routines SVM_LEARN and | ||
% SVM_CLASSIFY. For the options, we only assume that SVMlight is | ||
% installed in the current directory '.' | ||
|
||
% Joachims' first example (inductive SVM): | ||
options = svmlopt('ExecPath', '.'); | ||
svm_learn(options, 'example1/train.dat', 'example1/model'); | ||
svm_classify(options, 'example1/test.dat', 'example1/model', ... | ||
'example1/predictions'); | ||
% The file example1/model holds the trained SVM model, | ||
% example1/predictions are the labels for the test points | ||
|
||
% Joachims' second example (transductive SVM) | ||
svm_learn(options, 'example2/train_transduction.dat', 'example2/model'); | ||
svm_classify(options, 'example2/test.dat', 'example2/model', ... | ||
'example2/predictions'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
function status = svm_classify(options, data, model, predictions) | ||
% SVM_CLASSIFY - Interface to SVM light, classification module | ||
% | ||
% STATUS = SVM_CLASSIFY(OPTIONS, DATA, MODEL, PREDICTIONS) | ||
% Call the classification program 'svm_classify' of the SVM light | ||
% package. | ||
% OPTIONS must be a structure generated by SVMLOPT. For SVM_CLASSIFY, | ||
% only the options 'ExecPath' and 'Verbosity' are relevant. | ||
% DATA is the name of the file containing the data to be classified | ||
% (the test data). Use SVMLWRITE to convert a Matlab matrix to the | ||
% appropriate format. | ||
% MODEL is the name of the file holding the trained Support Vector | ||
% Machine, generated by SVM_LEARN. | ||
% PREDICTIONS is the file that will store the predicted classes. There | ||
% is one line per test example in output_file containing the value of | ||
% the decision function on that example. The sign of this value | ||
% determines the predicted class. This file can be read into Matlab | ||
% using SVMLREAD. | ||
% If 'svm_learn' is not on the path, OPTIONS must contain a field | ||
% 'ExecPath' with the path of the executable. | ||
% STATUS is the error code returned by SVM light (0 if everything went | ||
% fine) | ||
% | ||
% See also SVML, SVMLOPT, SVMLWRITE, SVMLREAD, SVM_LEARN | ||
% | ||
|
||
% | ||
% Copyright (c) by Anton Schwaighofer (2001) | ||
% $Revision: 1.6 $ $Date: 2002/08/09 20:24:12 $ | ||
% mailto:anton.schwaighofer@gmx.net | ||
% | ||
% This program is released unter the GNU General Public License. | ||
% | ||
|
||
error(nargchk(4, 4, nargin)); | ||
|
||
Names = fieldnames(options); | ||
[m,n] = size(Names); | ||
|
||
s = ''; | ||
for i = 1:m, | ||
field = Names{i,:}; | ||
value = getfield(options, field); | ||
switch field, | ||
case 'Verbosity' | ||
s = stroption(s, '-v %i', value); | ||
end | ||
end | ||
|
||
evalstr = [fullfile(options.ExecPath, 'svm_classify') s ' ' ... | ||
data ' ' model ' ' predictions]; | ||
fprintf('\nCalling SVMlight:\n%s\n\n', evalstr); | ||
if isunix, | ||
status = unix(evalstr); | ||
else | ||
status = dos(evalstr); | ||
end | ||
|
||
|
||
function s = stroption(s, formatstr, value, varargin) | ||
% STROPTION - Add a new option to string | ||
% | ||
|
||
if ~isempty(value), | ||
s = [s ' ' sprintf(formatstr, value, varargin{:})]; | ||
end |
Oops, something went wrong.