Skip to content

Commit

Permalink
Fix ECOC memory error with new SGVector.
Browse files Browse the repository at this point in the history
  • Loading branch information
pluskid committed May 6, 2012
1 parent 9a18a64 commit 786359f
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 8 deletions.
Expand Up @@ -96,7 +96,6 @@ int main(int argc, char** argv)
CMath::display_vector(out_labels.vector, out_labels.vlen);

// Free resources
out_labels.destroy_vector();
SG_UNREF(mc_svm);
SG_UNREF(svm);
SG_UNREF(output);
Expand Down
112 changes: 112 additions & 0 deletions examples/undocumented/libshogun/classifier_multiclass_ecoc_random.cpp
@@ -0,0 +1,112 @@
#include <shogun/features/Labels.h>
#include <shogun/io/StreamingAsciiFile.h>
#include <shogun/io/SGIO.h>
#include <shogun/features/StreamingSimpleFeatures.h>
#include <shogun/features/SimpleFeatures.h>
#include <shogun/multiclass/ecoc/ECOCStrategy.h>
#include <shogun/multiclass/ecoc/ECOCRandomSparseEncoder.h>
#include <shogun/multiclass/ecoc/ECOCHDDecoder.h>
#include <shogun/machine/LinearMulticlassMachine.h>
#include <shogun/classifier/svm/LibLinear.h>
#include <shogun/base/init.h>

#define EPSILON 1e-5

using namespace shogun;

int main(int argc, char** argv)
{
int32_t num_vectors = 0;
int32_t num_feats = 2;

init_shogun_with_defaults();

// Prepare to read a file for the training data
char fname_feats[] = "../data/fm_train_real.dat";
char fname_labels[] = "../data/label_train_multiclass.dat";
CStreamingAsciiFile* ffeats_train = new CStreamingAsciiFile(fname_feats);
CStreamingAsciiFile* flabels_train = new CStreamingAsciiFile(fname_labels);
SG_REF(ffeats_train);
SG_REF(flabels_train);

CStreamingSimpleFeatures< float64_t >* stream_features =
new CStreamingSimpleFeatures< float64_t >(ffeats_train, false, 1024);

CStreamingSimpleFeatures< float64_t >* stream_labels =
new CStreamingSimpleFeatures< float64_t >(flabels_train, true, 1024);

SG_REF(stream_features);
SG_REF(stream_labels);

// Create a matrix with enough space to read all the feature vectors
SGMatrix< float64_t > mat = SGMatrix< float64_t >(num_feats, 1000);

// Read the values from the file and store them in mat
SGVector< float64_t > vec;
stream_features->start_parser();
while ( stream_features->get_next_example() )
{
vec = stream_features->get_vector();

for ( int32_t i = 0 ; i < num_feats ; ++i )
mat[num_vectors*num_feats + i] = vec[i];

num_vectors++;
stream_features->release_example();
}
stream_features->end_parser();

// Create features with the useful values from mat
CSimpleFeatures< float64_t >* features = new CSimpleFeatures< float64_t >(mat.matrix, num_feats, num_vectors);

CLabels* labels = new CLabels(num_vectors);
SG_REF(features);
SG_REF(labels);

// Read the labels from the file
int32_t idx = 0;
stream_labels->start_parser();
while ( stream_labels->get_next_example() )
{
labels->set_int_label( idx++, (int32_t)stream_labels->get_label() );
stream_labels->release_example();
}
stream_labels->end_parser();

// Create liblinear svm classifier with L2-regularized L2-loss
CLibLinear* svm = new CLibLinear(L2R_L2LOSS_SVC);
SG_REF(svm);

// Add some configuration to the svm
svm->set_epsilon(EPSILON);
svm->set_bias_enabled(true);

// Create a multiclass svm classifier that consists of several of the previous one
CLinearMulticlassMachine* mc_svm = new CLinearMulticlassMachine(
new CECOCStrategy(new CECOCRandomSparseEncoder(), new CECOCHDDecoder()), (CDotFeatures*) features, svm, labels);
SG_REF(mc_svm);

// Train the multiclass machine using the data passed in the constructor
mc_svm->train();

// Classify the training examples and show the results
CLabels* output = mc_svm->apply();

SGVector< int32_t > out_labels = output->get_int_labels();
CMath::display_vector(out_labels.vector, out_labels.vlen);

// Free resources
SG_UNREF(mc_svm);
SG_UNREF(svm);
SG_UNREF(output);
SG_UNREF(features);
SG_UNREF(labels);
//SG_UNREF(ffeats_train);
//SG_UNREF(flabels_train);
SG_UNREF(stream_features);
SG_UNREF(stream_labels);
exit_shogun();

return 0;
}

2 changes: 1 addition & 1 deletion src/shogun/multiclass/ecoc/ECOCOVOEncoder.cpp
Expand Up @@ -14,7 +14,7 @@ using namespace shogun;

SGMatrix<int32_t> CECOCOVOEncoder::create_codebook(int32_t num_classes)
{
SGMatrix<int32_t> code_book(num_classes*(num_classes-1)/2, num_classes);
SGMatrix<int32_t> code_book(num_classes*(num_classes-1)/2, num_classes, true);
code_book.zero();
int32_t k=0;
for (int32_t i=0; i < num_classes; ++i)
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/multiclass/ecoc/ECOCOVREncoder.cpp
Expand Up @@ -14,7 +14,7 @@ using namespace shogun;

SGMatrix<int32_t> CECOCOVREncoder::create_codebook(int32_t num_classes)
{
SGMatrix<int32_t> code_book(num_classes, num_classes);
SGMatrix<int32_t> code_book(num_classes, num_classes, true);
code_book.set_const(-1);
for (int32_t i=0; i < num_classes; ++i)
code_book(i, i) = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/multiclass/ecoc/ECOCRandomSparseEncoder.cpp
Expand Up @@ -51,7 +51,7 @@ SGMatrix<int32_t> CECOCRandomSparseEncoder::create_codebook(int32_t num_classes)
if (codelen <= 0)
codelen = get_default_code_length(num_classes);

SGMatrix<int32_t> best_codebook(codelen, num_classes);
SGMatrix<int32_t> best_codebook(codelen, num_classes, true);
int32_t best_dist = 0;

SGMatrix<int32_t> codebook(codelen, num_classes);
Expand Down
8 changes: 4 additions & 4 deletions src/shogun/multiclass/ecoc/ECOCStrategy.cpp
Expand Up @@ -37,14 +37,14 @@ CECOCStrategy::~CECOCStrategy()
SG_UNREF(m_encoder);
SG_UNREF(m_decoder);

m_codebook.destroy_matrix();
m_codebook.free_matrix();
}

void CECOCStrategy::train_start(CLabels *orig_labels, CLabels *train_labels)
{
CMulticlassStrategy::train_start(orig_labels, train_labels);

m_codebook.destroy_matrix();
m_codebook.free_matrix();
m_codebook = m_encoder->create_codebook(m_num_classes);
}

Expand All @@ -55,7 +55,7 @@ bool CECOCStrategy::train_has_more()

SGVector<int32_t> CECOCStrategy::train_prepare_next()
{
SGVector<int32_t> subset(m_orig_labels->get_num_labels());
SGVector<int32_t> subset(m_orig_labels->get_num_labels(), false);
int32_t tot=0;
for (int32_t i=0; i < m_orig_labels->get_num_labels(); ++i)
{
Expand All @@ -77,7 +77,7 @@ SGVector<int32_t> CECOCStrategy::train_prepare_next()
}

CMulticlassStrategy::train_prepare_next();
return SGVector<int32_t>(subset.vector, tot);
return SGVector<int32_t>(subset.vector, tot, true);
}

int32_t CECOCStrategy::decide_label(SGVector<float64_t> outputs)
Expand Down

0 comments on commit 786359f

Please sign in to comment.