Skip to content

Commit

Permalink
Merge pull request #735 from karlnapf/master
Browse files Browse the repository at this point in the history
new cross-validation output system
  • Loading branch information
karlnapf committed Aug 17, 2012
2 parents 9bcdbb0 + 0b1bac4 commit 3103008
Show file tree
Hide file tree
Showing 34 changed files with 567 additions and 306 deletions.
76 changes: 41 additions & 35 deletions examples/undocumented/libshogun/classifier_libsvm.cpp
Expand Up @@ -5,99 +5,105 @@
* (at your option) any later version.
*
* Written (W) 2008-2009 Soeren Sonnenburg
* Written (W) 2012 Heiko Strathmann
* Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max Planck Society
*/
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/labels/BinaryLabels.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/classifier/svm/LibSVM.h>
#include <shogun/mathematics/Math.h>
#include <shogun/lib/common.h>
#include <shogun/base/init.h>

#include <stdlib.h>
#include <stdio.h>

using namespace shogun;

#define NUM 100
#define DIMS 2
#define DIST 0.5

float64_t* lab;
SGMatrix<float64_t> feat;

void gen_rand_data()
void gen_rand_data(SGVector<float64_t> lab, SGMatrix<float64_t> feat,
float64_t dist)
{
lab=SG_MALLOC(float64_t, NUM);
feat=SGMatrix<float64_t>(DIMS, NUM);
index_t dims=feat.num_rows;
index_t num=lab.vlen;

for (int32_t i=0; i<NUM; i++)
for (int32_t i=0; i<num; i++)
{
if (i<NUM/2)
if (i<num/2)
{
lab[i]=-1.0;

for (int32_t j=0; j<DIMS; j++)
feat.matrix[i*DIMS+j]=CMath::random(0.0,1.0)+DIST;
for (int32_t j=0; j<dims; j++)
feat(j, i)=CMath::random(0.0, 1.0)+dist;
}
else
{
lab[i]=1.0;

for (int32_t j=0; j<DIMS; j++)
feat.matrix[i*DIMS+j]=CMath::random(0.0,1.0)-DIST;
for (int32_t j=0; j<dims; j++)
feat(j, i)=CMath::random(0.0, 1.0)-dist;
}
}
SGVector<float64_t>::display_vector(lab,NUM);
SGMatrix<float64_t>::display_matrix(feat.matrix,DIMS, NUM);
lab.display_vector("lab");
feat.display_matrix("feat");
}

int main()
void test_libsvm()
{

const int32_t feature_cache=0;
const int32_t kernel_cache=0;
const float64_t rbf_width=10;
const float64_t svm_C=10;
const float64_t svm_eps=0.001;

init_shogun();
index_t num=100;
index_t dims=2;
float64_t dist=0.5;

gen_rand_data();
SGVector<float64_t> lab(num);
SGMatrix<float64_t> feat(dims, num);

gen_rand_data(lab, feat, dist);

// create train labels
CLabels* labels=new CBinaryLabels(SGVector<float64_t>(lab, NUM));
CLabels* labels=new CBinaryLabels(lab);

// create train features
CDenseFeatures<float64_t>* features = new CDenseFeatures<float64_t>(feature_cache);
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(
feature_cache);
SG_REF(features);
features->set_feature_matrix(feat);

// create gaussian kernel
CGaussianKernel* kernel = new CGaussianKernel(kernel_cache, rbf_width);
CGaussianKernel* kernel=new CGaussianKernel(kernel_cache, rbf_width);
SG_REF(kernel);
kernel->init(features, features);

// create svm via libsvm and train
CLibSVM* svm = new CLibSVM(svm_C, kernel, labels);
CLibSVM* svm=new CLibSVM(svm_C, kernel, labels);
SG_REF(svm);
svm->set_epsilon(svm_eps);
svm->train();

printf("num_sv:%d b:%f\n", svm->get_num_support_vectors(), svm->get_bias());
SG_SPRINT("num_sv:%d b:%f\n", svm->get_num_support_vectors(),
svm->get_bias());

// classify + display output
CBinaryLabels* out_labels=CBinaryLabels::obtain_from_generic(svm->apply());

for (int32_t i=0; i<NUM; i++)
printf("out[%d]=%f (%f)\n", i, out_labels->get_label(i), out_labels->get_confidence(i));
for (int32_t i=0; i<num; i++)
{
SG_SPRINT("out[%d]=%f (%f)\n", i, out_labels->get_label(i),
out_labels->get_confidence(i));
}

SG_UNREF(out_labels);
SG_UNREF(kernel);
SG_UNREF(features);
SG_UNREF(svm);
}

int main()
{
init_shogun();

test_libsvm();

exit_shogun();
return 0;
}

Expand Up @@ -108,8 +108,8 @@ void test_cross_validation()
cross->set_conf_int_alpha(0.05);

/* actual evaluation */
CrossValidationResult* result=(CrossValidationResult*)cross->evaluate();
CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate();

if (result->get_result_type() != CROSSVALIDATION_RESULT)
SG_SERROR("Evaluation result is not of type CrossValidationResult!");

Expand Down
Expand Up @@ -103,7 +103,7 @@ void test_cross_validation()
cross->set_num_runs(5);
cross->set_conf_int_alpha(0.05);

CrossValidationResult* tmp;
CCrossValidationResult* tmp;
/* no locking */
index_t repetitions=5;
SG_SPRINT("unlocked x-val\n");
Expand All @@ -113,7 +113,7 @@ void test_cross_validation()
time.start();
for (index_t i=0; i<repetitions; ++i)
{
tmp = (CrossValidationResult*)cross->evaluate();
tmp = (CCrossValidationResult*)cross->evaluate();
SG_UNREF(tmp);
}

Expand All @@ -127,7 +127,7 @@ void test_cross_validation()

for (index_t i=0; i<repetitions; ++i)
{
tmp = (CrossValidationResult*)cross->evaluate();
tmp = (CCrossValidationResult*)cross->evaluate();
SG_UNREF(tmp);
}

Expand All @@ -141,7 +141,7 @@ void test_cross_validation()

for (index_t i=0; i<repetitions; ++i)
{
tmp = (CrossValidationResult*)cross->evaluate();
tmp = (CCrossValidationResult*)cross->evaluate();
SG_UNREF(tmp);
}

Expand Down
Expand Up @@ -122,10 +122,10 @@ void test_cross_validation()
cross->set_conf_int_alpha(0.05);

/* actual evaluation */
CrossValidationResult* result=(CrossValidationResult*)cross->evaluate();
CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate();

if (result->get_result_type() != CROSSVALIDATION_RESULT)
SG_SERROR("Evaluation result is not of type CrossValidationResult!");
SG_SERROR("Evaluation result is not of type CCrossValidationResult!");

result->print_result();

Expand Down
Expand Up @@ -123,7 +123,7 @@ void test_multiclass_mkl_cv()
cross->set_conf_int_alpha(0.05);

/* perform x-val and print result */
CrossValidationResult* result=(CrossValidationResult*)cross->evaluate();
CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate();
SG_SPRINT("mean of %d %d-fold x-val runs: %f\n", n_runs, n_folds,
result->mean);

Expand Down
Expand Up @@ -95,10 +95,10 @@ void test_cross_validation()
cross->set_conf_int_alpha(0.05);

/* actual evaluation */
CrossValidationResult* result=(CrossValidationResult*)cross->evaluate();
CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate();

if (result->get_result_type() != CROSSVALIDATION_RESULT)
SG_SERROR("Evaluation result is not of type CrossValidationResult!");
SG_SERROR("Evaluation result is not of type CCrossValidationResult!");

SG_SPRINT("cross_validation estimate:\n");
result->print_result();
Expand Down
Expand Up @@ -153,10 +153,10 @@ int main(int argc, char **argv)
/* larger number of runs to have tighter confidence intervals */
cross->set_num_runs(10);
cross->set_conf_int_alpha(0.01);
CrossValidationResult* result=(CrossValidationResult*)cross->evaluate();
CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate();

if (result->get_result_type() != CROSSVALIDATION_RESULT)
SG_SERROR("Evaluation result is not of type CrossValidationResult!");
SG_SERROR("Evaluation result is not of type CCrossValidationResult!");

SG_SPRINT("result: ");
result->print_result();
Expand All @@ -167,10 +167,10 @@ int main(int argc, char **argv)
best_combination=grid_search->select_model(print_state);
best_combination->apply_to_machine(classifier);
SG_UNREF(result);
result=(CrossValidationResult*)cross->evaluate();
result=(CCrossValidationResult*)cross->evaluate();

if (result->get_result_type() != CROSSVALIDATION_RESULT)
SG_SERROR("Evaluation result is not of type CrossValidationResult!");
SG_SERROR("Evaluation result is not of type CCrossValidationResult!");

SG_SPRINT("result (unlocked): ");

Expand Down
Expand Up @@ -139,10 +139,10 @@ void test_cross_validation()
/* larger number of runs to have tighter confidence intervals */
cross->set_num_runs(10);
cross->set_conf_int_alpha(0.01);
CrossValidationResult* result=(CrossValidationResult*)cross->evaluate();
CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate();

if (result->get_result_type() != CROSSVALIDATION_RESULT)
SG_SERROR("Evaluation result is not of type CrossValidationResult!");
SG_SERROR("Evaluation result is not of type CCrossValidationResult!");

SG_SPRINT("result: ");
result->print_result();
Expand Down
Expand Up @@ -98,10 +98,10 @@ int main(int argc, char **argv)
best_combination->print_tree();

best_combination->apply_to_machine(classifier);
CrossValidationResult* result=(CrossValidationResult*)cross->evaluate();
CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate();

if (result->get_result_type() != CROSSVALIDATION_RESULT)
SG_SERROR("Evaluation result is not of type CrossValidationResult!");
SG_SERROR("Evaluation result is not of type CCrossValidationResult!");

result->print_result();

Expand Down
Expand Up @@ -138,10 +138,10 @@ int main(int argc, char **argv)
/* larger number of runs to have tighter confidence intervals */
cross->set_num_runs(10);
cross->set_conf_int_alpha(0.01);
CrossValidationResult* result=(CrossValidationResult*)cross->evaluate();
CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate();

if (result->get_result_type() != CROSSVALIDATION_RESULT)
SG_ERROR("Evaluation result is not of type CrossValidationResult!");
SG_ERROR("Evaluation result is not of type CCrossValidationResult!");

SG_SPRINT("result: ");
result.print_result();
Expand Down
Expand Up @@ -146,10 +146,10 @@ int main(int argc, char **argv)
cross->set_num_runs(10);
cross->set_conf_int_alpha(0.01);
classifier->data_lock(labels, features);
CrossValidationResult* result=(CrossValidationResult*)cross->evaluate();
CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate();

if (result->get_result_type() != CROSSVALIDATION_RESULT)
SG_SERROR("Evaluation result is not of type CrossValidationResult!");
SG_SERROR("Evaluation result is not of type CCrossValidationResult!");

SG_SPRINT("result: ");
result->print_result();
Expand Down
4 changes: 3 additions & 1 deletion src/interfaces/modular/Evaluation.i
Expand Up @@ -43,7 +43,8 @@
%rename(GradientCriterion) CGradientCriterion;
%rename(GradientEvaluation) CGradientEvaluation;
%rename(MulticlassOVREvaluation) CMulticlassOVREvaluation;

%rename(CrossValidationOutput) CCMulticlassOVREvaluation;
%rename(CrossValidationResult) CCrossValidationResult;


/* Include Class Headers to make them visible from within the target language */
Expand All @@ -70,3 +71,4 @@
%include <shogun/evaluation/MulticlassOVREvaluation.h>
%include <shogun/evaluation/StratifiedCrossValidationSplitting.h>
%include <shogun/evaluation/CrossValidationSplitting.h>
%include <shogun/evaluation/CrossValidationOutput.h>
1 change: 1 addition & 0 deletions src/interfaces/modular/Evaluation_includes.i
Expand Up @@ -22,5 +22,6 @@
#include <shogun/evaluation/SplittingStrategy.h>
#include <shogun/evaluation/StratifiedCrossValidationSplitting.h>
#include <shogun/evaluation/CrossValidationSplitting.h>
#include <shogun/evaluation/CrossValidationOutput.h>
%}

4 changes: 1 addition & 3 deletions src/interfaces/modular/ModelSelection.i
Expand Up @@ -8,7 +8,7 @@
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/

SERIALIZABLE_DUMMY(shogun::CrossValidationResult);
SERIALIZABLE_DUMMY(shogun::CCrossValidationResult);

/* These functions return new Objects */
%newobject *::select_model();
Expand All @@ -24,11 +24,9 @@ SERIALIZABLE_DUMMY(shogun::CrossValidationResult);
%rename(RandomSearchModelSelection) CRandomSearchModelSelection;
%rename(GradientModelSelection) CGradientModelSelection;
%rename(ModelSelectionBase) CModelSelection;
%rename(ModelSelectionOutput) CModelSelectionOutput;
%rename(ModelSelectionParameters) CModelSelectionParameters;
%rename(ParameterCombination) CParameterCombination;

%include <shogun/modelselection/ModelSelectionOutput.h>
%include <shogun/modelselection/ModelSelection.h>
%include <shogun/modelselection/GridSearchModelSelection.h>
%include <shogun/modelselection/RandomSearchModelSelection.h>
Expand Down
1 change: 0 additions & 1 deletion src/interfaces/modular/ModelSelection_includes.i
@@ -1,5 +1,4 @@
%{
#include <shogun/modelselection/ModelSelectionOutput.h>
#include <shogun/modelselection/ModelSelection.h>
#include <shogun/modelselection/ModelSelectionParameters.h>
#include <shogun/modelselection/GridSearchModelSelection.h>
Expand Down

0 comments on commit 3103008

Please sign in to comment.