Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #223 from sploving/master
add lua docs and further examples
- Loading branch information
Showing
16 changed files
with
309 additions
and
16 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
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
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
78 changes: 78 additions & 0 deletions
78
examples/undocumented/lua_modular/classifier_libsvm_minimal_modular.lua
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,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) |
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
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
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
39 changes: 39 additions & 0 deletions
39
examples/undocumented/lua_modular/kernel_comm_ulong_string_modular.lua
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,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 |
37 changes: 37 additions & 0 deletions
37
examples/undocumented/lua_modular/kernel_comm_word_string_modular.lua
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,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 |
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
24 changes: 24 additions & 0 deletions
24
examples/undocumented/lua_modular/kernel_linear_byte_modular.lua
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,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
26
examples/undocumented/lua_modular/kernel_linear_word_modular.lua
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,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 |
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
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
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
Oops, something went wrong.