Skip to content

Commit

Permalink
Merge pull request #223 from sploving/master
Browse files Browse the repository at this point in the history
add lua docs and further examples
  • Loading branch information
Soeren Sonnenburg committed Jul 22, 2011
2 parents c86e131 + fe2408f commit 6d0ae13
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 16 deletions.
6 changes: 6 additions & 0 deletions doc/pages/Installation.mainpage
Expand Up @@ -14,6 +14,7 @@ On debian, depending on the interface you want, install the package(s)
\verbatim
libshogun-dev - for C++ developers building extensions using
libshogun
shogun-lua-modular - for the modular lua interface
shogun-java-modular - for the modular java interface
shogun-octave - for the static octave interface
shogun-octave-modular - for the modular octave interface
Expand Down Expand Up @@ -69,6 +70,9 @@ CPLEX(tm) just make sure cplex can be found in the PATH. If it is not found
shogun will resort to GLPK (if version at least 4.29 is found) for 1-norm MKL,
p-norm MKL with p>1 will work nonetheless.

\li To compile the Lua interface you need to have the Lua developer files
(debian package lua) installed.

\li To compile the Java interface you need to have the Java developer files
(debian package openjdk-6-jdk) installed.

Expand Down Expand Up @@ -109,6 +113,8 @@ where \b interface is one of the following

\li java_modular -- for java (see http://www.java.com).

\li lua_modular -- for lua (see http://www.lua.org).

\li matlab -- for matlab (see http://www.mathworks.com)

(you don't need to compile libshogunui in case you compile a modular interface)
Expand Down
65 changes: 64 additions & 1 deletion doc/pages/ModularTutorial.mainpage
Expand Up @@ -156,8 +156,67 @@ DoubleMatrix trainlab = concatHorizontally(o.neg(), o);
DoubleMatrix testlab = concatHorizontarily(o.neg(), o)
\endverbatim

In lua, this is is done as follows:
\verbatim
require 'shogun'
require 'load'

function concatenate(...)
local result = ...
for _,t in ipairs{select(2, ...)} do
for row,rowdata in ipairs(t) do
for col,coldata in ipairs(rowdata) do
table.insert(result[row], coldata)
end
end
end
return result
end

function rand_matrix(rows, cols, dist)
local matrix = {}
for i = 1, rows do
matrix[i] = {}
for j = 1, cols do
matrix[i][j] = math.random() + dist
end
end
return matrix
end

function ones(num)
r={}
for i=1,num do
r[i]=1
end
return r
end


num=1000
dist=1
width=2.1
C=1

traindata_real=concatenate(rand_matrix(2,num, -dist),rand_matrix(2,num,dist))
testdata_real=concatenate(rand_matrix(2,num,-dist), rand_matrix(2,num, dist))
trainlab={}
for i = 1, num do
trainlab[i] = -1
trainlab[i + num] = 1
end

testlab={}
for i = 1, num do
testlab[i] = -1
testlab[i + num] = 1
end

\endverbatim

The rest of this tutorial below will now work the same (identical syntax) for python,
octave (when using a trailing semicolon for each command, which is optional in python).
octave (when using a trailing semicolon for each command, which is optional in python),
lua(we use colon to get the method of an object).

For java, we use DoubleMatrix to represent most of the types. If shogun C++ class need a
int Vector/Matrix, we convert the DoubleMatrix into int Vector/Matrix and when returned
Expand Down Expand Up @@ -249,6 +308,10 @@ For java:

\verbinclude classifier_libsvm_minimal_modular.java

For lua:

\verbinclude classifier_libsvm_minimal_modular.lua

For python:
\verbinclude classifier_libsvm_minimal_modular.py
*/
Expand Up @@ -22,8 +22,11 @@ function classifier_averaged_perceptron_modular (fm_train_real,fm_test_real,labe

perceptron:set_features(feats_test)
out_labels = perceptron:apply():get_labels()

return perceptron, out_labels
end

print 'AveragedPerceptron'
classifier_averaged_perceptron_modular(unpack(parameter_list[1]))
if debug.getinfo(3) == nill then
print 'AveragedPerceptron'
classifier_averaged_perceptron_modular(unpack(parameter_list[1]))
end
@@ -0,0 +1,78 @@
require 'shogun'
require 'load'

function concatenate(...)
local result = ...
for _,t in ipairs{select(2, ...)} do
for row,rowdata in ipairs(t) do
for col,coldata in ipairs(rowdata) do
table.insert(result[row], coldata)
end
end
end
return result
end

function rand_matrix(rows, cols, dist)
local matrix = {}
for i = 1, rows do
matrix[i] = {}
for j = 1, cols do
matrix[i][j] = math.random() + dist
end
end
return matrix
end

function ones(num)
r={}
for i=1,num do
r[i]=1
end
return r
end


num=1000
dist=1
width=2.1
C=1

traindata_real=concatenate(rand_matrix(2,num, -dist),rand_matrix(2,num,dist))
testdata_real=concatenate(rand_matrix(2,num,-dist), rand_matrix(2,num, dist))

trainlab={}
for i = 1, num do
trainlab[i] = -1
trainlab[i + num] = 1
end

testlab={}
for i = 1, num do
testlab[i] = -1
testlab[i + num] = 1
end

feats_train=RealFeatures(traindata_real)
feats_test=RealFeatures(testdata_real)
kernel=GaussianKernel(feats_train, feats_train, width)

labels=Labels(trainlab)
svm=LibSVM(C, kernel, labels)
svm:train()

kernel:init(feats_train, feats_test)
out=svm:apply():get_labels()

err_num = 0
for i = 1, num do
if out[i] > 0 then
err_num = err_num+1
end
if out[i+num] < 0 then
err_num = err_num+1
end
end

testerr=err_num/(2*num)
print(testerr)
Expand Up @@ -20,5 +20,7 @@ function distance_braycurtis_modular (fm_train_real,fm_test_real)
return distance,dm_train,dm_test
end

print 'BrayCurtisDistance'
distance_braycurtis_modular(unpack(parameter_list[1]))
if debug.getinfo(3) == nill then
print 'BrayCurtisDistance'
distance_braycurtis_modular(unpack(parameter_list[1]))
end
Expand Up @@ -48,6 +48,8 @@ function evaluation_contingencytableevaluation_modular(ground_truth, predicted)
return accuracy, errorrate, bal, wracc, f1, crosscorrelation, recall, precision, specificity
end

print 'ContingencyTableEvaluation'
evaluation_contingencytableevaluation_modular(unpack(parameter_list[1]))
if debug.getinfo(3) == nill then
print 'ContingencyTableEvaluation'
evaluation_contingencytableevaluation_modular(unpack(parameter_list[1]))
end

Expand Up @@ -13,5 +13,7 @@ function features_simple_real_modular(A)
return a_out
end

print 'simple_real'
features_simple_real_modular(unpack(parameter_list[1]))
if debug.getinfo(3) == nill then
print 'simple_real'
features_simple_real_modular(unpack(parameter_list[1]))
end
@@ -0,0 +1,39 @@
require 'shogun'
require 'load'

traindat = load_dna('../data/fm_train_dna.dat')
testdat = load_dna('../data/fm_test_dna.dat')
parameter_list = {{traindat,testdat,3,0,false},{traindat,testdat,4,0,false}}

function kernel_comm_ulong_string_modular (fm_train_dna,fm_test_dna, order, gap, reverse)
charfeat=StringCharFeatures(DNA)
charfeat:set_features(fm_train_dna)
feats_train=StringUlongFeatures(charfeat:get_alphabet())
feats_train:obtain_from_char(charfeat, order-1, order, gap, reverse)
preproc=SortUlongString()
preproc:init(feats_train)
feats_train:add_preprocessor(preproc)
feats_train:apply_preprocessor()


charfeat=StringCharFeatures(DNA)
charfeat:set_features(fm_test_dna)
feats_test=StringUlongFeatures(charfeat:get_alphabet())
feats_test:obtain_from_char(charfeat, order-1, order, gap, reverse)
feats_test:add_preprocessor(preproc)
feats_test:apply_preprocessor()

use_sign=false

kernel=CommUlongStringKernel(feats_train, feats_train, use_sign)

km_train=kernel:get_kernel_matrix()
kernel:init(feats_train, feats_test)
km_test=kernel:get_kernel_matrix()
return km_train,km_test,kernel
end

if debug.getinfo(3) == nill then
print 'CommUlongString'
kernel_comm_ulong_string_modular(unpack(parameter_list[1]))
end
@@ -0,0 +1,37 @@
require 'shogun'
require 'load'

traindat = load_dna('../data/fm_train_dna.dat')
testdat = load_dna('../data/fm_test_dna.dat')
parameter_list = {{traindat,testdat,4,0,false, false},{traindat,testdat,4,0,False,False}}

function kernel_comm_word_string_modular (fm_train_dna,fm_test_dna, order, gap, reverse, use_sign)
charfeat=StringCharFeatures(DNA)
charfeat:set_features(fm_train_dna)
feats_train=StringWordFeatures(charfeat:get_alphabet())
feats_train:obtain_from_char(charfeat, order-1, order, gap, reverse)

preproc=SortWordString()
preproc:init(feats_train)
feats_train:add_preprocessor(preproc)
feats_train:apply_preprocessor()

charfeat=StringCharFeatures(DNA)
charfeat:set_features(fm_test_dna)
feats_test=StringWordFeatures(charfeat:get_alphabet())
feats_test:obtain_from_char(charfeat, order-1, order, gap, reverse)
feats_test:add_preprocessor(preproc)
feats_test:apply_preprocessor()

kernel=CommWordStringKernel(feats_train, feats_train, use_sign)

km_train=kernel:get_kernel_matrix()
kernel:init(feats_train, feats_test)
km_test=kernel:get_kernel_matrix()
return km_train,km_test,kernel
end

if debug.getinfo(3) == nill then
print 'CommWordString'
kernel_comm_word_string_modular(unpack(parameter_list[1]))
end
6 changes: 4 additions & 2 deletions examples/undocumented/lua_modular/kernel_gaussian_modular.lua
Expand Up @@ -20,5 +20,7 @@ function kernel_gaussian_modular (fm_train_real,fm_test_real,width)
return km_train,km_test,kernel
end

print 'Gaussian'
kernel_gaussian_modular(unpack(parameter_list[1]))
if debug.getinfo(3) == nill then
print 'Gaussian'
kernel_gaussian_modular(unpack(parameter_list[1]))
end
24 changes: 24 additions & 0 deletions examples/undocumented/lua_modular/kernel_linear_byte_modular.lua
@@ -0,0 +1,24 @@
require 'shogun'
require 'load'

traindat = load_numbers('../data/fm_train_byte.dat')
testdat = load_numbers('../data/fm_test_byte.dat')

parameter_list={{traindat,testdat},{traindat,testdat}}

function kernel_linear_byte_modular(fm_train_byte,fm_test_byte)
feats_train=ByteFeatures(fm_train_byte)
feats_test=ByteFeatures(fm_test_byte)

kernel=LinearKernel(feats_train, feats_train)
km_train=kernel:get_kernel_matrix()

kernel:init(feats_train, feats_test)
km_test=kernel:get_kernel_matrix()
return kernel
end

if debug.getinfo(3) == nill then
print 'LinearByte'
kernel_linear_byte_modular(unpack(parameter_list[1]))
end
26 changes: 26 additions & 0 deletions examples/undocumented/lua_modular/kernel_linear_word_modular.lua
@@ -0,0 +1,26 @@
require 'shogun'
require 'load'

traindat = load_numbers('../data/fm_train_word.dat')
testdat = load_numbers('../data/fm_test_word.dat')

parameter_list={{traindat,testdat,1.2},{traindat,testdat,1.2}}

function kernel_linear_word_modular (fm_train_word,fm_test_word,scale)
feats_train=WordFeatures(fm_train_word)
feats_test=WordFeatures(fm_test_word)

kernel=LinearKernel(feats_train, feats_train)
kernel:set_normalizer(AvgDiagKernelNormalizer(scale))
kernel:init(feats_train, feats_train)

km_train=kernel:get_kernel_matrix()
kernel:init(feats_train, feats_test)
km_test=kernel:get_kernel_matrix()
return kernel
end

if debug.getinfo(3) == nill then
print 'LinearWord'
kernel_linear_word_modular(unpack(parameter_list[1]))
end
Expand Up @@ -26,7 +26,6 @@ function kernel_weighted_degree_string_modular (fm_train_dna,fm_test_dna,degree)
return km_train, km_test, kernel
end


if debug.getinfo(3) == nill then
print 'WeightedDegreeString'
kernel_weighted_degree_string_modular(unpack(parameter_list[1]))
Expand Down
Expand Up @@ -15,6 +15,8 @@ function preprocessor_classicisomap_modular(data)
return features
end

print 'ClassicIsomap'
preprocessor_classicisomap_modular(unpack(parameter_list[1]))
if debug.getinfo(3) == nill then
print 'ClassicIsomap'
preprocessor_classicisomap_modular(unpack(parameter_list[1]))
end

Expand Up @@ -116,5 +116,7 @@ function serialization_complex_example(num, dist, dim, C, width)
return svm,new_svm
end

print 'Serialization SVMLight'
serialization_complex_example(unpack(parameter_list[1]))
if debug.getinfo(3) == nill then
print 'Serialization SVMLight'
serialization_complex_example(unpack(parameter_list[1]))
end

0 comments on commit 6d0ae13

Please sign in to comment.