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 #597 from pluskid/multiclass
ShareBoost
- Loading branch information
Showing
11 changed files
with
2,893 additions
and
1 deletion.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
examples/undocumented/libshogun/classifier_multiclass_shareboost.cpp
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,94 @@ | ||
#include <algorithm> | ||
|
||
#include <shogun/labels/MulticlassLabels.h> | ||
#include <shogun/io/StreamingAsciiFile.h> | ||
#include <shogun/io/SGIO.h> | ||
#include <shogun/features/StreamingDenseFeatures.h> | ||
#include <shogun/features/DenseFeatures.h> | ||
#include <shogun/features/DenseSubsetFeatures.h> | ||
#include <shogun/base/init.h> | ||
#include <shogun/multiclass/ShareBoost.h> | ||
|
||
#define EPSILON 1e-5 | ||
|
||
using namespace shogun; | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
int32_t num_vectors = 0; | ||
int32_t num_feats = 0; | ||
|
||
init_shogun_with_defaults(); | ||
|
||
const char*fname_train = "../data/7class_example4_train.dense"; | ||
CStreamingAsciiFile *train_file = new CStreamingAsciiFile(fname_train); | ||
SG_REF(train_file); | ||
|
||
CStreamingDenseFeatures<float64_t> *stream_features = new CStreamingDenseFeatures<float64_t>(train_file, true, 1024); | ||
SG_REF(stream_features); | ||
|
||
SGMatrix<float64_t> mat; | ||
SGVector<float64_t> labvec(1000); | ||
|
||
stream_features->start_parser(); | ||
SGVector< float64_t > vec; | ||
while (stream_features->get_next_example()) | ||
{ | ||
vec = stream_features->get_vector(); | ||
if (num_feats == 0) | ||
{ | ||
num_feats = vec.vlen; | ||
mat = SGMatrix<float64_t>(num_feats, 1000); | ||
} | ||
std::copy(vec.vector, vec.vector+vec.vlen, mat.get_column_vector(num_vectors)); | ||
labvec[num_vectors] = stream_features->get_label(); | ||
num_vectors++; | ||
stream_features->release_example(); | ||
} | ||
stream_features->end_parser(); | ||
mat.num_cols = num_vectors; | ||
labvec.vlen = num_vectors; | ||
|
||
CMulticlassLabels* labels = new CMulticlassLabels(labvec); | ||
SG_REF(labels); | ||
|
||
// Create features with the useful values from mat | ||
CDenseFeatures< float64_t >* features = new CDenseFeatures<float64_t>(mat); | ||
SG_REF(features); | ||
|
||
SG_SPRINT("Performing ShareBoost on a %d-class problem\n", labels->get_num_classes()); | ||
|
||
// Create ShareBoost Machine | ||
CShareBoost *machine = new CShareBoost(features, labels, 10); | ||
SG_REF(machine); | ||
|
||
machine->train(); | ||
|
||
SGVector<int32_t> activeset = machine->get_activeset(); | ||
SG_SPRINT("%d out of %d features are selected:\n", activeset.vlen, mat.num_rows); | ||
for (int32_t i=0; i < activeset.vlen; ++i) | ||
SG_SPRINT("activeset[%02d] = %d\n", i, activeset[i]); | ||
|
||
CDenseSubsetFeatures<float64_t> *subset_fea = new CDenseSubsetFeatures<float64_t>(features, machine->get_activeset()); | ||
SG_REF(subset_fea); | ||
CMulticlassLabels* output = CMulticlassLabels::obtain_from_generic(machine->apply(subset_fea)); | ||
|
||
int32_t correct = 0; | ||
for (int32_t i=0; i < output->get_num_labels(); ++i) | ||
if (output->get_int_label(i) == labels->get_int_label(i)) | ||
correct++; | ||
SG_SPRINT("Accuracy = %.4f\n", float64_t(correct)/labels->get_num_labels()); | ||
|
||
// Free resources | ||
SG_UNREF(machine); | ||
SG_UNREF(output); | ||
SG_UNREF(subset_fea); | ||
SG_UNREF(features); | ||
SG_UNREF(labels); | ||
SG_UNREF(train_file); | ||
SG_UNREF(stream_features); | ||
exit_shogun(); | ||
|
||
return 0; | ||
} | ||
|
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,81 @@ | ||
#include <cstdio> | ||
#include <shogun/optimization/lbfgs/lbfgs.h> | ||
|
||
static lbfgsfloatval_t evaluate( | ||
void *instance, | ||
const lbfgsfloatval_t *x, | ||
lbfgsfloatval_t *g, | ||
const int n, | ||
const lbfgsfloatval_t step | ||
) | ||
{ | ||
int i; | ||
lbfgsfloatval_t fx = 0.0; | ||
|
||
for (i = 0;i < n;i += 2) { | ||
lbfgsfloatval_t t1 = 1.0 - x[i]; | ||
lbfgsfloatval_t t2 = 10.0 * (x[i+1] - x[i] * x[i]); | ||
g[i+1] = 20.0 * t2; | ||
g[i] = -2.0 * (x[i] * g[i+1] + t1); | ||
fx += t1 * t1 + t2 * t2; | ||
} | ||
return fx; | ||
} | ||
|
||
static int progress( | ||
void *instance, | ||
const lbfgsfloatval_t *x, | ||
const lbfgsfloatval_t *g, | ||
const lbfgsfloatval_t fx, | ||
const lbfgsfloatval_t xnorm, | ||
const lbfgsfloatval_t gnorm, | ||
const lbfgsfloatval_t step, | ||
int n, | ||
int k, | ||
int ls | ||
) | ||
{ | ||
printf("Iteration %d:\n", k); | ||
printf(" fx = %f, x[0] = %f, x[1] = %f\n", fx, x[0], x[1]); | ||
printf(" xnorm = %f, gnorm = %f, step = %f\n", xnorm, gnorm, step); | ||
printf("\n"); | ||
return 0; | ||
} | ||
|
||
#define N 100 | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int i, ret = 0; | ||
lbfgsfloatval_t fx; | ||
lbfgsfloatval_t *x = lbfgs_malloc(N); | ||
lbfgs_parameter_t param; | ||
|
||
if (x == NULL) { | ||
printf("ERROR: Failed to allocate a memory block for variables.\n"); | ||
return 1; | ||
} | ||
|
||
/* Initialize the variables. */ | ||
for (i = 0;i < N;i += 2) { | ||
x[i] = -1.2; | ||
x[i+1] = 1.0; | ||
} | ||
|
||
/* Initialize the parameters for the L-BFGS optimization. */ | ||
lbfgs_parameter_init(¶m); | ||
/*param.linesearch = LBFGS_LINESEARCH_BACKTRACKING;*/ | ||
|
||
/* | ||
Start the L-BFGS optimization; this will invoke the callback functions | ||
evaluate() and progress() when necessary. | ||
*/ | ||
ret = lbfgs(N, x, &fx, evaluate, progress, NULL, ¶m); | ||
|
||
/* Report the result. */ | ||
printf("L-BFGS optimization terminated with status code = %d\n", ret); | ||
printf(" fx = %f, x[0] = %f, x[1] = %f\n", fx, x[0], x[1]); | ||
|
||
lbfgs_free(x); | ||
return 0; | ||
} |
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,12 @@ | ||
/* | ||
* 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 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Written (W) 2012 Chiyuan Zhang | ||
* Copyright (C) 2012 Chiyuan Zhang | ||
*/ | ||
|
||
#include <shogun/features/DenseSubsetFeatures.h> | ||
|
Oops, something went wrong.