Skip to content

Commit

Permalink
Added larank libshogun example with multiple training call
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jul 17, 2012
1 parent 48a7858 commit 989a7f7
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions examples/undocumented/libshogun/classifier_larank.cpp
@@ -0,0 +1,63 @@
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/multiclass/LaRank.h>
#include <shogun/base/init.h>

using namespace shogun;

void print_message(FILE* target, const char* str)
{
fprintf(target, "%s", str);
}

int main(int argc, char** argv)
{
init_shogun(&print_message);
index_t num_vec=3;
index_t num_feat=2;
index_t num_class=2;

// create some data
SGMatrix<float64_t> matrix(num_feat, num_vec);
SGVector<float64_t>::range_fill_vector(matrix.matrix, num_feat*num_vec);

// create vectors
// shogun will now own the matrix created
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(matrix);

// create three labels
CMulticlassLabels* labels=new CMulticlassLabels(num_vec);
for (index_t i=0; i<num_vec; ++i)
labels->set_label(i, i%num_class);

// create gaussian kernel with cache 10MB, width 0.5
CGaussianKernel* kernel = new CGaussianKernel(10, 0.5);
kernel->init(features, features);

// create libsvm with C=10 and train
CLaRank* svm = new CLaRank(10, kernel, labels);
svm->train();
svm->train();

// classify on training examples
CMulticlassLabels* output=CMulticlassLabels::obtain_from_generic(svm->apply());
SGVector<float64_t>::display_vector(output->get_labels().vector, output->get_num_labels(),
"batch output");

/* assert that batch apply and apply(index_t) give same result */
for (index_t i=0; i<output->get_num_labels(); ++i)
{
float64_t label=svm->apply_one(i);
SG_SPRINT("single output[%d]=%f\n", i, label);
ASSERT(output->get_label(i)==label);
}
SG_UNREF(output);

// free up memory
SG_UNREF(svm);

exit_shogun();
return 0;
}

0 comments on commit 989a7f7

Please sign in to comment.