本页面包含了所有C++ libshogun接口的例子。
如果要运行这些例子你需要像下面这样编译它们:
g++ name_of_example.cpp -lshogun
如果你是将libshogun安装到非标准目录,你需要指定库目录和头文件目录,例如
g++ -I/path/to/libshogun/includes name_of_example.cpp -L/path/to/libshogun/sofile -lshogun
如果shogun被安装到标准目录,可以像下面这样运行例子
./name_of_example
相反如果被安装到非标准目录(那样它们不能被动态链接器找到),你需要指定
LD_LIBRARY_PATH=path/to/libshogun ./name_of_example
/* * 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) 2011 Shashwat Lal Das * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society * * This example demonstrates use of the Vowpal Wabbit learning algorithm. */ #include <shogun/base/init.h> #include <shogun/lib/common.h> #include <shogun/io/streaming/StreamingAsciiFile.h> #include <shogun/features/streaming/StreamingDenseFeatures.h> #include <shogun/multiclass/tree/BalancedConditionalProbabilityTree.h> using namespace shogun; int main(int argc, char **argv) { init_shogun_with_defaults(); const char* train_file_name = "../data/7class_example4_train.dense"; const char* test_file_name = "../data/7class_example4_test.dense"; CStreamingAsciiFile* train_file = new CStreamingAsciiFile(train_file_name); SG_REF(train_file); CStreamingDenseFeatures<float32_t>* train_features = new CStreamingDenseFeatures<float32_t>(train_file, true, 1024); SG_REF(train_features); CBalancedConditionalProbabilityTree *cpt = new CBalancedConditionalProbabilityTree(); cpt->set_num_passes(1); cpt->set_features(train_features); if (argc > 1) { float64_t alpha = 0.5; sscanf(argv[1], "%lf", &alpha); SG_SPRINT("Setting alpha to %.2lf\n", alpha); cpt->set_alpha(alpha); } cpt->train(); cpt->print_tree(); CStreamingAsciiFile* test_file = new CStreamingAsciiFile(test_file_name); SG_REF(test_file); CStreamingDenseFeatures<float32_t>* test_features = new CStreamingDenseFeatures<float32_t>(test_file, true, 1024); SG_REF(test_features); CMulticlassLabels *pred = cpt->apply_multiclass(test_features); test_features->reset_stream(); SG_SPRINT("num_labels = %d\n", pred->get_num_labels()); SG_UNREF(test_features); SG_UNREF(test_file); test_file = new CStreamingAsciiFile(test_file_name); SG_REF(test_file); test_features = new CStreamingDenseFeatures<float32_t>(test_file, true, 1024); SG_REF(test_features); CMulticlassLabels *gnd = new CMulticlassLabels(pred->get_num_labels()); SG_REF(gnd); test_features->start_parser(); for (int32_t i=0; i < pred->get_num_labels(); ++i) { test_features->get_next_example(); gnd->set_int_label(i, test_features->get_label()); test_features->release_example(); } test_features->end_parser(); int32_t n_correct = 0; for (index_t i=0; i < pred->get_num_labels(); ++i) { if (pred->get_int_label(i) == gnd->get_int_label(i)) n_correct++; //SG_SPRINT("%d-%d ", pred->get_int_label(i), gnd->get_int_label(i)); } SG_SPRINT("\n"); SG_SPRINT("Multiclass Accuracy = %.2f%%\n", 100.0*n_correct / gnd->get_num_labels()); SG_UNREF(gnd); SG_UNREF(train_features); SG_UNREF(test_features); SG_UNREF(train_file); SG_UNREF(test_file); SG_UNREF(cpt); SG_UNREF(pred); exit_shogun(); return 0; }
/* * 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) 2011 Heiko Strathmann * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/base/Parameter.h> #include <shogun/io/SerializableAsciiFile.h> #include <shogun/base/ParameterMap.h> #include <shogun/features/DenseFeatures.h> #include <unistd.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } class CTestClassInt : public CSGObject { public: CTestClassInt() { m_number=10; m_parameters->add(&m_number, "number", "Test number"); m_vector_length=3; m_vector=SG_MALLOC(int32_t, m_vector_length); SGVector<int32_t>::fill_vector(m_vector, m_vector_length, 10); m_parameters->add_vector(&m_vector, &m_vector_length, "vector", "Test vector"); m_matrix_rows=2; m_matrix_cols=3; m_matrix=SG_MALLOC(int32_t, m_matrix_rows*m_matrix_cols); SGVector<int32_t>::range_fill_vector(m_matrix, m_matrix_rows*m_matrix_cols); m_parameters->add_matrix(&m_matrix, &m_matrix_rows, &m_matrix_cols, "matrix", "Test matrix"); SGMatrix<int32_t> features=SGMatrix<int32_t>(2, 3); SGVector<int32_t>::range_fill_vector(features.matrix, features.num_rows*features.num_cols, 3); m_features=new CDenseFeatures<int32_t>(features); SG_REF(m_features); m_parameters->add((CSGObject**)&m_features, "int_features", "Test features"); } virtual ~CTestClassInt() { SG_FREE(m_vector); SG_FREE(m_matrix); SG_UNREF(m_features); } int32_t m_number; int32_t* m_vector; int32_t m_vector_length; int32_t* m_matrix; int32_t m_matrix_rows; int32_t m_matrix_cols; CDenseFeatures<int32_t>* m_features; virtual const char* get_name() const { return "TestClassInt"; } }; class CTestClassFloat : public CSGObject { public: CTestClassFloat() { m_number=3.2; m_vector=SGVector<float64_t>(10); m_matrix=SGMatrix<float64_t>(2, 3); m_parameters->add(&m_number, "number", "Test number"); m_parameters->add(&m_vector, "vector", "Test vector"); m_parameters->add(&m_matrix, "matrix", "Test matrix"); SGMatrix<float64_t> features=SGMatrix<float64_t>(2, 3); SGVector<float64_t>::range_fill_vector(features.matrix, features.num_rows*features.num_cols, 3.0); m_features=new CDenseFeatures<float64_t>(features); SG_REF(m_features); m_parameters->add((CSGObject**)&m_features, "float_features", "Test features"); /* add some parameter mappings for number, here: type changes */ m_parameter_map->put( new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 1), new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0) ); m_parameter_map->put( new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0), new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT32, -1) ); /* changes for vector: from int32_t vector to float64_t SG_VECTOR */ m_parameter_map->put( new SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_FLOAT64, 1), new SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0) ); /* from normal vector to SG_VECTOR of same type */ m_parameter_map->put( new SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0), new SGParamInfo("vector", CT_VECTOR, ST_NONE, PT_INT32, -1) ); /* changes for vector: from int32_t vector to float64_t SG_VECTOR */ m_parameter_map->put( new SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_FLOAT64, 1), new SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0) ); /* from normal vector to SG_VECTOR of same type */ m_parameter_map->put( new SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0), new SGParamInfo("matrix", CT_MATRIX, ST_NONE, PT_INT32, -1) ); /* name change for sgobject */ m_parameter_map->put( new SGParamInfo("float_features", CT_SCALAR, ST_NONE, PT_SGOBJECT, 1), new SGParamInfo("int_features", CT_SCALAR, ST_NONE, PT_SGOBJECT, 0) ); m_parameter_map->finalize_map(); } virtual ~CTestClassFloat() { SG_UNREF(m_features); } float64_t m_number; SGVector<float64_t> m_vector; SGMatrix<float64_t> m_matrix; CDenseFeatures<float64_t>* m_features; virtual const char* get_name() const { return "TestClassFloat"; } }; void test_load_file_parameter() { char filename_tmp[] = "load_all_file_test.XXXXXX"; char* filename=mktemp(filename_tmp); /* create one instance of each class */ CTestClassInt* int_instance=new CTestClassInt(); CTestClassFloat* float_instance=new CTestClassFloat(); CSerializableAsciiFile* file; /* serialize int instance */ file=new CSerializableAsciiFile(filename, 'w'); int_instance->save_serializable(file); file->close(); SG_UNREF(file); /* reopen file for reading */ file=new CSerializableAsciiFile(filename, 'r'); int32_t file_version=-1; /* load all parameter data, current version is set to 1 here */ DynArray<TParameter*>* params= float_instance->load_all_file_parameters(file_version, 1, file, ""); /* test the result */ for (index_t i=0; i<params->get_num_elements(); ++i) { TParameter* current=params->get_element(i); current->m_delete_data = true; // TODO: This shouldn't be necessary! /* ensure that data is same as of the instance for all parameters */ if (!strcmp(current->m_name, "number")) { int32_t value_number=*((int32_t*)current->m_parameter); SG_SPRINT("%i\n", value_number); ASSERT(value_number=int_instance->m_number); } else if (!strcmp(current->m_name, "vector")) { int32_t* value_vector=*((int32_t**)current->m_parameter); SGVector<int32_t>::display_vector(value_vector, int_instance->m_vector_length); for (index_t j=0; j<int_instance->m_vector_length; ++j) ASSERT(value_vector[j]=int_instance->m_vector[j]); } else if (!strcmp(current->m_name, "matrix")) { int32_t* value_matrix=*((int32_t**)current->m_parameter); SGMatrix<int32_t>::display_matrix(value_matrix, int_instance->m_matrix_rows, int_instance->m_matrix_cols); for (index_t j=0; j<int_instance->m_matrix_rows*int_instance->m_matrix_cols; ++j) { ASSERT(value_matrix[j]==int_instance->m_matrix[j]); } } else if (!strcmp(current->m_name, "int_features")) { CDenseFeatures<int32_t>* features= *((CDenseFeatures<int32_t>**) current->m_parameter); SGMatrix<int32_t> feature_matrix_loaded= features->get_feature_matrix(); SGMatrix<int32_t> feature_matrix_original= int_instance->m_features->get_feature_matrix(); SGMatrix<int32_t>::display_matrix(feature_matrix_loaded.matrix, feature_matrix_loaded.num_rows, feature_matrix_loaded.num_cols, "features"); for (index_t j=0; j<int_instance->m_matrix_rows*int_instance->m_matrix_cols; ++j) { ASSERT(feature_matrix_original.matrix[j]== feature_matrix_loaded.matrix[j]); } } } /* assert that parameter data is sorted */ for (index_t i=1; i<params->get_num_elements(); ++i) { /* assert via TParameter < and == operator */ TParameter* t1=params->get_element(i-1); TParameter* t2=params->get_element(i); ASSERT((*t1)<(*t2) || (*t1)==(*t2)); /* assert via name (which is used in the operator, but to be sure */ const char* s1=t1->m_name; const char* s2=t2->m_name; SG_SPRINT("param \"%s\" <= \"%s\" ? ... ", s1, s2); ASSERT(strcmp(s1, s2)<=0); SG_SPRINT("yes\n"); } /* clean up */ for (index_t i=0; i<params->get_num_elements(); ++i) delete params->get_element(i); delete params; file->close(); SG_UNREF(file); SG_UNREF(int_instance); SG_UNREF(float_instance); unlink(filename); } int main(int argc, char **argv) { init_shogun(&print_message, &print_message, &print_message); test_load_file_parameter(); exit_shogun(); return 0; }
/* * 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) 2011 Heiko Strathmann * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/base/Parameter.h> #include <shogun/io/SGIO.h> #include <shogun/io/SerializableAsciiFile.h> #include <shogun/base/ParameterMap.h> #include <shogun/features/DenseFeatures.h> #include <unistd.h> using namespace shogun; class CTestClassInt : public CSGObject { public: CTestClassInt() { m_number=10; m_parameters->add(&m_number, "number", "Test number"); m_vector_length=3; m_vector=SG_MALLOC(int32_t, m_vector_length); SGVector<int32_t>::fill_vector(m_vector, m_vector_length, 10); m_parameters->add_vector(&m_vector, &m_vector_length, "vector", "Test vector"); m_matrix_rows=2; m_matrix_cols=3; m_matrix=SG_MALLOC(int32_t, m_matrix_rows*m_matrix_cols); SGVector<int32_t>::range_fill_vector(m_matrix, m_matrix_rows*m_matrix_cols); m_parameters->add_matrix(&m_matrix, &m_matrix_rows, &m_matrix_cols, "matrix", "Test matrix"); SGMatrix<int32_t> features=SGMatrix<int32_t>(2, 3); SGVector<int32_t>::range_fill_vector(features.matrix, features.num_rows*features.num_cols, 3); m_features=new CDenseFeatures<int32_t>(features); SG_REF(m_features); m_parameters->add((CSGObject**)&m_features, "int_features", "Test features"); } virtual ~CTestClassInt() { SG_FREE(m_vector); SG_FREE(m_matrix); SG_UNREF(m_features); } int32_t m_number; int32_t* m_vector; int32_t m_vector_length; int32_t* m_matrix; int32_t m_matrix_rows; int32_t m_matrix_cols; CDenseFeatures<int32_t>* m_features; virtual const char* get_name() const { return "TestClassInt"; } }; class CTestClassFloat : public CSGObject { public: CTestClassFloat() { m_number=3.2; m_vector=SGVector<float64_t>(10); m_matrix=SGMatrix<float64_t>(2, 3); m_parameters->add(&m_number, "number", "Test number"); m_parameters->add(&m_vector, "vector", "Test vector"); m_parameters->add(&m_matrix, "matrix", "Test matrix"); SGMatrix<float64_t> features=SGMatrix<float64_t>(2, 3); SGVector<float64_t>::range_fill_vector(features.matrix, features.num_rows*features.num_cols, 3.0); m_features=new CDenseFeatures<float64_t>(features); SG_REF(m_features); m_parameters->add((CSGObject**)&m_features, "float_features", "Test features"); /* add some parameter mappings for number, here: type changes */ m_parameter_map->put( new const SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 1), new const SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0) ); m_parameter_map->put( new const SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0), new const SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT32, -1) ); /* changes for vector: from int32_t vector to float64_t SG_VECTOR */ m_parameter_map->put( new const SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_FLOAT64, 1), new const SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0) ); /* from normal vector to SG_VECTOR of same type */ m_parameter_map->put( new const SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0), new const SGParamInfo("vector", CT_VECTOR, ST_NONE, PT_INT32, -1) ); /* changes for vector: from int32_t vector to float64_t SG_VECTOR */ m_parameter_map->put( new const SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_FLOAT64, 1), new const SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0) ); /* from normal vector to SG_VECTOR of same type */ m_parameter_map->put( new const SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0), new const SGParamInfo("matrix", CT_MATRIX, ST_NONE, PT_INT32, -1) ); /* name change for sgobject */ m_parameter_map->put( new const SGParamInfo("float_features", CT_SCALAR, ST_NONE, PT_SGOBJECT, 1), new const SGParamInfo("int_features", CT_SCALAR, ST_NONE, PT_SGOBJECT, 0) ); m_parameter_map->finalize_map(); } virtual ~CTestClassFloat() { SG_UNREF(m_features); } float64_t m_number; SGVector<float64_t> m_vector; SGMatrix<float64_t> m_matrix; CDenseFeatures<float64_t>* m_features; virtual const char* get_name() const { return "TestClassFloat"; } }; void test_load_file_parameters() { char filename_tmp[] = "/tmp/file_params_test.XXXXXX"; char* filename = mktemp(filename_tmp); /* create one instance of each class */ CTestClassInt* int_instance=new CTestClassInt(); CTestClassFloat* float_instance=new CTestClassFloat(); CSerializableAsciiFile* file; /* serialize int instance */ file=new CSerializableAsciiFile(filename, 'w'); int_instance->save_serializable(file); file->close(); SG_UNREF(file); /* reopen file for reading */ file=new CSerializableAsciiFile(filename, 'r'); /* build parameter info for parameter of the OTHER instance, start from * version 1 */ const SGParamInfo param_info_number( float_instance->m_parameters->get_parameter(0), 1); const SGParamInfo param_info_vector( float_instance->m_parameters->get_parameter(1), 1); const SGParamInfo param_info_matrix( float_instance->m_parameters->get_parameter(2), 1); const SGParamInfo param_info_sgobject( float_instance->m_parameters->get_parameter(3), 1); int32_t file_version=-1; /* now, here the magic happens, the parameter info of the float instance is * mapped backwards (see its parameter map above) until the parameter * info of the file is found. Then the parameters with the file version * are loaded into memory. This will be used for migration. * Note that only one parameter is in the array here for testing */ DynArray<TParameter*>* file_loaded_number= float_instance->load_file_parameters(¶m_info_number, file_version, file); DynArray<TParameter*>* file_loaded_vector= float_instance->load_file_parameters(¶m_info_vector, file_version, file); DynArray<TParameter*>* file_loaded_matrix= float_instance->load_file_parameters(¶m_info_matrix, file_version, file); DynArray<TParameter*>* file_loaded_sgobject= float_instance->load_file_parameters(¶m_info_sgobject, file_version, file); /* Note that there is only ONE element in array here (old test) */ TParameter* current; /* ensure that its the same as of the instance */ current=file_loaded_number->get_element(0); current->m_delete_data = true; // TODO: This shouldn't be necessary! int32_t value_number=*((int32_t*)current->m_parameter); SG_SPRINT("%i\n", value_number); ASSERT(value_number=int_instance->m_number); /* same for the vector */ current=file_loaded_vector->get_element(0); current->m_delete_data = true; // TODO: This shouldn't be necessary! int32_t* value_vector=*((int32_t**)current->m_parameter); SGVector<int32_t>::display_vector(value_vector, int_instance->m_vector_length); for (index_t i=0; i<int_instance->m_vector_length; ++i) ASSERT(value_vector[i]=int_instance->m_vector[i]); /* and for the matrix */ current=file_loaded_matrix->get_element(0); current->m_delete_data = true; // TODO: This shouldn't be necessary! int32_t* value_matrix=*((int32_t**)current->m_parameter); SGMatrix<int32_t>::display_matrix(value_matrix, int_instance->m_matrix_rows, int_instance->m_matrix_cols); for (index_t i=0; i<int_instance->m_matrix_rows*int_instance->m_matrix_cols; ++i) { ASSERT(value_matrix[i]==int_instance->m_matrix[i]); } /* and for the feature object */ current=file_loaded_sgobject->get_element(0); current->m_delete_data = true; // TODO: This shouldn't be necessary! CDenseFeatures<int32_t>* features= *((CDenseFeatures<int32_t>**)current->m_parameter); SGMatrix<int32_t> feature_matrix_loaded= features->get_feature_matrix(); SGMatrix<int32_t> feature_matrix_original= int_instance->m_features->get_feature_matrix(); SGMatrix<int32_t>::display_matrix(feature_matrix_loaded.matrix, feature_matrix_loaded.num_rows, feature_matrix_loaded.num_cols, "features"); for (index_t i=0; i<int_instance->m_matrix_rows*int_instance->m_matrix_cols; ++i) { ASSERT(feature_matrix_original.matrix[i]== feature_matrix_loaded.matrix[i]); } /* only the TParameter instances have to be deleted, data, data pointer, * and possible length variables are deleted automatically */ for (index_t i=0; i<file_loaded_number->get_num_elements(); ++i) delete file_loaded_number->get_element(i); for (index_t i=0; i<file_loaded_vector->get_num_elements(); ++i) delete file_loaded_vector->get_element(i); for (index_t i=0; i<file_loaded_matrix->get_num_elements(); ++i) delete file_loaded_matrix->get_element(i); for (index_t i=0; i<file_loaded_sgobject->get_num_elements(); ++i) delete file_loaded_sgobject->get_element(i); /* also delete arrays */ delete file_loaded_number; delete file_loaded_vector; delete file_loaded_matrix; delete file_loaded_sgobject; file->close(); SG_UNREF(file); SG_UNREF(int_instance); SG_UNREF(float_instance); unlink(filename); } int main(int argc, char **argv) { init_shogun_with_defaults(); sg_io->set_loglevel(MSG_DEBUG); test_load_file_parameters(); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann * Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/base/Parameter.h> #include <shogun/io/SerializableAsciiFile.h> #include <shogun/base/ParameterMap.h> #include <shogun/features/DenseFeatures.h> #include <unistd.h> using namespace shogun; class CTestClassInt : public CSGObject { public: CTestClassInt() { m_number=10; m_parameters->add(&m_number, "number", "Test number"); m_vector_length=3; m_vector=SG_MALLOC(int32_t, m_vector_length); SGVector<int32_t>::fill_vector(m_vector, m_vector_length, 10); m_parameters->add_vector(&m_vector, &m_vector_length, "vector", "Test vector"); m_matrix_rows=2; m_matrix_cols=3; m_matrix=SG_MALLOC(int32_t, m_matrix_rows*m_matrix_cols); SGVector<int32_t>::range_fill_vector(m_matrix, m_matrix_rows*m_matrix_cols); m_parameters->add_matrix(&m_matrix, &m_matrix_rows, &m_matrix_cols, "matrix", "Test matrix"); SGMatrix<int32_t> features=SGMatrix<int32_t>(2, 3); SGVector<int32_t>::range_fill_vector(features.matrix, features.num_rows*features.num_cols, 3); m_features=new CDenseFeatures<int32_t>(10); m_features->set_feature_matrix(features); m_features->set_combined_feature_weight(5.0); SG_REF(m_features); m_parameters->add((CSGObject**)&m_features, "int_features", "Test features"); } virtual ~CTestClassInt() { SG_FREE(m_vector); SG_FREE(m_matrix); SG_UNREF(m_features); } int32_t m_number; int32_t* m_vector; int32_t m_vector_length; int32_t* m_matrix; int32_t m_matrix_rows; int32_t m_matrix_cols; CDenseFeatures<int32_t>* m_features; virtual const char* get_name() const { return "TestClassInt"; } }; class CTestClassFloat : public CSGObject { public: CTestClassFloat() { m_number=3.2; m_vector=SGVector<float64_t>(10); m_matrix=SGMatrix<float64_t>(2, 3); m_parameters->add(&m_number, "number", "Test number"); m_parameters->add(&m_vector, "vector", "Test vector"); m_parameters->add(&m_matrix, "matrix", "Test matrix"); SGMatrix<int32_t> features=SGMatrix<int32_t>(2, 3); SGVector<int32_t>::range_fill_vector(features.matrix, features.num_rows*features.num_cols, 3); m_features=new CDenseFeatures<int32_t>(features); SG_REF(m_features); m_parameters->add((CSGObject**)&m_features, "float_features", "Test features"); /* add some parameter mappings for number, here: type changes */ m_parameter_map->put( new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 1), new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0) ); m_parameter_map->put( new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0), new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT32, -1) ); /* changes for vector: from int32_t vector to float64_t SG_VECTOR */ m_parameter_map->put( new SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_FLOAT64, 1), new SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0) ); /* from normal vector to SG_VECTOR of same type */ m_parameter_map->put( new SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0), new SGParamInfo("vector", CT_VECTOR, ST_NONE, PT_INT32, -1) ); /* changes for vector: from int32_t vector to float64_t SG_VECTOR */ m_parameter_map->put( new SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_FLOAT64, 1), new SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0) ); /* from normal vector to SG_VECTOR of same type */ m_parameter_map->put( new SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0), new SGParamInfo("matrix", CT_MATRIX, ST_NONE, PT_INT32, -1) ); /* CSGObject mapping is not yet done */ /* name change for sgobject */ m_parameter_map->put( new SGParamInfo("float_features", CT_SCALAR, ST_NONE, PT_SGOBJECT, 1), new SGParamInfo("int_features", CT_SCALAR, ST_NONE, PT_SGOBJECT, 0) ); m_parameter_map->finalize_map(); } virtual ~CTestClassFloat() { SG_UNREF(m_features); } float64_t m_number; SGVector<float64_t> m_vector; SGMatrix<float64_t> m_matrix; /* no type change here */ CDenseFeatures<int32_t>* m_features; virtual const char* get_name() const { return "TestClassFloat"; } virtual TParameter* migrate(DynArray<TParameter*>* param_base, const SGParamInfo* target) { TSGDataType type(target->m_ctype, target->m_stype, target->m_ptype); TParameter* result=NULL; TParameter* to_migrate=NULL; if (*target==SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 1)) { one_to_one_migration_prepare(param_base, target, result, to_migrate); /* here: simply copy (and cast) data because nothing has changed */ *((float64_t*)result->m_parameter)= *((int8_t*)to_migrate->m_parameter); } else if (*target==SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0)) { one_to_one_migration_prepare(param_base, target, result, to_migrate); /* here: simply copy (and cast) data because nothing has changed */ *((int8_t*)result->m_parameter)= *((int32_t*)to_migrate->m_parameter); } else if (*target==SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_FLOAT64, 1)) { one_to_one_migration_prepare(param_base, target, result, to_migrate); /* here: copy data element wise because type changes */ float64_t* array_to=*((float64_t**)result->m_parameter); int32_t* array_from=*((int32_t**)to_migrate->m_parameter); for (index_t i=0; i<*to_migrate->m_datatype.m_length_y; ++i) array_to[i]=array_from[i]; } else if (*target==SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0)) { one_to_one_migration_prepare(param_base, target, result, to_migrate); /* here: copy data complete because its just wrapper type change */ int32_t* array_to=*((int32_t**)result->m_parameter); int32_t* array_from=*((int32_t**)to_migrate->m_parameter); memcpy(array_to, array_from, to_migrate->m_datatype.get_size()); } else if (*target==SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0)) { one_to_one_migration_prepare(param_base, target, result, to_migrate); /* here: copy data complete because its just wrapper type change */ int32_t* array_to=*((int32_t**)result->m_parameter); int32_t* array_from=*((int32_t**)to_migrate->m_parameter); memcpy(array_to, array_from, to_migrate->m_datatype.get_size()); } else if (*target==SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_FLOAT64, 1)) { one_to_one_migration_prepare(param_base, target, result, to_migrate); /* here: copy data element wise because type changes */ float64_t* array_to=*((float64_t**)result->m_parameter); int32_t* array_from=*((int32_t**)to_migrate->m_parameter); for (index_t i=0; i<to_migrate->m_datatype.get_num_elements(); ++i) array_to[i]=array_from[i]; } else if (*target==SGParamInfo("float_features", CT_SCALAR, ST_NONE, PT_SGOBJECT, 1)) { /* specify name change and thats it */ one_to_one_migration_prepare(param_base, target, result, to_migrate, (char*) "int_features"); } if (result) return result; else return CSGObject::migrate(param_base, target); } }; void test_load_file_parameter() { char filename_tmp[] = "map_params_test.XXXXXX"; char* filename=mktemp(filename_tmp); /* create one instance of each class */ CTestClassInt* int_instance=new CTestClassInt(); CTestClassFloat* float_instance=new CTestClassFloat(); CSerializableAsciiFile* file; /* serialize int instance */ file=new CSerializableAsciiFile(filename, 'w'); int_instance->save_serializable(file); file->close(); SG_UNREF(file); /* reopen file for reading */ file=new CSerializableAsciiFile(filename, 'r'); /* versions that are used in this example */ int32_t file_version=-1; int32_t current_version=1; /* load all parameter data, current version is set to 1 here */ DynArray<TParameter*>* params= float_instance->load_all_file_parameters(file_version, current_version, file, ""); /* create an array of param infos from float instance parameters */ DynArray<const SGParamInfo*>* param_infos= new DynArray<const SGParamInfo*>(); for (index_t i=0; i<float_instance->m_parameters->get_num_parameters(); ++i) { param_infos->append_element( new SGParamInfo(float_instance->m_parameters->get_parameter(i), current_version)); } /* here the magic mapping happens */ float_instance->map_parameters(params, file_version, param_infos); /* assert equalness of all parameters * alphabetical order is "float_features", "matrix", "number", "vector" */ TParameter* current=NULL; /* "float_features" (no type change here) */ current=params->get_element(0); SG_SPRINT("checking \"float_features\":\n"); ASSERT(!strcmp(current->m_name, "float_features")); /* cast to simple features */ CDenseFeatures<int32_t>* features= *((CDenseFeatures<int32_t>**)current->m_parameter); SG_SPRINT("checking address (mapped!=original): %p!=%p\n", features, int_instance->m_features); ASSERT((void*)features!=(void*)int_instance->m_features); SG_SPRINT("checking cache size: %d==%d\n", features->get_cache_size(), int_instance->m_features->get_cache_size()); ASSERT(features->get_cache_size()== int_instance->m_features->get_cache_size()); SG_SPRINT("checking combined feature weight: %f==%f\n", features->get_combined_feature_weight(), int_instance->m_features->get_combined_feature_weight()); ASSERT(features->get_combined_feature_weight()== int_instance->m_features->get_combined_feature_weight()); SG_SPRINT("checking feature matrix:\n"); SGMatrix<int32_t> int_matrix=int_instance->m_features->get_feature_matrix(); SGMatrix<int32_t> float_matrix=features->get_feature_matrix(); SG_SPRINT("number of rows: %d==%d\n", int_matrix.num_rows, float_matrix.num_rows); ASSERT(int_matrix.num_rows==float_matrix.num_rows); SG_SPRINT("number of cols: %d==%d\n", int_matrix.num_cols, float_matrix.num_cols); ASSERT(int_matrix.num_cols==float_matrix.num_cols); SGMatrix<int32_t>::display_matrix(float_matrix.matrix, float_matrix.num_rows, float_matrix.num_cols, "mapped"); SGMatrix<int32_t>::display_matrix(int_matrix.matrix, int_matrix.num_rows, int_matrix.num_cols, "original"); for (index_t i=0; i<int_matrix.num_rows*int_matrix.num_cols; ++i) ASSERT(int_matrix.matrix[i]==float_matrix.matrix[i]); /* "matrix" */ current=params->get_element(1); ASSERT(!strcmp(current->m_name, "matrix")); SGMatrix<float64_t> matrix(*(float64_t**)current->m_parameter, *current->m_datatype.m_length_y, *current->m_datatype.m_length_x, false); SG_SPRINT("checking \"matrix:\n"); SG_SPRINT("number of rows: %d==%d\n", *current->m_datatype.m_length_y, int_instance->m_matrix_rows); ASSERT(*current->m_datatype.m_length_y==int_instance->m_matrix_rows); SGMatrix<float64_t>::display_matrix(matrix.matrix, matrix.num_rows, matrix.num_cols, "mapped"); SGMatrix<int32_t>::display_matrix(int_instance->m_matrix, int_instance->m_matrix_rows, int_instance->m_matrix_cols, "original"); for (index_t i=0; i<int_instance->m_matrix_rows*int_instance->m_matrix_cols; ++i) { ASSERT(matrix.matrix[i]==int_instance->m_matrix[i]); } /* "number" */ current=params->get_element(2); ASSERT(!strcmp(current->m_name, "number")); float64_t number=*((float64_t*)current->m_parameter); SG_SPRINT("checking \"number\": %f == %d\n", number, int_instance->m_number); ASSERT(number==int_instance->m_number); /* "vector" */ current=params->get_element(3); ASSERT(!strcmp(current->m_name, "vector")); SGVector<float64_t> vector(*(float64_t**)current->m_parameter, *current->m_datatype.m_length_y, false); SG_SPRINT("checking \"vector:\n"); SG_SPRINT("length: %d==%d\n", *current->m_datatype.m_length_y, int_instance->m_vector_length); ASSERT(*current->m_datatype.m_length_y==int_instance->m_vector_length); SGVector<float64_t>::display_vector(vector.vector, vector.vlen, "mapped"); SGVector<int32_t>::display_vector(int_instance->m_vector, int_instance->m_vector_length, "original"); for (index_t i=0; i<int_instance->m_vector_length; ++i) ASSERT(vector.vector[i]==int_instance->m_vector[i]); /* clean up */ for (index_t i=0; i<param_infos->get_num_elements(); ++i) delete param_infos->get_element(i); delete param_infos; for (index_t i=0; i<params->get_num_elements(); ++i) { /* delete data of TParameters because they were mapped */ params->get_element(i)->m_delete_data=true; delete params->get_element(i); } delete params; file->close(); SG_UNREF(file); SG_UNREF(int_instance); SG_UNREF(float_instance); unlink(filename); } int main(int argc, char **argv) { init_shogun_with_defaults(); test_load_file_parameter(); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann * Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/base/Parameter.h> #include <shogun/io/SerializableAsciiFile.h> #include <shogun/base/ParameterMap.h> #include <unistd.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } class CTestClassOld : public CSGObject { public: CTestClassOld() { m_number_to_drop=10; m_parameters->add(&m_number_to_drop, "m_number_to_drop", ""); m_number_to_keep=10; m_parameters->add(&m_number_to_keep, "m_number_to_keep", ""); } int32_t m_number_to_drop; int32_t m_number_to_keep; virtual const char* get_name() const { return "TestClassOld"; } }; class CTestClassNew : public CSGObject { public: CTestClassNew() { m_number=3; m_parameters->add(&m_number, "m_number", ""); m_number_new=4; m_parameters->add(&m_number_new, "m_number_new", ""); /* change name of to be kept number */ m_parameter_map->put( new SGParamInfo("m_number", CT_SCALAR, ST_NONE, PT_INT32, 1), new SGParamInfo("m_number_to_keep", CT_SCALAR, ST_NONE, PT_INT32, 0) ); /* this parameter is new in this version, mapping from "nowhere" */ m_parameter_map->put( new SGParamInfo("m_number_new", CT_SCALAR, ST_NONE, PT_INT32, 1), new SGParamInfo() ); /* note that dropped parameters need not be considered, just ignored */ /* needed if more than one element */ m_parameter_map->finalize_map(); } int32_t m_number; int32_t m_number_new; virtual const char* get_name() const { return "TestClassNew"; } virtual TParameter* migrate(DynArray<TParameter*>* param_base, const SGParamInfo* target) { TParameter* result=NULL; TParameter* to_migrate=NULL; if (*target==SGParamInfo("m_number", CT_SCALAR, ST_NONE, PT_INT32, 1)) { /* specify name change here (again, was also done in mappings) */ char* old_name=(char*) "m_number_to_keep"; one_to_one_migration_prepare(param_base, target, result, to_migrate, old_name); /* here: simply copy data because nothing has changed */ *((int32_t*)result->m_parameter)= *((int32_t*)to_migrate->m_parameter); } /* note there has to be no case distinction for the new parameter */ if (result) return result; else return CSGObject::migrate(param_base, target); } }; void check_equalness(CTestClassOld* old_instance, CTestClassNew* new_instance) { /* number */ SG_SPRINT("checking \"m_number\":\n"); SG_SPRINT("\t%d==%d\n", old_instance->m_number_to_keep, new_instance->m_number); ASSERT(old_instance->m_number_to_keep==new_instance->m_number); /* new element */ SG_SPRINT("checking \"m_number_new\":\n"); SG_SPRINT("\t%d\n", new_instance->m_number_new); } void test_migration() { char filename_template[] = "migration_dropping_test.XXXXXX"; char* filename = mktemp(filename_template); /* create one instance of each class */ CTestClassOld* old_instance=new CTestClassOld(); CTestClassNew* new_instance=new CTestClassNew(); CSerializableAsciiFile* file; /* serialize int instance, use custom parameter version */ file=new CSerializableAsciiFile(filename, 'w'); old_instance->save_serializable(file, "", 0); file->close(); SG_UNREF(file); /* de-serialize float instance, use custom parameter version */ file=new CSerializableAsciiFile(filename, 'r'); new_instance->load_serializable(file, "", 1); file->close(); SG_UNREF(file); /* assert that content is equal */ check_equalness(old_instance, new_instance); SG_UNREF(old_instance); SG_UNREF(new_instance); SG_UNREF(file); unlink(filename); } int main(int argc, char **argv) { init_shogun(&print_message, &print_message, &print_message); test_migration(); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann * Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/base/Parameter.h> #include <shogun/io/SerializableAsciiFile.h> #include <shogun/base/ParameterMap.h> #include <shogun/lib/SGVector.h> #include <unistd.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } class CTestClassOld : public CSGObject { public: CTestClassOld() { m_number_1=1; m_number_2=2; m_parameters->add(&m_number_1, "m_number_1", ""); m_parameters->add(&m_number_2, "m_number_2", ""); } int32_t m_number_1; int32_t m_number_2; virtual const char* get_name() const { return "TestClassOld"; } }; class CTestClassNew : public CSGObject { public: CTestClassNew() { m_number=0; m_parameters->add(&m_number, "m_number", ""); /* number_1 in old version will become new, merged number */ m_parameter_map->put( new SGParamInfo("m_number", CT_SCALAR, ST_NONE, PT_INT32, 1), new SGParamInfo("m_number_1", CT_SCALAR, ST_NONE, PT_INT32, 0) ); /* Note that here, two mappings for one parameter are added. This means * that m_number both depends on m_number_1 and m_number_2 */ m_parameter_map->put( new SGParamInfo("m_number", CT_SCALAR, ST_NONE, PT_INT32, 1), new SGParamInfo("m_number_2", CT_SCALAR, ST_NONE, PT_INT32, 0) ); /* note that dropped parameters need not be considered, just ignored */ /* needed if more than one element */ m_parameter_map->finalize_map(); } int32_t m_number; int32_t m_number_new; virtual const char* get_name() const { return "TestClassNew"; } virtual TParameter* migrate(DynArray<TParameter*>* param_base, const SGParamInfo* target) { TParameter* result=NULL; if (*target==SGParamInfo("m_number", CT_SCALAR, ST_NONE, PT_INT32, 1)) { /* one to one migration may not be used here because two parameters * are merged into one parameter. Here the new parameter will * contain the sum of the two old ones. */ /* generate type of target structure */ TSGDataType type(target->m_ctype, target->m_stype, target->m_ptype); /* find elements that are needed for migration, in this case the * two numbers of the base */ char* name_1=(char*) "m_number_1"; char* name_2=(char*) "m_number_2"; /* dummy elements for searching */ TParameter* t_1=new TParameter(&type, NULL, name_1, ""); TParameter* t_2=new TParameter(&type, NULL, name_2, ""); index_t i_1=CMath::binary_search(param_base->get_array(), param_base->get_num_elements(), t_1); index_t i_2=CMath::binary_search(param_base->get_array(), param_base->get_num_elements(), t_2); delete t_1; delete t_2; /* gather search results and tell them that they are to be deleted * because they will be replaced */ ASSERT(i_1>=0 && i_2>=0); TParameter* to_migrate_1=param_base->get_element(i_1); TParameter* to_migrate_2=param_base->get_element(i_2); to_migrate_1->m_delete_data=true; to_migrate_2->m_delete_data=true; /* create result structure and allocate data for it */ result=new TParameter(&type, NULL, target->m_name, "New description"); /* scalar value has length one */ result->allocate_data_from_scratch(1, 1); /* merged element contains sum of both to be merged elements */ *((int32_t*)result->m_parameter)= *((int32_t*)to_migrate_1->m_parameter)+ *((int32_t*)to_migrate_2->m_parameter); } if (result) return result; else return CSGObject::migrate(param_base, target); } }; void test_migration() { char filename_tmp[] = "migration_multiple_dep_test.XXXXXX"; char* filename=mktemp(filename_tmp); /* create one instance of each class */ CTestClassOld* old_instance=new CTestClassOld(); CTestClassNew* new_instance=new CTestClassNew(); CSerializableAsciiFile* file; /* serialize int instance, use custom parameter version */ file=new CSerializableAsciiFile(filename, 'w'); old_instance->save_serializable(file, "", 0); file->close(); SG_UNREF(file); /* de-serialize float instance, use custom parameter version */ file=new CSerializableAsciiFile(filename, 'r'); new_instance->load_serializable(file, "", 1); file->close(); SG_UNREF(file); /* check that merged number is sum old to be merged ones */ SG_SPRINT("checking \"m_number\":\n"); SG_SPRINT("\t%d==%d+%d\n", new_instance->m_number, old_instance->m_number_1, old_instance->m_number_2); ASSERT(new_instance->m_number==old_instance->m_number_1+ old_instance->m_number_2); SG_UNREF(old_instance); SG_UNREF(new_instance); SG_UNREF(file); unlink(filename); } int main(int argc, char **argv) { init_shogun(&print_message, &print_message, &print_message); /* this is a more complex example, where a parameter is based on two * old parameter */ test_migration(); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann * Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/base/Parameter.h> #include <shogun/io/SerializableAsciiFile.h> #include <shogun/base/ParameterMap.h> #include <unistd.h> using namespace shogun; class CTestClassOld : public CSGObject { public: CTestClassOld() { } virtual const char* get_name() const { return "TestClassOld"; } }; class CTestClassNew : public CSGObject { public: CTestClassNew() { m_number=3; m_parameters->add(&m_number, "m_number", ""); /* this parameter is new in this version, mapping from "nowhere" */ m_parameter_map->put( new SGParamInfo("m_number", CT_SCALAR, ST_NONE, PT_INT32, 1), new SGParamInfo() ); /* note that dropped parameters need not be considered, just ignored */ /* needed if more than one element */ m_parameter_map->finalize_map(); } int32_t m_number; virtual const char* get_name() const { return "TestClassNew"; } }; void test() { char filename_tmp[] = "migration_buggy_test.XXXXXX"; char* filename=mktemp(filename_tmp); /* create one instance of each class */ CTestClassOld* old_instance=new CTestClassOld(); CTestClassNew* new_instance=new CTestClassNew(); CSerializableAsciiFile* file; /* serialize int instance, use custom parameter version */ file=new CSerializableAsciiFile(filename, 'w'); old_instance->save_serializable(file, "", 0); file->close(); SG_UNREF(file); /* de-serialize float instance, use custom parameter version 2 which means * that the class is already one step further and the parameter which was * added in version 1 has to be migrated (which is done automatically) */ // change this version to two once it works! file=new CSerializableAsciiFile(filename, 'r'); new_instance->load_serializable(file, "", 1); file->close(); SG_UNREF(file); SG_UNREF(old_instance); SG_UNREF(new_instance); SG_UNREF(file); unlink(filename); } int main(int argc, char **argv) { init_shogun_with_defaults(); test(); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann * Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/base/Parameter.h> #include <shogun/io/SerializableAsciiFile.h> #include <shogun/io/SGIO.h> #include <shogun/base/ParameterMap.h> #include <shogun/features/DenseFeatures.h> #include <unistd.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } class CTestClassInt : public CSGObject { public: CTestClassInt() { m_number=10; m_parameters->add(&m_number, "number", "Test number"); m_vector_length=3; m_vector=SG_MALLOC(int32_t, m_vector_length); SGVector<int32_t>::fill_vector(m_vector, m_vector_length, 10); m_parameters->add_vector(&m_vector, &m_vector_length, "vector", "Test vector"); m_matrix_rows=2; m_matrix_cols=3; m_matrix=SG_MALLOC(int32_t, m_matrix_rows*m_matrix_cols); SGVector<int32_t>::range_fill_vector(m_matrix, m_matrix_rows*m_matrix_cols); m_parameters->add_matrix(&m_matrix, &m_matrix_rows, &m_matrix_cols, "matrix", "Test matrix"); SGMatrix<int32_t> features=SGMatrix<int32_t>(2, 3); SGVector<int32_t>::range_fill_vector(features.matrix, features.num_rows*features.num_cols, 3); m_features=new CDenseFeatures<int32_t>(10); m_features->set_feature_matrix(features); m_features->set_combined_feature_weight(5.0); SG_REF(m_features); m_parameters->add((CSGObject**)&m_features, "int_features", "Test features"); } virtual ~CTestClassInt() { SG_FREE(m_vector); SG_FREE(m_matrix); SG_UNREF(m_features); } int32_t m_number; int32_t* m_vector; int32_t m_vector_length; int32_t* m_matrix; int32_t m_matrix_rows; int32_t m_matrix_cols; CDenseFeatures<int32_t>* m_features; virtual const char* get_name() const { return "TestClassInt"; } }; class CTestClassFloat : public CSGObject { public: CTestClassFloat() { m_number=3.2; m_vector=SGVector<float64_t>(10); SGVector<float64_t>::fill_vector(m_vector.vector, m_vector.vlen, 0.0); m_matrix=SGMatrix<float64_t>(3, 3); SGVector<float64_t>::range_fill_vector(m_matrix.matrix, m_matrix.num_rows*m_matrix.num_cols, 0.0); m_parameters->add(&m_number, "number", "Test number"); m_parameters->add(&m_vector, "vector", "Test vector"); m_parameters->add(&m_matrix, "matrix", "Test matrix"); SGMatrix<int32_t> features=SGMatrix<int32_t>(2, 3); SGVector<int32_t>::range_fill_vector(features.matrix, features.num_rows*features.num_cols, 0); m_features=new CDenseFeatures<int32_t>(features); SG_REF(m_features); m_parameters->add((CSGObject**)&m_features, "float_features", "Test features"); /* add some parameter mappings for number, here: type changes */ m_parameter_map->put( new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 1), new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0) ); m_parameter_map->put( new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0), new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT32, -1) ); /* changes for vector: from int32_t vector to float64_t SG_VECTOR */ m_parameter_map->put( new SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_FLOAT64, 1), new SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0) ); /* from normal vector to SG_VECTOR of same type */ m_parameter_map->put( new SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0), new SGParamInfo("vector", CT_VECTOR, ST_NONE, PT_INT32, -1) ); /* changes for vector: from int32_t vector to float64_t SG_VECTOR */ m_parameter_map->put( new SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_FLOAT64, 1), new SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0) ); /* from normal vector to SG_VECTOR of same type */ m_parameter_map->put( new SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0), new SGParamInfo("matrix", CT_MATRIX, ST_NONE, PT_INT32, -1) ); /* CSGObject mapping is not yet done */ /* name change for sgobject */ m_parameter_map->put( new SGParamInfo("float_features", CT_SCALAR, ST_NONE, PT_SGOBJECT, 1), new SGParamInfo("int_features", CT_SCALAR, ST_NONE, PT_SGOBJECT, 0) ); m_parameter_map->finalize_map(); } virtual ~CTestClassFloat() { SG_UNREF(m_features); } float64_t m_number; SGVector<float64_t> m_vector; SGMatrix<float64_t> m_matrix; /* no type change here */ CDenseFeatures<int32_t>* m_features; virtual const char* get_name() const { return "TestClassFloat"; } virtual TParameter* migrate(DynArray<TParameter*>* param_base, const SGParamInfo* target) { TParameter* result=NULL; TParameter* to_migrate=NULL; if (*target==SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 1)) { one_to_one_migration_prepare(param_base, target, result, to_migrate); /* here: simply copy (and cast) data because nothing has changed */ *((float64_t*)result->m_parameter)= *((int8_t*)to_migrate->m_parameter); } else if (*target==SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0)) { one_to_one_migration_prepare(param_base, target, result, to_migrate); /* here: simply copy (and cast) data because nothing has changed */ *((int8_t*)result->m_parameter)= *((int32_t*)to_migrate->m_parameter); } else if (*target==SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_FLOAT64, 1)) { one_to_one_migration_prepare(param_base, target, result, to_migrate); /* here: copy data element wise because type changes */ float64_t* array_to=*((float64_t**)result->m_parameter); int32_t* array_from=*((int32_t**)to_migrate->m_parameter); for (index_t i=0; i<*to_migrate->m_datatype.m_length_y; ++i) array_to[i]=array_from[i]; } else if (*target==SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0)) { one_to_one_migration_prepare(param_base, target, result, to_migrate); /* here: copy data complete because its just wrapper type change */ int32_t* array_to=*((int32_t**)result->m_parameter); int32_t* array_from=*((int32_t**)to_migrate->m_parameter); memcpy(array_to, array_from, to_migrate->m_datatype.get_size()); } else if (*target==SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0)) { one_to_one_migration_prepare(param_base, target, result, to_migrate); /* here: copy data complete because its just wrapper type change */ int32_t* array_to=*((int32_t**)result->m_parameter); int32_t* array_from=*((int32_t**)to_migrate->m_parameter); memcpy(array_to, array_from, to_migrate->m_datatype.get_size()); } else if (*target==SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_FLOAT64, 1)) { one_to_one_migration_prepare(param_base, target, result, to_migrate); /* here: copy data element wise because type changes */ float64_t* array_to=*((float64_t**)result->m_parameter); int32_t* array_from=*((int32_t**)to_migrate->m_parameter); for (index_t i=0; i<to_migrate->m_datatype.get_num_elements(); ++i) array_to[i]=array_from[i]; } else if (*target==SGParamInfo("float_features", CT_SCALAR, ST_NONE, PT_SGOBJECT, 1)) { /* specify name change and thats it */ char* new_name=(char*) "int_features"; one_to_one_migration_prepare(param_base, target, result, to_migrate, new_name); } if (result) return result; else return CSGObject::migrate(param_base, target); } }; void check_equalness(CTestClassInt* int_instance, CTestClassFloat* float_instance) { /* number */ SG_SPRINT("checking \"number\":\n"); SG_SPRINT("\t%d==%f\n", int_instance->m_number, float_instance->m_number); ASSERT(int_instance->m_number==float_instance->m_number); /* "vector" */ SG_SPRINT("checking \"vector\":\n"); SG_SPRINT("\tlength: %d==%d\n", int_instance->m_vector_length, float_instance->m_vector.vlen); ASSERT(int_instance->m_vector_length==float_instance->m_vector.vlen); SGVector<int32_t>::display_vector(int_instance->m_vector, int_instance->m_vector_length, "oiginal", "\t"); SGVector<float64_t>::display_vector(float_instance->m_vector.vector, float_instance->m_vector.vlen, "migrated", "\t"); for (index_t i=0; i<int_instance->m_vector_length; ++i) ASSERT(int_instance->m_vector[i]==float_instance->m_vector.vector[i]); /* "matrix" */ SG_SPRINT("checking \"matrix\":\n"); SG_SPRINT("\trows: %d==%d\n", int_instance->m_matrix_rows, float_instance->m_matrix.num_rows); ASSERT(int_instance->m_matrix_rows==float_instance->m_matrix.num_rows); SG_SPRINT("\tcols: %d==%d\n", int_instance->m_matrix_cols, float_instance->m_matrix.num_cols); ASSERT(int_instance->m_matrix_cols==float_instance->m_matrix.num_cols); SGMatrix<int32_t>::display_matrix(int_instance->m_matrix, int_instance->m_matrix_rows, int_instance->m_matrix_cols, "original", "\t"); SGMatrix<float64_t>::display_matrix(float_instance->m_matrix.matrix, float_instance->m_matrix.num_rows, float_instance->m_matrix.num_cols, "migrated", "\t"); for (index_t i=0; i<int_instance->m_matrix_rows*int_instance->m_matrix_cols; ++i) { ASSERT(int_instance->m_matrix[i]==float_instance->m_matrix.matrix[i]); } /* "features" */ SG_SPRINT("checking \"features\":\n"); SG_SPRINT("\tchecking \"feature matrix\":\n"); SGMatrix<int32_t> original_matrix= int_instance->m_features->get_feature_matrix(); SGMatrix<int32_t> migrated_matrix= float_instance->m_features->get_feature_matrix(); SG_SPRINT("\t\trows: %d==%d\n", original_matrix.num_rows, migrated_matrix.num_rows); ASSERT(original_matrix.num_rows==migrated_matrix.num_rows); SG_SPRINT("\t\tcols: %d==%d\n", original_matrix.num_cols, migrated_matrix.num_cols); ASSERT(original_matrix.num_cols==migrated_matrix.num_cols); SGMatrix<int32_t>::display_matrix(original_matrix.matrix, original_matrix.num_rows, original_matrix.num_cols, "original", "\t\t"); SGMatrix<int32_t>::display_matrix(migrated_matrix.matrix, migrated_matrix.num_rows, migrated_matrix.num_cols, "migrated", "\t\t"); for (index_t i=0; i<int_instance->m_matrix_rows*int_instance->m_matrix_cols; ++i) { ASSERT(original_matrix.matrix[i]==migrated_matrix.matrix[i]); } } void test_migration() { char filename_tmp[] = "migration_type_conv_test.XXXXXX"; char* filename=mktemp(filename_tmp); /* create one instance of each class */ CTestClassInt* int_instance=new CTestClassInt(); CTestClassFloat* float_instance=new CTestClassFloat(); CSerializableAsciiFile* file; /* serialize int instance, use custom parameter version */ file=new CSerializableAsciiFile(filename, 'w'); int_instance->save_serializable(file, "", -1); file->close(); SG_UNREF(file); /* now the magic happens, the float instance is derserialized from file. * Note that the parameter types are different. they will all be mapped. * See migration methods. Everything is just converted, value is kept. * The float instance has different initial values for all members, however, * after de-serializing it from the int_instance file, the values should be * the same * * The parameter mappings are chosen in such way that CTestClassInt could * be seen as an old version of CTestClassFloat. */ /* de-serialize float instance, use custom parameter version * Note that a warning will appear, complaining that there is no parameter * version in file. This is not true, the version is -1, which is used here * as custom version. Normally numbers >=0 are used. */ file=new CSerializableAsciiFile(filename, 'r'); // mute the warning so we don't have a false positive on the buildbot float_instance->io->set_loglevel(MSG_ERROR); float_instance->load_serializable(file, "", 1); float_instance->io->set_loglevel(MSG_WARN); file->close(); SG_UNREF(file); /* assert that content is equal */ check_equalness(int_instance, float_instance); SG_UNREF(int_instance); SG_UNREF(float_instance); SG_UNREF(file); unlink(filename); } int main(int argc, char **argv) { init_shogun(&print_message, &print_message, &print_message); test_migration(); exit_shogun(); return 0; }
/* * 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) 2011 Heiko Strathmann * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/base/Parameter.h> #include <shogun/base/ParameterMap.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } void test_mapping_1() { ParameterMap* map=new ParameterMap(); map->put( new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 2), new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT32, 1) ); map->put( new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT32, 1), new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 0) ); map->put( new SGParamInfo("number_2", CT_SCALAR, ST_NONE, PT_INT32, 1), new SGParamInfo("number_to_keep", CT_SCALAR, ST_NONE, PT_INT32, 0) ); /* finalizing the map is needed before accessing it */ SG_SPRINT("\n\before finalization:\n"); map->finalize_map(); SG_SPRINT("\n\nafter finalization:\n"); map->print_map(); SG_SPRINT("\n"); /* get some elements from map, one/two ARE in map, three and four are NOT */ DynArray<SGParamInfo*> dummies; dummies.append_element(new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT32, 1)); dummies.append_element(new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 2)); dummies.append_element(new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT32, 2)); dummies.append_element(new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 0)); dummies.append_element(new SGParamInfo("number_2", CT_SCALAR, ST_NONE, PT_INT32, 1)); for (index_t i=0; i<dummies.get_num_elements(); ++i) { SGParamInfo* current=dummies.get_element(i); char* s=current->to_string(); SG_SPRINT("searching for: %s\n", s); SG_FREE(s); DynArray<const SGParamInfo*>* result=map->get(current); if (result) { for (index_t j=0; j<result->get_num_elements(); ++j) { s=result->get_element(j)->to_string(); SG_SPRINT("found: %s\n\n", s); SG_FREE(s); } } else SG_SPRINT("nothing found\n\n"); delete current; } delete map; } void print_value(const SGParamInfo* key, ParameterMap* map) { DynArray<const SGParamInfo*>* current=map->get(key); key->print_param_info(); SG_SPRINT("value: "); if (current) { for (index_t i=0; i<current->get_num_elements(); ++i) current->get_element(i)->print_param_info("\t"); } else SG_SPRINT("no elements\n"); SG_SPRINT("\n"); } void test_mapping_2() { ParameterMap* map=new ParameterMap(); EContainerType cfrom=CT_SCALAR; EContainerType cto=CT_MATRIX; EStructType sfrom=ST_NONE; EStructType sto=ST_STRING; EPrimitiveType pfrom=PT_BOOL; EPrimitiveType pto=PT_SGOBJECT; map->put(new SGParamInfo("1", cfrom, sfrom, pfrom, 2), new SGParamInfo("eins", cto, sto, pto, 1)); map->put(new SGParamInfo("2", cfrom, sfrom, pfrom, 2), new SGParamInfo("zwei", cto, sto, pto, 1)); map->put(new SGParamInfo("3", cfrom, sfrom, pfrom, 4), new SGParamInfo("drei", cto, sto, pto, 3)); map->put(new SGParamInfo("4", cfrom, sfrom, pfrom, 4), new SGParamInfo("vier", cto, sto, pto, 3)); map->finalize_map(); SG_SPRINT("\n\nafter finalization:\n"); map->print_map(); const SGParamInfo* key; SG_SPRINT("\n\ntesting map\n"); key=new SGParamInfo("1", cfrom, sfrom, pfrom, 1); print_value(key, map); delete key; key=new SGParamInfo("2", cfrom, sfrom, pfrom, 2); print_value(key, map); delete key; key=new SGParamInfo("2", cto, sfrom, pfrom, 2); print_value(key, map); delete key; key=new SGParamInfo("2", cfrom, sto, pfrom, 2); print_value(key, map); delete key; key=new SGParamInfo("2", cfrom, sfrom, pto, 2); print_value(key, map); delete key; key=new SGParamInfo("5", cfrom, sfrom, pfrom, 4); print_value(key, map); delete key; delete map; } void test_mapping_0() { /* test multiple values per key */ ParameterMap* map=new ParameterMap(); EContainerType cfrom=CT_SCALAR; EContainerType cto=CT_MATRIX; EStructType sfrom=ST_NONE; EStructType sto=ST_STRING; EPrimitiveType pfrom=PT_BOOL; EPrimitiveType pto=PT_SGOBJECT; /* 3 equal keys */ map->put(new SGParamInfo("1", cfrom, sfrom, pfrom, 2), new SGParamInfo("eins a", cto, sto, pto, 1)); map->put(new SGParamInfo("1", cfrom, sfrom, pfrom, 2), new SGParamInfo("eins b", cto, sto, pto, 1)); map->put(new SGParamInfo("1", cfrom, sfrom, pfrom, 2), new SGParamInfo("eins c", cto, sto, pto, 1)); /* 2 equal keys */ map->put(new SGParamInfo("2", cfrom, sfrom, pfrom, 2), new SGParamInfo("zwei a", cto, sto, pto, 1)); map->put(new SGParamInfo("2", cfrom, sfrom, pfrom, 2), new SGParamInfo("zwei b", cto, sto, pto, 1)); map->finalize_map(); SG_SPRINT("printing finalized map\n"); map->print_map(); /* assert that all is there */ DynArray<const SGParamInfo*>* result; bool found; /* key 0 */ result=map->get(SGParamInfo("1", cfrom, sfrom, pfrom, 2)); ASSERT(result); /* first value element */ found=false; for (index_t i=0; i<result->get_num_elements(); ++i) { if (*result->get_element(i) == SGParamInfo("eins a", cto, sto, pto, 1)) found=true; } ASSERT(found); /* second value element */ found=false; for (index_t i=0; i<result->get_num_elements(); ++i) { if (*result->get_element(i) == SGParamInfo("eins b", cto, sto, pto, 1)) found=true; } ASSERT(found); /* third value element */ found=false; for (index_t i=0; i<result->get_num_elements(); ++i) { if (*result->get_element(i) == SGParamInfo("eins c", cto, sto, pto, 1)) found=true; } ASSERT(found); /* key 1 */ result=map->get(SGParamInfo("2", cfrom, sfrom, pfrom, 2)); ASSERT(result); /* first value element */ found=false; for (index_t i=0; i<result->get_num_elements(); ++i) { if (*result->get_element(i) == SGParamInfo("zwei a", cto, sto, pto, 1)) found=true; } ASSERT(found); /* second value element */ found=false; for (index_t i=0; i<result->get_num_elements(); ++i) { if (*result->get_element(i) == SGParamInfo("zwei b", cto, sto, pto, 1)) found=true; } ASSERT(found); delete map; } int main(int argc, char **argv) { init_shogun(&print_message, &print_message, &print_message); test_mapping_0(); test_mapping_1(); test_mapping_2(); exit_shogun(); return 0; }
#include <shogun/base/init.h> using namespace shogun; int main(int argc, char** argv) { init_shogun_with_defaults(); exit_shogun(); return 0; }
/* * 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) 2013 Viktor Gal */ #include <shogun/base/init.h> #include <shogun/machine/BaggingMachine.h> #include <shogun/classifier/svm/LibLinear.h> #include <shogun/ensemble/MajorityVote.h> #include <shogun/labels/BinaryLabels.h> #include <shogun/features/DenseFeatures.h> #include <shogun/features/streaming/generators/MeanShiftDataGenerator.h> #include <shogun/evaluation/ContingencyTableEvaluation.h> using namespace shogun; int main(int argc, char** argv) { init_shogun_with_defaults(); float64_t difference = 2.5; index_t dim = 2; index_t num_neg = 20; index_t num_pos = 20; int32_t num_bags = 5; int32_t bag_size = 25; /* streaming data generator for mean shift distributions */ CMeanShiftDataGenerator* gen_n = new CMeanShiftDataGenerator(0, dim); CMeanShiftDataGenerator* gen_p = new CMeanShiftDataGenerator(difference, dim); CFeatures* neg = gen_n->get_streamed_features(num_pos); CFeatures* pos = gen_p->get_streamed_features(num_neg); CDenseFeatures<float64_t>* train_feats = CDenseFeatures<float64_t>::obtain_from_generic(neg->create_merged_copy(pos)); SGVector<float64_t> tl(num_neg+num_pos); tl.set_const(1); for (index_t i = 0; i < num_neg; ++i) tl[i] = -1; CBinaryLabels* train_labels = new CBinaryLabels(tl); CBaggingMachine* bm = new CBaggingMachine(train_feats, train_labels); CLibLinear* ll = new CLibLinear(); ll->set_bias_enabled(true); CMajorityVote* mv = new CMajorityVote(); bm->set_num_bags(num_bags); bm->set_bag_size(bag_size); bm->set_machine(ll); bm->set_combination_rule(mv); bm->train(); CBinaryLabels* pred_bagging = bm->apply_binary(train_feats); CContingencyTableEvaluation* eval = new CContingencyTableEvaluation(); pred_bagging->get_int_labels().display_vector(); float64_t bag_accuracy = eval->evaluate(pred_bagging, train_labels); float64_t oob_error = bm->get_oob_error(eval); CLibLinear* libLin = new CLibLinear(2.0, train_feats, train_labels); libLin->set_bias_enabled(true); libLin->train(); CBinaryLabels* pred_liblin = libLin->apply_binary(train_feats); pred_liblin->get_int_labels().display_vector(); float64_t liblin_accuracy = eval->evaluate(pred_liblin, train_labels); SG_SPRINT("bagging accuracy: %f (OOB-error: %f)\nLibLinear accuracy: %f\n", bag_accuracy, oob_error, liblin_accuracy); SG_UNREF(bm); SG_UNREF(pos); SG_UNREF(neg); SG_UNREF(eval); exit_shogun(); return 0; }
#include <shogun/labels/RegressionLabels.h> #include <shogun/features/DenseFeatures.h> #include <shogun/classifier/FeatureBlockLogisticRegression.h> #include <shogun/lib/IndexBlock.h> #include <shogun/lib/IndexBlockTree.h> #include <shogun/lib/IndexBlockGroup.h> #include <shogun/base/init.h> #include <shogun/lib/common.h> #include <shogun/io/SGIO.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } int main(int argc, char** argv) { init_shogun_with_defaults(); // create some data SGMatrix<float64_t> matrix(4,4); for (int32_t i=0; i<4*4; i++) matrix.matrix[i]=i; CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix); // create three labels CBinaryLabels* labels=new CBinaryLabels(4); labels->set_label(0, -1); labels->set_label(1, +1); labels->set_label(2, -1); labels->set_label(3, +1); CIndexBlock* first_block = new CIndexBlock(0,2); CIndexBlock* second_block = new CIndexBlock(2,4); CIndexBlockGroup* block_group = new CIndexBlockGroup(); block_group->add_block(first_block); block_group->add_block(second_block); CFeatureBlockLogisticRegression* regressor = new CFeatureBlockLogisticRegression(0.5,features,labels,block_group); regressor->train(); regressor->get_w().display_vector(); CIndexBlock* root_block = new CIndexBlock(0,4); root_block->add_sub_block(first_block); root_block->add_sub_block(second_block); CIndexBlockTree* block_tree = new CIndexBlockTree(root_block); regressor->set_feature_relation(block_tree); regressor->train(); regressor->get_w().display_vector(); SG_UNREF(regressor); exit_shogun(); return 0; }
/* * 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) 2013 Roman Votyakov */ #include <shogun/lib/config.h> // Eigen3 is required for working with this example #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/labels/BinaryLabels.h> #include <shogun/features/DenseFeatures.h> #include <shogun/kernel/GaussianKernel.h> #include <shogun/machine/gp/LaplacianInferenceMethod.h> #include <shogun/machine/gp/EPInferenceMethod.h> #include <shogun/machine/gp/ZeroMean.h> #include <shogun/machine/gp/LogitLikelihood.h> #include <shogun/machine/gp/ProbitLikelihood.h> #include <shogun/classifier/GaussianProcessBinaryClassification.h> #include <shogun/io/CSVFile.h> using namespace shogun; // files with training data const char* fname_feat_train="../data/fm_train_real.dat"; const char* fname_label_train="../data/label_train_twoclass.dat"; // file with testing data const char* fname_feat_test="../data/fm_test_real.dat"; int main(int argc, char** argv) { init_shogun_with_defaults(); // trainig data SGMatrix<float64_t> X_train; SGVector<float64_t> y_train; // load training features from file CCSVFile* file_feat_train=new CCSVFile(fname_feat_train); X_train.load(file_feat_train); SG_UNREF(file_feat_train); // load training labels from file CCSVFile* file_label_train=new CCSVFile(fname_label_train); y_train.load(file_label_train); SG_UNREF(file_label_train); // testing features SGMatrix<float64_t> X_test; // load testing features from file CCSVFile* file_feat_test=new CCSVFile(fname_feat_test); X_test.load(file_feat_test); SG_UNREF(file_feat_test); // convert training and testing data into shogun representation CDenseFeatures<float64_t>* feat_train=new CDenseFeatures<float64_t>(X_train); CBinaryLabels* lab_train=new CBinaryLabels(y_train); CDenseFeatures<float64_t>* feat_test=new CDenseFeatures<float64_t>(X_test); SG_REF(feat_test); // create Gaussian kernel with width = 2.0 CGaussianKernel* kernel=new CGaussianKernel(10, 2.0); // create zero mean function CZeroMean* mean=new CZeroMean(); // you can easily switch between probit and logit likelihood models // by uncommenting/commenting the following lines: // create probit likelihood model // CProbitLikelihood* lik=new CProbitLikelihood(); // create logit likelihood model CLogitLikelihood* lik=new CLogitLikelihood(); // you can easily switch between Laplace and EP approximation by // uncommenting/commenting the following lines: // specify Laplace approximation inference method // CLaplacianInferenceMethod* inf=new CLaplacianInferenceMethod(kernel, // feat_train, mean, lab_train, lik); // specify EP approximation inference method CEPInferenceMethod* inf=new CEPInferenceMethod(kernel, feat_train, mean, lab_train, lik); // create and train GP classifier, which uses Laplace approximation CGaussianProcessBinaryClassification* gpc=new CGaussianProcessBinaryClassification(inf); gpc->train(); // apply binary classification to the test data and get -1/+1 // labels of the predictions CBinaryLabels* predictions=gpc->apply_binary(feat_test); predictions->get_labels().display_vector("predictions"); // get probabilities p(y*=1|x*) for each testing feature x* SGVector<float64_t> p_test=gpc->get_probabilities(feat_test); p_test.display_vector("predictive probability"); // get predictive mean SGVector<float64_t> mu_test=gpc->get_mean_vector(feat_test); mu_test.display_vector("predictive mean"); // get predictive variance SGVector<float64_t> s2_test=gpc->get_variance_vector(feat_test); s2_test.display_vector("predictive variance"); // free up memory SG_UNREF(gpc); SG_UNREF(predictions); SG_UNREF(feat_test); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif /* HAVE_EIGEN3 */
#include <shogun/labels/MulticlassLabels.h> #include <shogun/features/DenseFeatures.h> #include <shogun/multiclass/GaussianNaiveBayes.h> #include <shogun/base/init.h> #include <shogun/lib/common.h> #include <shogun/io/SGIO.h> using namespace shogun; int main(int argc, char** argv) { init_shogun_with_defaults(); // create some data SGMatrix<float64_t> matrix(2,3); for (int32_t i=0; i<6; i++) matrix.matrix[i]=i; // create three 2-dimensional vectors // shogun will now own the matrix created CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix); // create three labels CMulticlassLabels* labels=new CMulticlassLabels(3); labels->set_label(0, 0); labels->set_label(1, +1); labels->set_label(2, +2); CGaussianNaiveBayes* ci = new CGaussianNaiveBayes(features,labels); ci->train(); // classify on training examples for (int32_t i=0; i<3; i++) SG_SPRINT("output[%d]=%f\n", i, ci->apply_one(i)); // free up memory SG_UNREF(ci); exit_shogun(); return 0; }
/* * 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) 2013 Fernando J. Iglesias García */ #include <shogun/base/init.h> #include <shogun/distance/EuclideanDistance.h> #include <shogun/features/DataGenerator.h> #include <shogun/features/DenseFeatures.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/multiclass/KNN.h> using namespace shogun; #define NUM 10 #define DIMS 2 #define CLASSES 4 #define k 3 int main(int, char*[]) { init_shogun_with_defaults(); #ifdef HAVE_LAPACK /* because of CDataGenerator::generate_gaussians */ // Labels and features containers SGVector<float64_t> lab(CLASSES*NUM); SGMatrix<float64_t> feat(DIMS, CLASSES*NUM); // Random generation of features feat = CDataGenerator::generate_gaussians(NUM,CLASSES,DIMS); // Labels for (int32_t i = 0; i < CLASSES; ++i) for (int32_t j = 0; j < NUM; ++j) lab[i*NUM + j] = i; // Create train labels CMulticlassLabels* labels = new CMulticlassLabels(lab); // Create train features CDenseFeatures<float64_t>* features = new CDenseFeatures<float64_t>(feat); // Create KNN classifier CKNN* knn = new CKNN(k, new CEuclideanDistance(features, features), labels); // Train classifier knn->train(); // Apply classifier CMulticlassLabels* output = CLabelsFactory::to_multiclass( knn->apply() ); SGMatrix<int32_t> multiple_k_output = knn->classify_for_multiple_k(); // Free memory SG_UNREF(knn) SG_UNREF(output) #endif /* HAVE_LAPACK */ exit_shogun(); return 0; }
/* * 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) 2013 Heiko Strathmann and others */ #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 test() { index_t num_vec=10; index_t num_feat=3; index_t num_class=num_feat; // to make data easy float64_t distance=15; // create some linearly seperable data SGMatrix<float64_t> matrix(num_class, num_vec); SGMatrix<float64_t> matrix_test(num_class, num_vec); CMulticlassLabels* labels=new CMulticlassLabels(num_vec); CMulticlassLabels* labels_test=new CMulticlassLabels(num_vec); for (index_t i=0; i<num_vec; ++i) { index_t label=i%num_class; for (index_t j=0; j<num_feat; ++j) { matrix(j,i)=CMath::randn_double(); matrix_test(j,i)=CMath::randn_double(); labels->set_label(i, label); labels_test->set_label(i, label); } /* make sure data is linearly seperable per class */ matrix(label,i)+=distance; matrix_test(label,i)+=distance; } matrix.display_matrix("matrix"); labels->get_int_labels().display_vector("labels"); // shogun will now own the matrix created CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(matrix); CDenseFeatures<float64_t>* features_test= new CDenseFeatures<float64_t>(matrix_test); // create three labels 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*)svm->apply(); output->get_labels().display_vector("batch output"); /* assert that batch apply and apply(index_t) give same result */ SGVector<float64_t> single_outputs(output->get_num_labels()); for (index_t i=0; i<output->get_num_labels(); ++i) single_outputs[i]=svm->apply_one(i); single_outputs.display_vector("single_outputs"); for (index_t i=0; i<output->get_num_labels(); ++i) ASSERT(output->get_label(i)==single_outputs[i]); CMulticlassLabels* output_test= (CMulticlassLabels*)svm->apply(features_test); labels_test->get_labels().display_vector("labels_test"); output_test->get_labels().display_vector("output_test"); for (index_t i=0; i<output->get_num_labels(); ++i) ASSERT(labels_test->get_label(i)==output_test->get_label(i)); // free up memory SG_UNREF(output); SG_UNREF(labels_test); SG_UNREF(output_test); SG_UNREF(svm); } int main(int argc, char** argv) { init_shogun_with_defaults(); test(); exit_shogun(); return 0; }
#include <shogun/labels/LatentLabels.h> #include <shogun/features/LatentFeatures.h> #include <shogun/latent/LatentSVM.h> #include <shogun/features/DenseFeatures.h> #include <shogun/base/init.h> #include <shogun/lib/common.h> #include <shogun/io/SGIO.h> #include <libgen.h> using namespace shogun; #define MAX_LINE_LENGTH 4096 #define HOG_SIZE 1488 struct CBoundingBox : public CData { CBoundingBox(int32_t x, int32_t y) : CData(), x_pos(x), y_pos(y) {}; int32_t x_pos, y_pos; /** @return name of SGSerializable */ virtual const char* get_name() const { return "BoundingBox"; } }; struct CHOGFeatures : public CData { CHOGFeatures(int32_t w, int32_t h) : CData(), width(w), height(h) {}; int32_t width, height; float64_t ***hog; /** @return name of SGSerializable */ virtual const char* get_name() const { return "HOGFeatures"; } }; class CObjectDetector: public CLatentModel { public: CObjectDetector() {} CObjectDetector(CLatentFeatures* feat, CLatentLabels* labels) : CLatentModel(feat, labels) {} virtual ~CObjectDetector() {} virtual int32_t get_dim() const { return HOG_SIZE; } virtual CDotFeatures* get_psi_feature_vectors() { int32_t num_examples = this->get_num_vectors(); int32_t dim = this->get_dim(); SGMatrix<float64_t> psi_m(dim, num_examples); for (int32_t i = 0; i < num_examples; ++i) { CHOGFeatures* hf = (CHOGFeatures*) m_features->get_sample(i); CBoundingBox* bb = (CBoundingBox*) m_labels->get_latent_label(i); memcpy(psi_m.matrix+i*dim, hf->hog[bb->x_pos][bb->y_pos], dim*sizeof(float64_t)); } CDenseFeatures<float64_t>* psi_feats = new CDenseFeatures<float64_t>(psi_m); return psi_feats; } virtual CData* infer_latent_variable(const SGVector<float64_t>& w, index_t idx) { int32_t pos_x = 0, pos_y = 0; float64_t max_score = -CMath::INFTY; CHOGFeatures* hf = (CHOGFeatures*) m_features->get_sample(idx); for (int i = 0; i < hf->width; ++i) { for (int j = 0; j < hf->height; ++j) { float64_t score = w.dot(w.vector, hf->hog[i][j], w.vlen); if (score > max_score) { pos_x = i; pos_y = j; max_score = score; } } } SG_SDEBUG("%d %d %f\n", pos_x, pos_y, max_score); CBoundingBox* h = new CBoundingBox(pos_x, pos_y); SG_REF(h); return h; } }; static void read_dataset(char* fname, CLatentFeatures*& feats, CLatentLabels*& labels) { FILE* fd = fopen(fname, "r"); char line[MAX_LINE_LENGTH]; char *pchar, *last_pchar; int num_examples,label,height,width; char* path = dirname(fname); if (fd == NULL) SG_SERROR("Cannot open input file %s!\n", fname); fgets(line, MAX_LINE_LENGTH, fd); num_examples = atoi(line); labels = new CLatentLabels(num_examples); SG_REF(labels); CBinaryLabels* ys = new CBinaryLabels(num_examples); feats = new CLatentFeatures(num_examples); SG_REF(feats); CMath::init_random(); for (int i = 0; (!feof(fd)) && (i < num_examples); ++i) { fgets(line, MAX_LINE_LENGTH, fd); pchar = line; while ((*pchar)!=' ') pchar++; *pchar = '\0'; pchar++; /* label: {-1, 1} */ last_pchar = pchar; while ((*pchar)!=' ') pchar++; *pchar = '\0'; label = (atoi(last_pchar) % 2 == 0) ? 1 : -1; pchar++; if (ys->set_label(i, label) == false) SG_SERROR("Couldn't set label for element %d\n", i); last_pchar = pchar; while ((*pchar)!=' ') pchar++; *pchar = '\0'; width = atoi(last_pchar); pchar++; last_pchar = pchar; while ((*pchar)!='\n') pchar++; *pchar = '\0'; height = atoi(last_pchar); /* create latent label */ int x = CMath::random(0, width-1); int y = CMath::random(0, height-1); CBoundingBox* bb = new CBoundingBox(x,y); labels->add_latent_label(bb); SG_SPROGRESS(i, 0, num_examples); CHOGFeatures* hog = new CHOGFeatures(width, height); hog->hog = SG_CALLOC(float64_t**, hog->width); for (int j = 0; j < width; ++j) { hog->hog[j] = SG_CALLOC(float64_t*, hog->height); for (int k = 0; k < height; ++k) { char filename[MAX_LINE_LENGTH]; hog->hog[j][k] = SG_CALLOC(float64_t, HOG_SIZE); sprintf(filename,"%s/%s.%03d.%03d.txt",path,line,j,k); FILE* f = fopen(filename, "r"); if (f == NULL) SG_SERROR("Could not open file: %s\n", filename); for (int l = 0; l < HOG_SIZE; ++l) fscanf(f,"%lf",&hog->hog[j][k][l]); fclose(f); } } feats->add_sample(hog); } fclose(fd); labels->set_labels(ys); SG_SDONE(); } int main(int argc, char** argv) { init_shogun_with_defaults(); sg_io->set_loglevel(MSG_DEBUG); /* check whether the train/test args are given */ if (argc < 3) { SG_SERROR("not enough arguements given\n"); } CLatentFeatures* train_feats = NULL; CLatentLabels* train_labels = NULL; /* read train data set */ read_dataset(argv[1], train_feats, train_labels); /* train the classifier */ float64_t C = 10.0; CObjectDetector* od = new CObjectDetector(train_feats, train_labels); CLatentSVM llm(od, C); llm.train(); // CLatentFeatures* test_feats = NULL; // CLatentLabels* test_labels = NULL; // read_dataset(argv[2], test_feats, test_labels); SG_SPRINT("Testing with the test set\n"); llm.apply(train_feats); exit_shogun(); return 0; }
/* * 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) 2013 Kevin Hughes * Copyright (C) 2013 Kevin Hughes * * Thanks to Fernando Jose Iglesias Garcia (shogun) * and Matthieu Perrot (scikit-learn) */ #include <shogun/base/init.h> #include <shogun/lib/config.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/multiclass/MCLDA.h> #include <shogun/features/DenseFeatures.h> #include <shogun/io/SGIO.h> #include <shogun/lib/common.h> #include <shogun/features/DataGenerator.h> using namespace shogun; #define NUM 50 #define DIMS 2 #define CLASSES 2 void test() { #ifdef HAVE_LAPACK #ifdef HAVE_EIGEN3 SGVector< float64_t > lab(CLASSES*NUM); SGMatrix< float64_t > feat(DIMS, CLASSES*NUM); feat = CDataGenerator::generate_gaussians(NUM,CLASSES,DIMS); for( int i = 0 ; i < CLASSES ; ++i ) for( int j = 0 ; j < NUM ; ++j ) lab[i*NUM+j] = double(i); // Create train labels CMulticlassLabels* labels = new CMulticlassLabels(lab); // Create train features CDenseFeatures< float64_t >* features = new CDenseFeatures< float64_t >(feat); // Create QDA classifier CMCLDA* lda = new CMCLDA(features, labels); SG_REF(lda); lda->train(); // Classify and display output CMulticlassLabels* output=CLabelsFactory::to_multiclass(lda->apply()); SG_REF(output); SGVector<float64_t>::display_vector(output->get_labels().vector, output->get_num_labels()); // Free memory SG_UNREF(output); SG_UNREF(lda); #endif #endif } int main(int argc, char ** argv) { init_shogun_with_defaults(); test(); exit_shogun(); return 0; }
/* * 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) 2008-2009 Soeren Sonnenburg * Written (W) 2012 Heiko Strathmann * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max Planck Society */ #include <shogun/base/init.h> #include <shogun/kernel/GaussianKernel.h> #include <shogun/labels/BinaryLabels.h> #include <shogun/features/DenseFeatures.h> #include <shogun/classifier/svm/LibSVM.h> using namespace shogun; void gen_rand_data(SGVector<float64_t> lab, SGMatrix<float64_t> feat, float64_t dist) { index_t dims=feat.num_rows; index_t num=lab.vlen; for (int32_t i=0; i<num; i++) { if (i<num/2) { lab[i]=-1.0; 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(j, i)=CMath::random(0.0, 1.0)-dist; } } lab.display_vector("lab"); feat.display_matrix("feat"); } 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; index_t num=100; index_t dims=2; float64_t dist=0.5; SGVector<float64_t> lab(num); SGMatrix<float64_t> feat(dims, num); gen_rand_data(lab, feat, dist); // create train labels CLabels* labels=new CBinaryLabels(lab); // create train features 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); SG_REF(kernel); kernel->init(features, features); // create svm via libsvm and train CLibSVM* svm=new CLibSVM(svm_C, kernel, labels); SG_REF(svm); svm->set_epsilon(svm_eps); svm->train(); SG_SPRINT("num_sv:%d b:%f\n", svm->get_num_support_vectors(), svm->get_bias()); // classify + display output CBinaryLabels* out_labels=CLabelsFactory::to_binary(svm->apply()); for (int32_t i=0; i<num; i++) { SG_SPRINT("out[%d]=%f (%f)\n", i, out_labels->get_label(i), out_labels->get_value(i)); } SG_UNREF(out_labels); SG_UNREF(kernel); SG_UNREF(features); SG_UNREF(svm); } int main() { init_shogun(); test_libsvm(); exit_shogun(); return 0; }
#include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/kernel/LinearKernel.h> #include <shogun/labels/BinaryLabels.h> #include <shogun/classifier/svm/LibSVM.h> #include <iostream> using namespace shogun; //generates data points (of different classes) randomly void gen_rand_data(SGMatrix<float64_t> features, SGVector<float64_t> labels, float64_t distance) { index_t num_samples=labels.vlen; index_t dimensions=features.num_rows; for (int32_t i=0; i<num_samples; i++) { if (i<num_samples/2) { labels[i]=-1.0; for(int32_t j=0; j<dimensions; j++) features(j,i)=CMath::random(0.0,1.0)+distance; } else { labels[i]=1.0; for(int32_t j=0; j<dimensions; j++) features(j,i)=CMath::random(0.0,1.0)-distance; } } labels.display_vector("labels"); std::cout<<std::endl; features.display_matrix("features"); std::cout<<std::endl; } int main(int argc, char** argv) { init_shogun_with_defaults(); const float64_t svm_C=10; index_t num_samples=20; index_t dimensions=2; float64_t dist=0.5; SGMatrix<float64_t> featureMatrix(dimensions,num_samples); SGVector<float64_t> labelVector(num_samples); //random generation of data gen_rand_data(featureMatrix,labelVector,dist); //create train labels CLabels* labels=new CBinaryLabels(labelVector); //create train features CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(); SG_REF(features); features->set_feature_matrix(featureMatrix); //create linear kernel CLinearKernel* kernel=new CLinearKernel(); SG_REF(kernel); kernel->init(features, features); //create svm classifier by LibSVM CLibSVM* svm=new CLibSVM(svm_C,kernel, labels); SG_REF(svm); svm->train(); //classify data points CBinaryLabels* out_labels=CLabelsFactory::to_binary(svm->apply()); /*convert scores to calibrated probabilities by fitting a sigmoid function using the method described in Lin, H., Lin, C., and Weng, R. (2007). A note on Platt's probabilistic outputs for support vector machines. See BinaryLabels documentation for details*/ out_labels->scores_to_probabilities(); //display output labels and probabilities for (int32_t i=0; i<num_samples; i++) { SG_SPRINT("out[%d]=%f (%f)\n", i, out_labels->get_label(i), out_labels->get_value(i)); } //clean up SG_UNREF(out_labels); SG_UNREF(kernel); SG_UNREF(features); SG_UNREF(svm); exit_shogun(); return 0; }
#include <shogun/labels/BinaryLabels.h> #include <shogun/features/DenseFeatures.h> #include <shogun/kernel/GaussianKernel.h> #include <shogun/classifier/svm/LibSVM.h> #include <shogun/base/init.h> #include <shogun/lib/common.h> #include <shogun/io/SGIO.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); // create some data SGMatrix<float64_t> matrix(2,3); for (int32_t i=0; i<6; i++) matrix.matrix[i]=i; // create three 2-dimensional vectors // shogun will now own the matrix created CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(); features->set_feature_matrix(matrix); // create three labels CBinaryLabels* labels=new CBinaryLabels(3); labels->set_label(0, -1); labels->set_label(1, +1); labels->set_label(2, -1); // 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 CLibSVM* svm = new CLibSVM(10, kernel, labels); svm->train(); // classify on training examples for (int32_t i=0; i<3; i++) SG_SPRINT("output[%d]=%f\n", i, svm->apply_one(i)); // free up memory SG_UNREF(svm); exit_shogun(); return 0; }
/* * 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) 2009 Alexander Binder * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society */ #include <shogun/base/init.h> #include <iostream> #include <shogun/io/SGIO.h> #include <shogun/lib/ShogunException.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/kernel/CustomKernel.h> #include <shogun/kernel/CombinedKernel.h> #include <shogun/classifier/mkl/MKLMulticlass.h> // g++ -Wall -O3 classifier_mklmulticlass.cpp -I /home/theseus/private/alx/shoguntrunk/compiledtmp/include -L/home/theseus/private/alx/shoguntrunk/compiledtmp/lib -lshogun using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } void print_warning(FILE* target, const char* str) { fprintf(target, "%s", str); } void print_error(FILE* target, const char* str) { fprintf(target, "%s", str); } void getgauss(float64_t & y1, float64_t & y2) { float x1, x2, w; do { x1 = 2.0 * rand()/(float64_t)RAND_MAX - 1.0; x2 = 2.0 * rand()/(float64_t)RAND_MAX - 1.0; w = x1 * x1 + x2 * x2; } while ( (w >= 1.0)|| (w<1e-9) ); w = sqrt( (-2.0 * log( w ) ) / w ); y1 = x1 * w; y2 = x2 * w; } void gendata(std::vector<float64_t> & x,std::vector<float64_t> & y, CMulticlassLabels*& lab) { int32_t totalsize=240; int32_t class1size=80; int32_t class2size=70; //generating three class data set x.resize(totalsize); y.resize(totalsize); for(size_t i=0; i< x.size();++i) getgauss(x[i], y[i]); for(size_t i=0; i< x.size();++i) { if((int32_t)i < class1size) { x[i]+=0; y[i]+=0; } else if( (int32_t)i< class1size+class2size) { x[i]+=+1; y[i]+=-1; } else { x[i]+=-1; y[i]+=+1; } } //set labels lab=new CMulticlassLabels(x.size()); for(size_t i=0; i< x.size();++i) { if((int32_t)i < class1size) lab->set_int_label(i,0); else if( (int32_t)i< class1size+class2size) lab->set_int_label(i,1); else lab->set_int_label(i,2); } } void gentrainkernel(float64_t * & ker1 ,float64_t * & ker2, float64_t * & ker3 ,float64_t & autosigma,float64_t & n1,float64_t & n2, float64_t & n3, const std::vector<float64_t> & x, const std::vector<float64_t> & y) { autosigma=0; for(size_t l=0; l< x.size();++l) { for(size_t r=0; r<= l;++r) { float64_t dist=((x[l]-x[r])*(x[l]-x[r]) + (y[l]-y[r])*(y[l]-y[r])); autosigma+=dist*2.0/(float64_t)x.size()/((float64_t)x.size()+1); } } float64_t fm1=0, mean1=0,fm2=0, mean2=0,fm3=0, mean3=0; ker1=SG_MALLOC(float64_t, x.size()*x.size()); ker2=SG_MALLOC(float64_t, x.size()*x.size()); ker3=SG_MALLOC(float64_t, x.size()*x.size()); for(size_t l=0; l< x.size();++l) { for(size_t r=0; r< x.size();++r) { float64_t dist=((x[l]-x[r])*(x[l]-x[r]) + (y[l]-y[r])*(y[l]-y[r])); ker1[l +r*x.size()]= exp( -dist/autosigma/autosigma) ; //ker2[l +r*x.size()]= exp( -dist/sigma2/sigma2) ; ker2[l +r*x.size()]= x[l]*x[r] + y[l]*y[r]; ker3[l +r*x.size()]= (x[l]*x[r] + y[l]*y[r]+1)*(x[l]*x[r] + y[l]*y[r]+1); fm1+=ker1[l +r*x.size()]/(float64_t)x.size()/((float64_t)x.size()); fm2+=ker2[l +r*x.size()]/(float64_t)x.size()/((float64_t)x.size()); fm3+=ker3[l +r*x.size()]/(float64_t)x.size()/((float64_t)x.size()); if(l==r) { mean1+=ker1[l +r*x.size()]/(float64_t)x.size(); mean2+=ker2[l +r*x.size()]/(float64_t)x.size(); mean3+=ker3[l +r*x.size()]/(float64_t)x.size(); } } } n1=(mean1-fm1); n2=(mean2-fm2); n3=(mean3-fm3); for(size_t l=0; l< x.size();++l) { for(size_t r=0; r< x.size();++r) { ker1[l +r*x.size()]=ker1[l +r*x.size()]/n1; ker2[l +r*x.size()]=ker2[l +r*x.size()]/n2; ker3[l +r*x.size()]=ker3[l +r*x.size()]/n3; } } } void gentestkernel(float64_t * & ker1 ,float64_t * & ker2,float64_t * & ker3, const float64_t autosigma,const float64_t n1,const float64_t n2, const float64_t n3, const std::vector<float64_t> & x,const std::vector<float64_t> & y, const std::vector<float64_t> & tx,const std::vector<float64_t> & ty) { ker1=SG_MALLOC(float64_t, x.size()*tx.size()); ker2=SG_MALLOC(float64_t, x.size()*tx.size()); ker3=SG_MALLOC(float64_t, x.size()*tx.size()); for(size_t l=0; l< x.size();++l) { for(size_t r=0; r< tx.size();++r) { float64_t dist=((x[l]-tx[r])*(x[l]-tx[r]) + (y[l]-ty[r])*(y[l]-ty[r])); ker1[l +r*x.size()]= exp( -dist/autosigma/autosigma) ; ker2[l +r*x.size()]= x[l]*tx[r] + y[l]*ty[r]; ker3[l +r*x.size()]= (x[l]*tx[r] + y[l]*ty[r]+1)*(x[l]*tx[r] + y[l]*ty[r]+1); } } for(size_t l=0; l< x.size();++l) { for(size_t r=0; r< tx.size();++r) { ker1[l +r*x.size()]=ker1[l +r*x.size()]/n1; ker2[l +r*x.size()]=ker2[l +r*x.size()]/n2; ker3[l +r*x.size()]=ker3[l +r*x.size()]/n2; } } } void tester() { CMulticlassLabels* lab=NULL; std::vector<float64_t> x,y; gendata(x,y, lab); SG_REF(lab); float64_t* ker1=NULL; float64_t* ker2=NULL; float64_t* ker3=NULL; float64_t autosigma=1; float64_t n1=0; float64_t n2=0; float64_t n3=0; int32_t numdata=0; gentrainkernel( ker1 , ker2, ker3 , autosigma, n1, n2, n3,x,y); numdata=x.size(); CCombinedKernel* ker=new CCombinedKernel(); CCustomKernel* kernel1=new CCustomKernel(); CCustomKernel* kernel2=new CCustomKernel(); CCustomKernel* kernel3=new CCustomKernel(); kernel1->set_full_kernel_matrix_from_full(SGMatrix<float64_t>(ker1, numdata,numdata,false)); kernel2->set_full_kernel_matrix_from_full(SGMatrix<float64_t>(ker2, numdata,numdata,false)); kernel3->set_full_kernel_matrix_from_full(SGMatrix<float64_t>(ker3, numdata,numdata,false)); SG_FREE(ker1); SG_FREE(ker2); SG_FREE(ker3); ker->append_kernel(kernel1); ker->append_kernel(kernel2); ker->append_kernel(kernel3); //here comes the core stuff float64_t regconst=1.0; CMKLMulticlass* tsvm =new CMKLMulticlass(regconst, ker, lab); tsvm->set_epsilon(0.0001); // SVM epsilon // MKL parameters tsvm->set_mkl_epsilon(0.01); // subkernel weight L2 norm termination criterion tsvm->set_max_num_mkliters(120); // well it will be just three iterations tsvm->set_mkl_norm(1.5); // mkl norm //starting svm training tsvm->train(); SG_SPRINT("finished svm training\n"); //starting svm testing on training data CMulticlassLabels* res=CLabelsFactory::to_multiclass(tsvm->apply()); ASSERT(res); float64_t err=0; for(int32_t i=0; i<numdata;++i) { ASSERT(i< res->get_num_labels()); if (lab->get_int_label(i)!=res->get_int_label(i)) err+=1; } err/=(float64_t)res->get_num_labels(); SG_SPRINT("prediction error on training data (3 classes): %f ",err); SG_SPRINT("random guess error would be: %f \n",2/3.0); //generate test data CMulticlassLabels* tlab=NULL; std::vector<float64_t> tx,ty; gendata( tx,ty,tlab); SG_REF(tlab); float64_t* tker1=NULL; float64_t* tker2=NULL; float64_t* tker3=NULL; gentestkernel(tker1,tker2,tker3, autosigma, n1,n2,n3, x,y, tx,ty); int32_t numdatatest=tx.size(); CCombinedKernel* tker=new CCombinedKernel(); SG_REF(tker); CCustomKernel* tkernel1=new CCustomKernel(); CCustomKernel* tkernel2=new CCustomKernel(); CCustomKernel* tkernel3=new CCustomKernel(); tkernel1->set_full_kernel_matrix_from_full(SGMatrix<float64_t>(tker1,numdata, numdatatest, false)); tkernel2->set_full_kernel_matrix_from_full(SGMatrix<float64_t>(tker2,numdata, numdatatest, false)); tkernel3->set_full_kernel_matrix_from_full(SGMatrix<float64_t>(tker2,numdata, numdatatest, false)); SG_FREE(tker1); SG_FREE(tker2); SG_FREE(tker3); tker->append_kernel(tkernel1); tker->append_kernel(tkernel2); tker->append_kernel(tkernel3); int32_t numweights; float64_t* weights=tsvm->getsubkernelweights(numweights); SG_SPRINT("test kernel weights\n"); for(int32_t i=0; i< numweights;++i) SG_SPRINT("%f ", weights[i]); SG_SPRINT("\n"); //set kernel tker->set_subkernel_weights(SGVector<float64_t>(weights, numweights)); tsvm->set_kernel(tker); //compute classification error, check mem CMulticlassLabels* tres=CLabelsFactory::to_multiclass(tsvm->apply()); float64_t terr=0; for(int32_t i=0; i<numdatatest;++i) { ASSERT(i< tres->get_num_labels()); if(tlab->get_int_label(i)!=tres->get_int_label(i)) terr+=1; } terr/=(float64_t) tres->get_num_labels(); SG_SPRINT("prediction error on test data (3 classes): %f ",terr); SG_SPRINT("random guess error would be: %f \n",2/3.0); SG_UNREF(tsvm); SG_UNREF(res); SG_UNREF(tres); SG_UNREF(lab); SG_UNREF(tlab); SG_UNREF(tker); SG_SPRINT( "finished \n"); } namespace shogun { extern Version* sg_version; extern SGIO* sg_io; } int main() { init_shogun(&print_message, &print_warning, &print_error); try { sg_version->print_version(); sg_io->set_loglevel(MSG_INFO); tester(); } catch(ShogunException & sh) { printf("%s",sh.get_exception_string()); } exit_shogun(); return 0; }
#include <shogun/io/CSVFile.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/io/SGIO.h> #include <shogun/features/DenseFeatures.h> #include <shogun/multiclass/ecoc/ECOCStrategy.h> #include <shogun/multiclass/ecoc/ECOCOVREncoder.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; // Training data const char fname_feats[]="../data/fm_train_real.dat"; const char fname_labels[]="../data/label_train_multiclass.dat"; void test() { /* dense features from matrix */ CCSVFile* feature_file = new CCSVFile(fname_feats); SGMatrix<float64_t> mat=SGMatrix<float64_t>(); mat.load(feature_file); SG_UNREF(feature_file); CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat); SG_REF(features); /* labels from vector */ CCSVFile* label_file = new CCSVFile(fname_labels); SGVector<float64_t> label_vec; label_vec.load(label_file); SG_UNREF(label_file); CMulticlassLabels* labels=new CMulticlassLabels(label_vec); SG_REF(labels); // 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 CECOCOVREncoder(), 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 CMulticlassLabels* output = CLabelsFactory::to_multiclass(mc_svm->apply()); SGVector< int32_t > out_labels = output->get_int_labels(); SGVector< int32_t >::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); } int main(int argc, char** argv) { init_shogun_with_defaults(); test(); exit_shogun(); return 0; }
#include <shogun/io/CSVFile.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/io/SGIO.h> #include <shogun/features/DenseFeatures.h> #include <shogun/multiclass/ecoc/ECOCStrategy.h> #include <shogun/multiclass/ecoc/ECOCDiscriminantEncoder.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; /* file data */ const char fname_feats[]="../data/fm_train_real.dat"; const char fname_labels[]="../data/label_train_multiclass.dat"; void test() { /* dense features from matrix */ CCSVFile* feature_file = new CCSVFile(fname_feats); SGMatrix<float64_t> mat=SGMatrix<float64_t>(); mat.load(feature_file); SG_UNREF(feature_file); CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat); SG_REF(features); /* labels from vector */ CCSVFile* label_file = new CCSVFile(fname_labels); SGVector<float64_t> label_vec; label_vec.load(label_file); SG_UNREF(label_file); CMulticlassLabels* labels=new CMulticlassLabels(label_vec); SG_REF(labels); // 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); CECOCDiscriminantEncoder *encoder = new CECOCDiscriminantEncoder(); encoder->set_features(features); encoder->set_labels(labels); // Create a multiclass svm classifier that consists of several of the previous one CLinearMulticlassMachine* mc_svm = new CLinearMulticlassMachine( new CECOCStrategy(encoder, 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 CMulticlassLabels* output = CLabelsFactory::to_multiclass(mc_svm->apply()); SGVector< int32_t > out_labels = output->get_int_labels(); SGVector< int32_t >::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); } int main(int argc, char** argv) { init_shogun_with_defaults(); test(); exit_shogun(); return 0; }
#include <shogun/io/CSVFile.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/io/SGIO.h> #include <shogun/features/DenseFeatures.h> #include <shogun/multiclass/ecoc/ECOCStrategy.h> #include <shogun/multiclass/ecoc/ECOCRandomDenseEncoder.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; /* file data */ const char fname_feats[]="../data/fm_train_real.dat"; const char fname_labels[]="../data/label_train_multiclass.dat"; void test() { /* dense features from matrix */ CCSVFile* feature_file = new CCSVFile(fname_feats); SGMatrix<float64_t> mat=SGMatrix<float64_t>(); mat.load(feature_file); SG_UNREF(feature_file); CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat); SG_REF(features); /* labels from vector */ CCSVFile* label_file = new CCSVFile(fname_labels); SGVector<float64_t> label_vec; label_vec.load(label_file); SG_UNREF(label_file); CMulticlassLabels* labels=new CMulticlassLabels(label_vec); SG_REF(labels); // 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 CECOCRandomDenseEncoder(), 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 CMulticlassLabels* output = CLabelsFactory::to_multiclass(mc_svm->apply()); SGVector< int32_t > out_labels = output->get_int_labels(); SGVector< int32_t >::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); } int main(int argc, char** argv) { init_shogun_with_defaults(); // sg_io->set_loglevel(MSG_DEBUG); test(); exit_shogun(); return 0; }
#include <shogun/io/CSVFile.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/io/SGIO.h> #include <shogun/features/DenseFeatures.h> #include <shogun/multiclass/MulticlassStrategy.h> #include <shogun/multiclass/MulticlassOneVsOneStrategy.h> #include <shogun/multiclass/MulticlassOneVsRestStrategy.h> #include <shogun/machine/LinearMulticlassMachine.h> #include <shogun/classifier/svm/LibLinear.h> #include <shogun/base/init.h> #define EPSILON 1e-5 using namespace shogun; /* file data */ const char fname_feats[]="../data/fm_train_real.dat"; const char fname_labels[]="../data/label_train_multiclass.dat"; void test() { /* dense features from matrix */ CCSVFile* feature_file = new CCSVFile(fname_feats); SGMatrix<float64_t> mat=SGMatrix<float64_t>(); mat.load(feature_file); SG_UNREF(feature_file); CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat); SG_REF(features); /* labels from vector */ CCSVFile* label_file = new CCSVFile(fname_labels); SGVector<float64_t> label_vec; label_vec.load(label_file); SG_UNREF(label_file); CMulticlassLabels* labels=new CMulticlassLabels(label_vec); SG_REF(labels); // 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 // There are several heuristics are implemented: // OVA_NORM, OVA_SOFTMAX // OVO_PRICE, OVO_HASTIE, OVO_HAMAMURA CLinearMulticlassMachine* mc_svm = new CLinearMulticlassMachine( new CMulticlassOneVsOneStrategy(OVO_HASTIE), (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 CMulticlassLabels* output = CLabelsFactory::to_multiclass(mc_svm->apply()); SGVector< int32_t > out_labels = output->get_int_labels(); SGVector<int32_t>::display_vector(out_labels.vector, out_labels.vlen); for (int32_t i=0; i<output->get_num_labels(); i++) { SG_SPRINT("out_values[%d] = ", i); SGVector<float64_t> out_values = output->get_multiclass_confidences(i); SGVector<float64_t>::display_vector(out_values.vector, out_values.vlen); SG_SPRINT("\n"); } //Free resources SG_UNREF(mc_svm); SG_UNREF(svm); SG_UNREF(output); SG_UNREF(features); SG_UNREF(labels); } int main(int argc, char** argv) { init_shogun_with_defaults(); //sg_io->set_loglevel(MSG_DEBUG); test(); exit_shogun(); return 0; }
#include <shogun/io/CSVFile.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/io/SGIO.h> #include <shogun/features/DenseFeatures.h> #include <shogun/features/DenseSubsetFeatures.h> #include <shogun/base/init.h> #include <shogun/multiclass/tree/RelaxedTree.h> #include <shogun/multiclass/MulticlassLibLinear.h> #include <shogun/evaluation/MulticlassAccuracy.h> #include <shogun/kernel/GaussianKernel.h> #define EPSILON 1e-5 using namespace shogun; const char* fname_feats = "../data/7class_example4_train.dense"; const char* fname_labels = "../data/7class_example4_train.label"; int main(int argc, char** argv) { init_shogun_with_defaults(); /* dense features from matrix */ CCSVFile* feature_file = new CCSVFile(fname_feats); SGMatrix<float64_t> mat=SGMatrix<float64_t>(); mat.load(feature_file); SG_UNREF(feature_file); CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat); SG_REF(features); /* labels from vector */ CCSVFile* label_file = new CCSVFile(fname_labels); SGVector<float64_t> label_vec; label_vec.load(label_file); SG_UNREF(label_file); CMulticlassLabels* labels=new CMulticlassLabels(label_vec); SG_REF(labels); // Create RelaxedTree Machine CRelaxedTree *machine = new CRelaxedTree(); SG_REF(machine); machine->set_labels(labels); CKernel *kernel = new CGaussianKernel(); SG_REF(kernel); machine->set_kernel(kernel); CMulticlassLibLinear *svm = new CMulticlassLibLinear(); machine->set_machine_for_confusion_matrix(svm); machine->train(features); CMulticlassLabels* output = CLabelsFactory::to_multiclass(machine->apply()); CMulticlassAccuracy *evaluator = new CMulticlassAccuracy(); SG_SPRINT("Accuracy = %.4f\n", evaluator->evaluate(output, labels)); // Free resources SG_UNREF(machine); SG_UNREF(output); SG_UNREF(features); SG_UNREF(labels); SG_UNREF(evaluator); SG_UNREF(kernel); exit_shogun(); return 0; }
#include <shogun/io/CSVFile.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/io/SGIO.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; const char* fname_feats = "../data/7class_example4_train.dense"; const char* fname_labels = "../data/7class_example4_train.label"; int main(int argc, char** argv) { init_shogun_with_defaults(); /* dense features from matrix */ CCSVFile* feature_file = new CCSVFile(fname_feats); SGMatrix<float64_t> mat=SGMatrix<float64_t>(); mat.load(feature_file); SG_UNREF(feature_file); CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat); SG_REF(features); /* labels from vector */ CCSVFile* label_file = new CCSVFile(fname_labels); SGVector<float64_t> label_vec; label_vec.load(label_file); SG_UNREF(label_file); CMulticlassLabels* labels=new CMulticlassLabels(label_vec); SG_REF(labels); 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 = CLabelsFactory::to_multiclass(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); exit_shogun(); return 0; }
#include <shogun/labels/MulticlassLabels.h> #include <shogun/features/DenseFeatures.h> #include <shogun/kernel/GaussianKernel.h> #include <shogun/multiclass/MulticlassLibSVM.h> #include <shogun/base/init.h> using namespace shogun; int main(int argc, char** argv) { init_shogun_with_defaults(); 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 CMulticlassLibSVM* svm = new CMulticlassLibSVM(10, kernel, labels); svm->train(); // classify on training examples CMulticlassLabels* output=CLabelsFactory::to_multiclass(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; }
#include <shogun/io/CSVFile.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/io/SGIO.h> #include <shogun/features/DenseFeatures.h> #include <shogun/multiclass/MulticlassOneVsOneStrategy.h> #include <shogun/machine/LinearMulticlassMachine.h> #include <shogun/classifier/svm/LibLinear.h> #include <shogun/base/init.h> #define EPSILON 1e-5 using namespace shogun; /* file data */ const char fname_feats[]="../data/fm_train_real.dat"; const char fname_labels[]="../data/label_train_multiclass.dat"; void test() { /* dense features from matrix */ CCSVFile* feature_file = new CCSVFile(fname_feats); SGMatrix<float64_t> mat=SGMatrix<float64_t>(); mat.load(feature_file); SG_UNREF(feature_file); CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat); SG_REF(features); /* labels from vector */ CCSVFile* label_file = new CCSVFile(fname_labels); SGVector<float64_t> label_vec; label_vec.load(label_file); SG_UNREF(label_file); CMulticlassLabels* labels=new CMulticlassLabels(label_vec); SG_REF(labels); // 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 CMulticlassOneVsOneStrategy(), (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 CMulticlassLabels* output = CLabelsFactory::to_multiclass(mc_svm->apply()); SGVector< int32_t > out_labels = output->get_int_labels(); SGVector<int32_t>::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); } int main(int argc, char** argv) { init_shogun_with_defaults(); sg_io->set_loglevel(MSG_DEBUG); test(); exit_shogun(); return 0; }
#include <shogun/features/Labels.h> #include <shogun/features/DenseFeatures.h> #include <shogun/distance/EuclideanDistance.h> #include <shogun/classifier/NearestCentroid.h> #include <shogun/base/init.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } int main(){ init_shogun(&print_message); index_t num_vec=7; index_t num_feat=2; index_t num_class=2; // create some data SGMatrix<float64_t> matrix(num_feat, num_vec); CMath::range_fill_vector(matrix.matrix, num_feat*num_vec); // Create features ; shogun will now own the matrix created CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(matrix); CMath::display_matrix(matrix.matrix,num_feat,num_vec); //Create labels CLabels* labels=new CLabels(num_vec); for (index_t i=0; i<num_vec; ++i) labels->set_label(i, i%num_class); //Create Euclidean Distance CEuclideanDistance* distance = new CEuclideanDistance(features,features); //Create Nearest Centroid CNearestCentroid* nearest_centroid = new CNearestCentroid(distance, labels); nearest_centroid->train(); // classify on training examples CLabels* output=nearest_centroid->apply(); CMath::display_vector(output->get_labels().vector, output->get_num_labels(), "batch output"); SG_UNREF(output); // free up memory SG_UNREF(nearest_centroid); exit_shogun(); return 0; }
#include <shogun/base/init.h> #include <shogun/features/Labels.h> #include <shogun/features/DenseFeatures.h> #include <shogun/mathematics/Math.h> #include <shogun/classifier/svm/NewtonSVM.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,&print_message,&print_message);//initialising shogun without giving arguments shogun wont be able to print int32_t x_n=4,x_d=2;//X dimensions : x_n for no of datapoints and x_d for dimensionality of data SGMatrix<float64_t> fmatrix(x_d,x_n); SG_SPRINT("\nTEST 1:\n\n"); /*Initialising Feature Matrix */ for (int i=0; i<x_n*x_d; i++) fmatrix.matrix[i] = i+1; SG_SPRINT("FEATURE MATRIX :\n"); CMath::display_matrix(fmatrix.matrix,x_d,x_n); CDenseFeatures<float64_t>* features = new CDenseFeatures<float64_t>(fmatrix); SG_REF(features); /*Creating random labels */ CLabels* labels=new CLabels(x_n); // create labels, two classes labels->set_label(0,1); labels->set_label(1,-1); labels->set_label(2,1); labels->set_label(3,1); SG_REF(labels); /*Working with Newton SVM */ float64_t lambda=1.0; int32_t iter=20; CNewtonSVM *nsvm = new CNewtonSVM(lambda,features,labels,iter); SG_REF(nsvm); nsvm->train(); SG_UNREF(labels); SG_UNREF(nsvm); SG_SPRINT("TEST 2:\n\n"); x_n=5; x_d=3; SGMatrix<float64_t> fmatrix2(x_d,x_n); for (int i=0; i<x_n*x_d; i++) fmatrix2.matrix[i] = i+1; SG_SPRINT("FEATURE MATRIX :\n"); CMath::display_matrix(fmatrix2.matrix,x_d,x_n); features->set_feature_matrix(fmatrix2); SG_REF(features); /*Creating random labels */ CLabels* labels2=new CLabels(x_n); // create labels, two classes labels2->set_label(0,1); labels2->set_label(1,-1); labels2->set_label(2,1); labels2->set_label(3,1); labels2->set_label(4,-1); SG_REF(labels2); /*Working with Newton SVM */ lambda=1.0; iter=20; CNewtonSVM *nsvm2 = new CNewtonSVM(lambda,features,labels2,iter); SG_REF(nsvm2); nsvm2->train(); SG_UNREF(labels2); SG_UNREF(nsvm2); SG_UNREF(features); exit_shogun(); return 0; }
/* * 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 Fernando Jose Iglesias Garcia * Copyright (C) 2012 Fernando Jose Iglesias Garcia */ #include <shogun/base/init.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/multiclass/QDA.h> #include <shogun/features/DenseFeatures.h> #include <shogun/io/SGIO.h> #include <shogun/lib/common.h> #include <shogun/features/DataGenerator.h> using namespace shogun; #define NUM 50 #define DIMS 2 #define CLASSES 2 void test() { #ifdef HAVE_EIGEN3 #ifdef HAVE_LAPACK SGVector< float64_t > lab(CLASSES*NUM); SGMatrix< float64_t > feat(DIMS, CLASSES*NUM); feat = CDataGenerator::generate_gaussians(NUM,CLASSES,DIMS); for( int i = 0 ; i < CLASSES ; ++i ) for( int j = 0 ; j < NUM ; ++j ) lab[i*NUM+j] = double(i); // Create train labels CMulticlassLabels* labels = new CMulticlassLabels(lab); // Create train features CDenseFeatures< float64_t >* features = new CDenseFeatures< float64_t >(feat); // Create QDA classifier CQDA* qda = new CQDA(features, labels); SG_REF(qda); qda->train(); // Classify and display output CMulticlassLabels* output = CLabelsFactory::to_multiclass(qda->apply()); SG_REF(output); SGVector<float64_t>::display_vector(output->get_labels().vector, output->get_num_labels()); // Free memory SG_UNREF(output); SG_UNREF(qda); #endif // HAVE_LAPACK #endif // HAVE_EIGEN3 } int main(int argc, char ** argv) { init_shogun_with_defaults(); test(); exit_shogun(); return 0; }
/* * 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) 2011 Heiko Strathmann * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/evaluation/CrossValidation.h> #include <shogun/evaluation/ContingencyTableEvaluation.h> #include <shogun/evaluation/StratifiedCrossValidationSplitting.h> #include <shogun/modelselection/GridSearchModelSelection.h> #include <shogun/modelselection/ModelSelectionParameters.h> #include <shogun/modelselection/ParameterCombination.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/features/DenseFeatures.h> #include <shogun/clustering/KMeans.h> #include <shogun/distance/EuclideanDistance.h> #include <shogun/distance/MinkowskiMetric.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, &print_message, &print_message); int32_t num_clusters=4; int32_t num_features=11; int32_t dim_features=3; int32_t num_vectors_per_cluster=5; float64_t cluster_std_dev=2.0; /* build random cluster centers */ SGMatrix<float64_t> cluster_centers(dim_features, num_clusters); SGVector<float64_t>::random_vector(cluster_centers.matrix, dim_features*num_clusters, -10.0, 10.0); SGMatrix<float64_t>::display_matrix(cluster_centers.matrix, cluster_centers.num_rows, cluster_centers.num_cols, "cluster centers"); /* create data around clusters */ SGMatrix<float64_t> data(dim_features, num_clusters*num_vectors_per_cluster); for (index_t i=0; i<num_clusters; ++i) { for (index_t j=0; j<dim_features; ++j) { for (index_t k=0; k<num_vectors_per_cluster; ++k) { index_t idx=i*dim_features*num_vectors_per_cluster; idx+=j; idx+=k*dim_features; float64_t entry=cluster_centers.matrix[i*dim_features+j]; data.matrix[idx]=CMath::normal_random(entry, cluster_std_dev); } } } /* create features, SG_REF to avoid deletion */ CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t> (); features->set_feature_matrix(data); SG_REF(features); /* create labels for cluster centers */ CMulticlassLabels* labels=new CMulticlassLabels(num_features); for (index_t i=0; i<num_features; ++i) labels->set_label(i, i%2==0 ? 0 : 1); /* create distance */ CEuclideanDistance* distance=new CEuclideanDistance(features, features); /* create distance machine */ CKMeans* clustering=new CKMeans(num_clusters, distance); clustering->train(features); /* build clusters */ CMulticlassLabels* result=CLabelsFactory::to_multiclass(clustering->apply()); for (index_t i=0; i<result->get_num_labels(); ++i) SG_SPRINT("cluster index of vector %i: %f\n", i, result->get_label(i)); /* print cluster centers */ CDenseFeatures<float64_t>* centers= (CDenseFeatures<float64_t>*)distance->get_lhs(); SGMatrix<float64_t> centers_matrix=centers->get_feature_matrix(); SGMatrix<float64_t>::display_matrix(centers_matrix.matrix, centers_matrix.num_rows, centers_matrix.num_cols, "learned centers"); SGMatrix<float64_t>::display_matrix(cluster_centers.matrix, cluster_centers.num_rows, cluster_centers.num_cols, "real centers"); /* clean up */ SG_UNREF(result); SG_UNREF(centers); SG_UNREF(clustering); SG_UNREF(labels); SG_UNREF(features); exit_shogun(); return 0; }
/* * 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) 2011 Sergey Lisitsyn * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/DiffusionMaps.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun(); int N = 100; int dim = 3; float64_t* matrix = new double[N*dim]; for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures<double>* features = new CDenseFeatures<double>(SGMatrix<double>(matrix,dim,N)); SG_REF(features); CDiffusionMaps* dmaps = new CDiffusionMaps(); dmaps->set_target_dim(2); dmaps->set_t(10); dmaps->parallel->set_num_threads(4); CDenseFeatures<double>* embedding = dmaps->embed(features); SG_UNREF(embedding); SG_UNREF(dmaps); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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) 2013 Sergey Lisitsyn */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/FactorAnalysis.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun(); int N = 100; int dim = 3; float64_t* matrix = new double[N*dim]; for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures<double>* features = new CDenseFeatures<double>(SGMatrix<double>(matrix,dim,N)); SG_REF(features); CFactorAnalysis* fa = new CFactorAnalysis(); CDenseFeatures<double>* embedding = fa->embed(features); SG_UNREF(embedding); SG_UNREF(fa); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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) 2011 Sergey Lisitsyn * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/HessianLocallyLinearEmbedding.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun(); int N = 100; int dim = 3; float64_t* matrix = new double[N*dim]; for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures<double>* features = new CDenseFeatures<double>(SGMatrix<double>(matrix,dim,N)); SG_REF(features); CHessianLocallyLinearEmbedding* hlle = new CHessianLocallyLinearEmbedding(); hlle->set_target_dim(2); hlle->set_k(8); hlle->parallel->set_num_threads(4); CDenseFeatures<double>* embedding = hlle->embed(features); SG_UNREF(embedding); SG_UNREF(hlle); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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) 2011 Sergey Lisitsyn * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/Isomap.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun(); int N = 100; int dim = 3; float64_t* matrix = new double[N*dim]; for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures<double>* features = new CDenseFeatures<double>(SGMatrix<double>(matrix,dim,N)); SG_REF(features); CIsomap* isomap = new CIsomap(); isomap->set_target_dim(2); isomap->set_landmark(false); isomap->set_k(4); isomap->parallel->set_num_threads(4); CDenseFeatures<double>* embedding = isomap->embed(features); SG_UNREF(embedding); SG_UNREF(isomap); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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) 2013 Kevin Hughes * * Thanks to Andreas Ziehe */ #include <shogun/base/init.h> #include <shogun/lib/common.h> #include <iostream> using namespace shogun; #ifdef HAVE_EIGEN3 #include <shogun/features/DenseFeatures.h> #include <shogun/mathematics/Math.h> #include <shogun/mathematics/eigen3.h> #include <shogun/converter/ica/Jade.h> #include <shogun/evaluation/ica/PermutationMatrix.h> #include <shogun/evaluation/ica/AmariIndex.h> using namespace Eigen; void test() { // Generate sample data CMath::init_random(0); int n_samples = 2000; VectorXd time(n_samples, true); time.setLinSpaced(n_samples,0,10); // Source Signals MatrixXd S(2,n_samples); for(int i = 0; i < n_samples; i++) { // Sin wave S(0,i) = sin(2*time[i]); S(0,i) += 0.2*CMath::randn_double(); // Square wave S(1,i) = sin(3*time[i]) < 0 ? -1 : 1; S(1,i) += 0.2*CMath::randn_double(); } // Standardize data VectorXd avg = S.rowwise().sum() / n_samples; VectorXd std = ((S.colwise() - avg).array().pow(2).rowwise().sum() / n_samples).array().sqrt(); for(int i = 0; i < n_samples; i++) S.col(i) = S.col(i).cwiseQuotient(std); // Mixing Matrix SGMatrix<float64_t> mixing_matrix(2,2); Map<MatrixXd> A(mixing_matrix.matrix,2,2); A(0,0) = 1; A(0,1) = 0.5; A(1,0) = 0.5; A(1,1) = 1; std::cout << "Mixing Matrix:" << std::endl; std::cout << A << std::endl << std::endl; // Mix signals SGMatrix<float64_t> X(2,n_samples); Map<MatrixXd> EX(X.matrix,2,n_samples); EX = A * S; CDenseFeatures< float64_t >* mixed_signals = new CDenseFeatures< float64_t >(X); // Separate CJade* jade = new CJade(); SG_REF(jade); CFeatures* signals = jade->apply(mixed_signals); SG_REF(signals); // Close to a permutation matrix (with random scales) Map<MatrixXd> EA(jade->get_mixing_matrix().matrix,2,2); std::cout << "Estimated Mixing Matrix:" << std::endl; std::cout << EA << std::endl << std::endl; SGMatrix<float64_t> P(2,2); Eigen::Map<MatrixXd> EP(P.matrix,2,2); EP = EA.inverse() * A; bool isperm = is_permutation_matrix(P); std::cout << "EA^-1 * A == Permuatation Matrix is: " << isperm << std::endl; float64_t amari_err = amari_index(jade->get_mixing_matrix(), mixing_matrix, true); std::cout << "Amari Error: " << amari_err << std::endl; SG_UNREF(jade); SG_UNREF(mixed_signals); SG_UNREF(signals); return; } #endif // HAVE_EIGEN3 int main(int argc, char ** argv) { init_shogun_with_defaults(); #ifdef HAVE_EIGEN3 test(); #endif // HAVE_EIGEN3 exit_shogun(); return 0; }
/* * 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) 2011 Sergey Lisitsyn * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/KernelLocallyLinearEmbedding.h> #include <shogun/kernel/LinearKernel.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun(); int N = 100; int dim = 3; float64_t* matrix = new double[N*dim]; for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures<double>* features = new CDenseFeatures<double>(SGMatrix<double>(matrix,dim,N)); SG_REF(features); CKernelLocallyLinearEmbedding* klle = new CKernelLocallyLinearEmbedding(); CKernel* kernel = new CLinearKernel(); klle->set_target_dim(2); klle->set_k(4); klle->set_kernel(kernel); klle->parallel->set_num_threads(4); CDenseFeatures<double>* embedding = klle->embed(features); SG_UNREF(embedding); SG_UNREF(klle); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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) 2011 Sergey Lisitsyn * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/LaplacianEigenmaps.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun(); int N = 100; int dim = 3; float64_t* matrix = new double[N*dim]; for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures<double>* features = new CDenseFeatures<double>(SGMatrix<double>(matrix,dim,N)); SG_REF(features); CLaplacianEigenmaps* lem = new CLaplacianEigenmaps(); lem->set_target_dim(2); lem->set_k(10); lem->parallel->set_num_threads(4); CDenseFeatures<double>* embedding = lem->embed(features); SG_UNREF(embedding); SG_UNREF(lem); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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) 2011 Sergey Lisitsyn * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/LinearLocalTangentSpaceAlignment.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun(); int N = 100; int dim = 3; float64_t* matrix = new double[N*dim]; for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures<double>* features = new CDenseFeatures<double>(SGMatrix<double>(matrix,dim,N)); SG_REF(features); CLinearLocalTangentSpaceAlignment* lltsa = new CLinearLocalTangentSpaceAlignment(); lltsa->set_target_dim(2); lltsa->set_k(4); lltsa->parallel->set_num_threads(4); CDenseFeatures<double>* embedding = lltsa->embed(features); SG_UNREF(embedding); SG_UNREF(lltsa); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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) 2011 Sergey Lisitsyn * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/LocalityPreservingProjections.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun_with_defaults(); int N = 100; int dim = 3; float64_t* matrix = new double[N*dim]; for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures<double>* features = new CDenseFeatures<double>(SGMatrix<double>(matrix,dim,N)); SG_REF(features); CLocalityPreservingProjections* lpp = new CLocalityPreservingProjections(); lpp->set_target_dim(2); lpp->set_k(10); lpp->parallel->set_num_threads(4); CDenseFeatures<double>* embedding = lpp->embed(features); SG_UNREF(embedding); SG_UNREF(lpp); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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) 2011 Sergey Lisitsyn * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/LocallyLinearEmbedding.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun_with_defaults(); int N = 100; int dim = 3; float64_t* matrix = new double[N*dim]; for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures<double>* features = new CDenseFeatures<double>(SGMatrix<double>(matrix,dim,N)); SG_REF(features); CLocallyLinearEmbedding* lle = new CLocallyLinearEmbedding(); lle->set_target_dim(2); lle->set_k(4); lle->parallel->set_num_threads(4); CDenseFeatures<double>* embedding = lle->embed(features); SG_UNREF(embedding); SG_UNREF(lle); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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) 2011 Sergey Lisitsyn * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/LocalTangentSpaceAlignment.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun(); int N = 100; int dim = 3; float64_t* matrix = new double[N*dim]; for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures<double>* features = new CDenseFeatures<double>(SGMatrix<double>(matrix,dim,N)); SG_REF(features); CLocalTangentSpaceAlignment* ltsa = new CLocalTangentSpaceAlignment(); ltsa->set_target_dim(2); ltsa->set_k(4); ltsa->parallel->set_num_threads(4); CDenseFeatures<double>* embedding = ltsa->embed(features); SG_UNREF(embedding); SG_UNREF(ltsa); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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) 2011 Sergey Lisitsyn * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/MultidimensionalScaling.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun(); int N = 100; int dim = 3; float64_t* matrix = new double[N*dim]; for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures<double>* features = new CDenseFeatures<double>(SGMatrix<double>(matrix,dim,N)); SG_REF(features); CMultidimensionalScaling* mds = new CMultidimensionalScaling(); mds->set_target_dim(2); mds->set_landmark(true); mds->parallel->set_num_threads(4); CDenseFeatures<double>* embedding = mds->embed(features); SG_UNREF(embedding); SG_UNREF(mds); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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) 2011 Sergey Lisitsyn * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/NeighborhoodPreservingEmbedding.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun(); int N = 100; int dim = 3; float64_t* matrix = new double[N*dim]; for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures<double>* features = new CDenseFeatures<double>(SGMatrix<double>(matrix,dim,N)); SG_REF(features); CNeighborhoodPreservingEmbedding* npe = new CNeighborhoodPreservingEmbedding(); npe->set_target_dim(2); npe->set_k(15); npe->parallel->set_num_threads(4); CDenseFeatures<double>* embedding = npe->embed(features); SG_UNREF(embedding); SG_UNREF(npe); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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 Fernando José Iglesias García * Copyright (C) 2012 Fernando José Iglesias García */ #include <shogun/lib/config.h> #ifdef HAVE_EIGEN3 #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/converter/StochasticProximityEmbedding.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main() { init_shogun_with_defaults(); int N = 100; int dim = 3; // Generate toy data SGMatrix< float64_t > matrix(dim, N); for (int i=0; i<N*dim; i++) matrix[i] = CMath::sin((i/float64_t(N*dim))*3.14); CDenseFeatures< float64_t >* features = new CDenseFeatures<float64_t>(matrix); SG_REF(features); // Create embedding and set parameters for global strategy CStochasticProximityEmbedding* spe = new CStochasticProximityEmbedding(); spe->set_target_dim(2); spe->set_strategy(SPE_GLOBAL); spe->set_nupdates(40); SG_REF(spe); // Apply embedding with global strategy CDenseFeatures< float64_t >* embedding = spe->embed(features); SG_REF(embedding); // Set parameters for local strategy spe->set_strategy(SPE_LOCAL); spe->set_k(12); // Apply embedding with local strategy SG_UNREF(embedding); embedding = spe->embed(features); SG_REF(embedding); // Free memory SG_UNREF(embedding); SG_UNREF(spe); SG_UNREF(features); exit_shogun(); return 0; } #else int main(int argc, char **argv) { return 0; } #endif
/* * 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 Heiko Strathmann * Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/labels/BinaryLabels.h> #include <shogun/kernel/GaussianKernel.h> #include <shogun/classifier/svm/LibSVM.h> #include <shogun/evaluation/CrossValidation.h> #include <shogun/evaluation/StratifiedCrossValidationSplitting.h> #include <shogun/evaluation/ContingencyTableEvaluation.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } void test_cross_validation() { /* data matrix dimensions */ index_t num_vectors=40; index_t num_features=5; /* data means -1, 1 in all components, std deviation of 3 */ SGVector<float64_t> mean_1(num_features); SGVector<float64_t> mean_2(num_features); SGVector<float64_t>::fill_vector(mean_1.vector, mean_1.vlen, -1.0); SGVector<float64_t>::fill_vector(mean_2.vector, mean_2.vlen, 1.0); float64_t sigma=3; SGVector<float64_t>::display_vector(mean_1.vector, mean_1.vlen, "mean 1"); SGVector<float64_t>::display_vector(mean_2.vector, mean_2.vlen, "mean 2"); /* fill data matrix around mean */ SGMatrix<float64_t> train_dat(num_features, num_vectors); for (index_t i=0; i<num_vectors; ++i) { for (index_t j=0; j<num_features; ++j) { float64_t mean=i<num_vectors/2 ? mean_1.vector[0] : mean_2.vector[0]; train_dat.matrix[i*num_features+j]=CMath::normal_random(mean, sigma); } } /* training features */ CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(train_dat); SG_REF(features); /* training labels +/- 1 for each cluster */ SGVector<float64_t> lab(num_vectors); for (index_t i=0; i<num_vectors; ++i) lab.vector[i]=i<num_vectors/2 ? -1.0 : 1.0; CBinaryLabels* labels=new CBinaryLabels(lab); /* gaussian kernel */ int32_t kernel_cache=100; int32_t width=10; CGaussianKernel* kernel=new CGaussianKernel(kernel_cache, width); kernel->init(features, features); /* create svm via libsvm */ float64_t svm_C=10; float64_t svm_eps=0.0001; CLibSVM* svm=new CLibSVM(svm_C, kernel, labels); svm->set_epsilon(svm_eps); /* train and output */ svm->train(features); CBinaryLabels* output=CLabelsFactory::to_binary(svm->apply(features)); for (index_t i=0; i<num_vectors; ++i) SG_SPRINT("i=%d, class=%f,\n", i, output->get_label(i)); /* evaluation criterion */ CContingencyTableEvaluation* eval_crit= new CContingencyTableEvaluation(ACCURACY); /* evaluate training error */ float64_t eval_result=eval_crit->evaluate(output, labels); SG_SPRINT("training error: %f\n", eval_result); SG_UNREF(output); /* assert that regression "works". this is not guaranteed to always work * but should be a really coarse check to see if everything is going * approx. right */ ASSERT(eval_result<2); /* splitting strategy */ index_t n_folds=5; CStratifiedCrossValidationSplitting* splitting= new CStratifiedCrossValidationSplitting(labels, n_folds); /* cross validation instance, 10 runs, 95% confidence interval */ CCrossValidation* cross=new CCrossValidation(svm, features, labels, splitting, eval_crit); cross->set_num_runs(10); cross->set_conf_int_alpha(0.05); /* actual evaluation */ CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); if (result->get_result_type() != CROSSVALIDATION_RESULT) SG_SERROR("Evaluation result is not of type CrossValidationResult!"); result->print_result(); /* clean up */ SG_UNREF(result); SG_UNREF(cross); SG_UNREF(features); } int main(int argc, char **argv) { init_shogun(&print_message, &print_message, &print_message); sg_io->set_loglevel(MSG_DEBUG); test_cross_validation(); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann * Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society * Written (W) 2013 Saurabh Mahindre */ #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/multiclass/KNN.h> #include <shogun/io/SGIO.h> #include <shogun/io/CSVFile.h> #include <shogun/evaluation/CrossValidation.h> #include <shogun/evaluation/StratifiedCrossValidationSplitting.h> #include <shogun/evaluation/MulticlassAccuracy.h> #include <shogun/distance/EuclideanDistance.h> using namespace shogun; // Prepare to read a file for the training data const char fname_feats[] = "../data/fm_train_real.dat"; const char fname_labels[] = "../data/label_train_multiclass.dat"; void test_cross_validation() { index_t k =4; /* dense features from matrix */ CCSVFile* feature_file = new CCSVFile(fname_feats); SGMatrix<float64_t> mat=SGMatrix<float64_t>(); mat.load(feature_file); SG_UNREF(feature_file); CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat); SG_REF(features); /* labels from vector */ CCSVFile* label_file = new CCSVFile(fname_labels); SGVector<float64_t> label_vec; label_vec.load(label_file); SG_UNREF(label_file); CMulticlassLabels* labels=new CMulticlassLabels(label_vec); SG_REF(labels); /* create knn */ CEuclideanDistance* distance = new CEuclideanDistance(features, features); CKNN* knn=new CKNN (k, distance, labels); /* train and output */ knn->train(features); CMulticlassLabels* output=CLabelsFactory::to_multiclass(knn->apply(features)); for (index_t i=0; i<features->get_num_vectors(); ++i) SG_SPRINT("i=%d, class=%f,\n", i, output->get_label(i)); /* evaluation criterion */ CMulticlassAccuracy* eval_crit = new CMulticlassAccuracy (); /* evaluate training error */ float64_t eval_result=eval_crit->evaluate(output, labels); SG_SPRINT("training accuracy: %f\n", eval_result); SG_UNREF(output); /* assert that regression "works". this is not guaranteed to always work * but should be a really coarse check to see if everything is going * approx. right */ ASSERT(eval_result<2); /* splitting strategy */ index_t n_folds=5; CStratifiedCrossValidationSplitting* splitting= new CStratifiedCrossValidationSplitting(labels, n_folds); /* cross validation instance, 10 runs, 95% confidence interval */ CCrossValidation* cross=new CCrossValidation(knn, features, labels, splitting, eval_crit); cross->set_num_runs(1); cross->set_conf_int_alpha(0.05); /* actual evaluation */ CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); if (result->get_result_type() != CROSSVALIDATION_RESULT) SG_SERROR("Evaluation result is not of type CCrossValidationResult!"); result->print_result(); /* clean up */ SG_UNREF(result); SG_UNREF(cross); SG_UNREF(features); SG_UNREF(labels); } int main(int argc, char **argv) { init_shogun_with_defaults(); sg_io->set_loglevel(MSG_DEBUG); test_cross_validation(); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann */ #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/labels/BinaryLabels.h> #include <shogun/kernel/GaussianKernel.h> #include <shogun/classifier/svm/LibSVM.h> #include <shogun/evaluation/CrossValidation.h> #include <shogun/evaluation/StratifiedCrossValidationSplitting.h> #include <shogun/evaluation/ContingencyTableEvaluation.h> #include <shogun/lib/Time.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } void test_cross_validation() { /* data matrix dimensions */ index_t num_vectors=50; index_t num_features=5; /* data means -1, 1 in all components, std deviation of sigma */ SGVector<float64_t> mean_1(num_features); SGVector<float64_t> mean_2(num_features); SGVector<float64_t>::fill_vector(mean_1.vector, mean_1.vlen, -1.0); SGVector<float64_t>::fill_vector(mean_2.vector, mean_2.vlen, 1.0); float64_t sigma=1.5; /* fill data matrix around mean */ SGMatrix<float64_t> train_dat(num_features, num_vectors); for (index_t i=0; i<num_vectors; ++i) { for (index_t j=0; j<num_features; ++j) { float64_t mean=i<num_vectors/2 ? mean_1.vector[0] : mean_2.vector[0]; train_dat.matrix[i*num_features+j]=CMath::normal_random(mean, sigma); } } /* training features */ CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(train_dat); SG_REF(features); /* training labels +/- 1 for each cluster */ SGVector<float64_t> lab(num_vectors); for (index_t i=0; i<num_vectors; ++i) lab.vector[i]=i<num_vectors/2 ? -1.0 : 1.0; CBinaryLabels* labels=new CBinaryLabels(lab); /* gaussian kernel */ CGaussianKernel* kernel=new CGaussianKernel(); kernel->set_width(10); kernel->init(features, features); /* create svm via libsvm */ float64_t svm_C=1; float64_t svm_eps=0.0001; CSVM* svm=new CLibSVM(svm_C, kernel, labels); svm->set_epsilon(svm_eps); /* train and output the normal way */ SG_SPRINT("starting normal training\n"); svm->train(features); CBinaryLabels* output=CLabelsFactory::to_binary(svm->apply(features)); /* evaluation criterion */ CContingencyTableEvaluation* eval_crit= new CContingencyTableEvaluation(ACCURACY); /* evaluate training error */ float64_t eval_result=eval_crit->evaluate(output, labels); SG_SPRINT("training accuracy: %f\n", eval_result); SG_UNREF(output); /* assert that regression "works". this is not guaranteed to always work * but should be a really coarse check to see if everything is going * approx. right */ ASSERT(eval_result<2); /* splitting strategy */ index_t n_folds=3; CStratifiedCrossValidationSplitting* splitting= new CStratifiedCrossValidationSplitting(labels, n_folds); /* cross validation instance, 10 runs, 95% confidence interval */ CCrossValidation* cross=new CCrossValidation(svm, features, labels, splitting, eval_crit); cross->set_num_runs(5); cross->set_conf_int_alpha(0.05); CCrossValidationResult* tmp; /* no locking */ index_t repetitions=5; SG_SPRINT("unlocked x-val\n"); kernel->init(features, features); cross->set_autolock(false); CTime time; time.start(); for (index_t i=0; i<repetitions; ++i) { tmp = (CCrossValidationResult*)cross->evaluate(); SG_UNREF(tmp); } time.stop(); SG_SPRINT("%f sec\n", time.cur_time_diff()); /* auto_locking in every iteration of this loop (better, not so nice) */ SG_SPRINT("locked in every iteration x-val\n"); cross->set_autolock(true); time.start(); for (index_t i=0; i<repetitions; ++i) { tmp = (CCrossValidationResult*)cross->evaluate(); SG_UNREF(tmp); } time.stop(); SG_SPRINT("%f sec\n", time.cur_time_diff()); /* lock once before, (no locking/unlocking in this loop) */ svm->data_lock(labels, features); SG_SPRINT("locked x-val\n"); time.start(); for (index_t i=0; i<repetitions; ++i) { tmp = (CCrossValidationResult*)cross->evaluate(); SG_UNREF(tmp); } time.stop(); SG_SPRINT("%f sec\n", time.cur_time_diff()); /* clean up */ SG_UNREF(cross); SG_UNREF(features); } int main(int argc, char **argv) { init_shogun(&print_message, &print_message, &print_message); test_cross_validation(); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann */ #include <shogun/base/init.h> #include <shogun/kernel/GaussianKernel.h> #include <shogun/kernel/CombinedKernel.h> #include <shogun/labels/BinaryLabels.h> #include <shogun/features/DenseFeatures.h> #include <shogun/classifier/mkl/MKLClassification.h> #include <shogun/classifier/svm/LibSVM.h> #include <shogun/evaluation/CrossValidation.h> #include <shogun/evaluation/CrossValidationPrintOutput.h> #include <shogun/evaluation/CrossValidationMKLStorage.h> #include <shogun/evaluation/StratifiedCrossValidationSplitting.h> #include <shogun/evaluation/ContingencyTableEvaluation.h> #include <shogun/mathematics/Statistics.h> using namespace shogun; void gen_rand_data(SGVector<float64_t> lab, SGMatrix<float64_t> feat, float64_t dist) { index_t dims=feat.num_rows; index_t num=lab.vlen; for (int32_t i=0; i<num; i++) { if (i<num/2) { lab[i]=-1.0; 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(j, i)=CMath::random(0.0, 1.0)-dist; } } lab.display_vector("lab"); feat.display_matrix("feat"); } void test_mkl_cross_validation() { /* generate random data */ index_t num=10; index_t dims=2; float64_t dist=0.5; SGVector<float64_t> lab(num); SGMatrix<float64_t> feat(dims, num); gen_rand_data(lab, feat, dist); /*create train labels */ CLabels* labels=new CBinaryLabels(lab); /* create train features */ CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(); features->set_feature_matrix(feat); SG_REF(features); /* create combined features */ CCombinedFeatures* comb_features=new CCombinedFeatures(); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); SG_REF(comb_features); /* create multiple gaussian kernels */ CCombinedKernel* kernel=new CCombinedKernel(); kernel->append_kernel(new CGaussianKernel(10, 0.1)); kernel->append_kernel(new CGaussianKernel(10, 1)); kernel->append_kernel(new CGaussianKernel(10, 2)); kernel->init(comb_features, comb_features); SG_REF(kernel); /* create mkl using libsvm, due to a mem-bug, interleaved is not possible */ CMKLClassification* svm=new CMKLClassification(new CLibSVM()); svm->set_interleaved_optimization_enabled(false); svm->set_kernel(kernel); SG_REF(svm); /* create cross-validation instance */ index_t num_folds=3; CSplittingStrategy* split=new CStratifiedCrossValidationSplitting(labels, num_folds); CEvaluation* eval=new CContingencyTableEvaluation(ACCURACY); CCrossValidation* cross=new CCrossValidation(svm, comb_features, labels, split, eval, false); /* add print output listener and mkl storage listener */ cross->add_cross_validation_output(new CCrossValidationPrintOutput()); CCrossValidationMKLStorage* mkl_storage=new CCrossValidationMKLStorage(); cross->add_cross_validation_output(mkl_storage); /* perform cross-validation, this will print loads of information * (caused by the CCrossValidationPrintOutput instance attached to it) */ CEvaluationResult* result=cross->evaluate(); /* print mkl weights */ SGMatrix<float64_t> weights=mkl_storage->get_mkl_weights(); weights.display_matrix("mkl weights"); /* print mean and variance of each kernel weight. These could for example * been used to compute confidence intervals */ CStatistics::matrix_mean(weights, false).display_vector("mean per kernel"); CStatistics::matrix_variance(weights, false).display_vector("variance per kernel"); CStatistics::matrix_std_deviation(weights, false).display_vector("std-dev per kernel"); SG_UNREF(result); /* again for two runs */ cross->set_num_runs(2); result=cross->evaluate(); /* print mkl weights */ weights=mkl_storage->get_mkl_weights(); weights.display_matrix("mkl weights"); /* print mean and variance of each kernel weight. These could for example * been used to compute confidence intervals */ CStatistics::matrix_mean(weights, false).display_vector("mean per kernel"); CStatistics::matrix_variance(weights, false).display_vector("variance per kernel"); CStatistics::matrix_std_deviation(weights, false).display_vector("std-dev per kernel"); /* clean up */ SG_UNREF(result); SG_UNREF(cross); SG_UNREF(kernel); SG_UNREF(features); SG_UNREF(comb_features); SG_UNREF(svm); } int main() { init_shogun_with_defaults(); // sg_io->set_loglevel(MSG_DEBUG); test_mkl_cross_validation(); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann * Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/multiclass/MulticlassLibLinear.h> #include <shogun/io/SGIO.h> #include <shogun/io/CSVFile.h> #include <shogun/evaluation/CrossValidation.h> #include <shogun/evaluation/StratifiedCrossValidationSplitting.h> #include <shogun/evaluation/MulticlassAccuracy.h> using namespace shogun; // Prepare to read a file for the training data const char fname_feats[] = "../data/fm_train_real.dat"; const char fname_labels[] = "../data/label_train_multiclass.dat"; void test_cross_validation() { /* dense features from matrix */ CCSVFile* feature_file = new CCSVFile(fname_feats); SGMatrix<float64_t> mat=SGMatrix<float64_t>(); mat.load(feature_file); SG_UNREF(feature_file); CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat); SG_REF(features); /* labels from vector */ CCSVFile* label_file = new CCSVFile(fname_labels); SGVector<float64_t> label_vec; label_vec.load(label_file); SG_UNREF(label_file); CMulticlassLabels* labels=new CMulticlassLabels(label_vec); SG_REF(labels); /* create svm via libsvm */ float64_t svm_C=10; float64_t svm_eps=0.0001; CMulticlassLibLinear* svm=new CMulticlassLibLinear(svm_C, features, labels); svm->set_epsilon(svm_eps); /* train and output */ svm->train(features); CMulticlassLabels* output=CLabelsFactory::to_multiclass(svm->apply(features)); for (index_t i=0; i<features->get_num_vectors(); ++i) SG_SPRINT("i=%d, class=%f,\n", i, output->get_label(i)); /* evaluation criterion */ CMulticlassAccuracy* eval_crit = new CMulticlassAccuracy (); /* evaluate training error */ float64_t eval_result=eval_crit->evaluate(output, labels); SG_SPRINT("training accuracy: %f\n", eval_result); SG_UNREF(output); /* assert that regression "works". this is not guaranteed to always work * but should be a really coarse check to see if everything is going * approx. right */ ASSERT(eval_result<2); /* splitting strategy */ index_t n_folds=5; CStratifiedCrossValidationSplitting* splitting= new CStratifiedCrossValidationSplitting(labels, n_folds); /* cross validation instance, 10 runs, 95% confidence interval */ CCrossValidation* cross=new CCrossValidation(svm, features, labels, splitting, eval_crit); cross->set_num_runs(1); cross->set_conf_int_alpha(0.05); /* actual evaluation */ CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); if (result->get_result_type() != CROSSVALIDATION_RESULT) SG_SERROR("Evaluation result is not of type CCrossValidationResult!"); result->print_result(); /* clean up */ SG_UNREF(result); SG_UNREF(cross); SG_UNREF(features); SG_UNREF(labels); } int main(int argc, char **argv) { init_shogun_with_defaults(); sg_io->set_loglevel(MSG_DEBUG); test_cross_validation(); exit_shogun(); return 0; }
/* * 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 yoo, thereisnoknife@gmail.com * Written (W) 2012 Heiko Strathmann */ #include <shogun/base/init.h> #include <shogun/io/CSVFile.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/features/DenseFeatures.h> #include <shogun/kernel/GaussianKernel.h> #include <shogun/kernel/LinearKernel.h> #include <shogun/kernel/PolyKernel.h> #include <shogun/kernel/CombinedKernel.h> #include <shogun/classifier/mkl/MKLMulticlass.h> #include <shogun/evaluation/StratifiedCrossValidationSplitting.h> #include <shogun/evaluation/CrossValidation.h> #include <shogun/evaluation/MulticlassAccuracy.h> using namespace shogun; /* cross-validation instances */ const index_t n_folds=2; const index_t n_runs=2; /* file data */ const char fname_feats[]="../data/fm_train_real.dat"; const char fname_labels[]="../data/label_train_multiclass.dat"; void test_multiclass_mkl_cv() { /* init random number generator for reproducible results of cross-validation in the light of ASSERT(result->mean>0.81); some lines down below */ sg_rand->set_seed(12); /* dense features from matrix */ CCSVFile* feature_file = new CCSVFile(fname_feats); SGMatrix<float64_t> mat=SGMatrix<float64_t>(); mat.load(feature_file); SG_UNREF(feature_file); CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(mat); SG_REF(features); /* labels from vector */ CCSVFile* label_file = new CCSVFile(fname_labels); SGVector<float64_t> label_vec; label_vec.load(label_file); SG_UNREF(label_file); CMulticlassLabels* labels=new CMulticlassLabels(label_vec); SG_REF(labels); /* combined features and kernel */ CCombinedFeatures *cfeats=new CCombinedFeatures(); CCombinedKernel *cker=new CCombinedKernel(); SG_REF(cfeats); SG_REF(cker); /** 1st kernel: gaussian */ cfeats->append_feature_obj(features); cker->append_kernel(new CGaussianKernel(features, features, 1.2, 10)); /** 2nd kernel: linear */ cfeats->append_feature_obj(features); cker->append_kernel(new CLinearKernel(features, features)); /** 3rd kernel: poly */ cfeats->append_feature_obj(features); cker->append_kernel(new CPolyKernel(features, features, 2, true, 10)); cker->init(cfeats, cfeats); /* create mkl instance */ CMKLMulticlass* mkl=new CMKLMulticlass(1.2, cker, labels); SG_REF(mkl); mkl->set_epsilon(0.00001); mkl->parallel->set_num_threads(1); mkl->set_mkl_epsilon(0.001); mkl->set_mkl_norm(1.5); /* train to see weights */ mkl->train(); cker->get_subkernel_weights().display_vector("weights"); CMulticlassAccuracy* eval_crit=new CMulticlassAccuracy(); CStratifiedCrossValidationSplitting* splitting= new CStratifiedCrossValidationSplitting(labels, n_folds); CCrossValidation *cross=new CCrossValidation(mkl, cfeats, labels, splitting, eval_crit); cross->set_autolock(false); cross->set_num_runs(n_runs); cross->set_conf_int_alpha(0.05); /* perform x-val and print result */ CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); SG_SPRINT("mean of %d %d-fold x-val runs: %f\n", n_runs, n_folds, result->mean); /* assert high accuracy */ ASSERT(result->mean>0.81); /* clean up */ SG_UNREF(features); SG_UNREF(labels); SG_UNREF(cfeats); SG_UNREF(cker); SG_UNREF(mkl); SG_UNREF(cross); SG_UNREF(result); } int main(int argc, char** argv){ shogun::init_shogun_with_defaults(); // sg_io->set_loglevel(MSG_DEBUG); /* performs cross-validation on a multi-class mkl machine */ test_multiclass_mkl_cv(); exit_shogun(); }
/* * 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 Heiko Strathmann * Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/labels/RegressionLabels.h> #include <shogun/kernel/LinearKernel.h> #include <shogun/regression/KernelRidgeRegression.h> #include <shogun/evaluation/CrossValidation.h> #include <shogun/evaluation/CrossValidationSplitting.h> #include <shogun/evaluation/MeanSquaredError.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } void test_cross_validation() { #ifdef HAVE_LAPACK /* data matrix dimensions */ index_t num_vectors=100; index_t num_features=1; /* training label data */ SGVector<float64_t> lab(num_vectors); /* fill data matrix and labels */ SGMatrix<float64_t> train_dat(num_features, num_vectors); SGVector<float64_t>::range_fill_vector(train_dat.matrix, num_vectors); for (index_t i=0; i<num_vectors; ++i) { /* labels are linear plus noise */ lab.vector[i]=i+CMath::normal_random(0, 1.0); } /* training features */ CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(train_dat); SG_REF(features); /* training labels */ CRegressionLabels* labels=new CRegressionLabels(lab); /* kernel */ CLinearKernel* kernel=new CLinearKernel(); kernel->init(features, features); /* kernel ridge regression*/ float64_t tau=0.0001; CKernelRidgeRegression* krr=new CKernelRidgeRegression(tau, kernel, labels); /* evaluation criterion */ CMeanSquaredError* eval_crit= new CMeanSquaredError(); /* train and output */ krr->train(features); CRegressionLabels* output= CLabelsFactory::to_regression(krr->apply()); for (index_t i=0; i<num_vectors; ++i) { SG_SPRINT("x=%f, train=%f, predict=%f\n", train_dat.matrix[i], labels->get_label(i), output->get_label(i)); } /* evaluate training error */ float64_t eval_result=eval_crit->evaluate(output, labels); SG_SPRINT("training error: %f\n", eval_result); SG_UNREF(output); /* assert that regression "works". this is not guaranteed to always work * but should be a really coarse check to see if everything is going * approx. right */ ASSERT(eval_result<2); /* splitting strategy */ index_t n_folds=5; CCrossValidationSplitting* splitting= new CCrossValidationSplitting(labels, n_folds); /* cross validation instance, 10 runs, 95% confidence interval */ CCrossValidation* cross=new CCrossValidation(krr, features, labels, splitting, eval_crit); cross->set_num_runs(100); cross->set_conf_int_alpha(0.05); /* actual evaluation */ CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); if (result->get_result_type() != CROSSVALIDATION_RESULT) SG_SERROR("Evaluation result is not of type CCrossValidationResult!"); SG_SPRINT("cross_validation estimate:\n"); result->print_result(); /* same crude assertion as for above evaluation */ ASSERT(result->mean<2); /* clean up */ SG_UNREF(result); SG_UNREF(cross); SG_UNREF(features); #endif /* HAVE_LAPACK */ } int main(int argc, char **argv) { init_shogun(&print_message, &print_message, &print_message); test_cross_validation(); exit_shogun(); return 0; }
/* * 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) 2011-2012 Heiko Strathmann * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/features/Subset.h> using namespace shogun; void test() { SGMatrix<float64_t> data(3, 10); CDenseFeatures<float64_t>* f=new CDenseFeatures<float64_t>(data); SGVector<float64_t>::range_fill_vector(data.matrix, data.num_cols*data.num_rows, 1.0); SGMatrix<float64_t>::display_matrix(data.matrix, data.num_rows, data.num_cols, "original feature data"); index_t offset_subset=1; SGVector<index_t> feature_subset(8); SGVector<index_t>::range_fill_vector(feature_subset.vector, feature_subset.vlen, offset_subset); SGVector<index_t>::display_vector(feature_subset.vector, feature_subset.vlen, "feature subset"); f->add_subset(feature_subset); SG_SPRINT("feature vectors after setting subset on original data:\n"); for (index_t i=0; i<f->get_num_vectors(); ++i) { SGVector<float64_t> vec=f->get_feature_vector(i); SG_SPRINT("%i: ", i); SGVector<float64_t>::display_vector(vec.vector, vec.vlen); f->free_feature_vector(vec, i); } index_t offset_copy=2; SGVector<index_t> feature_copy_subset(4); SGVector<index_t>::range_fill_vector(feature_copy_subset.vector, feature_copy_subset.vlen, offset_copy); SGVector<index_t>::display_vector(feature_copy_subset.vector, feature_copy_subset.vlen, "indices that are to be copied"); CDenseFeatures<float64_t>* subset_copy= (CDenseFeatures<float64_t>*)f->copy_subset(feature_copy_subset); SGMatrix<float64_t> subset_copy_matrix=subset_copy->get_feature_matrix(); SGMatrix<float64_t>::display_matrix(subset_copy_matrix.matrix, subset_copy_matrix.num_rows, subset_copy_matrix.num_cols, "copy matrix"); index_t num_its=subset_copy_matrix.num_rows*subset_copy_matrix.num_cols; for (index_t i=0; i<num_its; ++i) { index_t idx=i+(offset_copy+offset_subset)*subset_copy_matrix.num_rows; ASSERT(subset_copy_matrix.matrix[i]==data.matrix[idx]); } SG_UNREF(f); SG_UNREF(subset_copy); } int main(int argc, char **argv) { init_shogun_with_defaults(); test(); exit_shogun(); return 0; }
/* * 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) 2011-2012 Heiko Strathmann * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/features/SparseFeatures.h> #include <shogun/features/Subset.h> using namespace shogun; void test() { index_t num_vectors=10; index_t num_dimensions=7; index_t num_features=3; /* create some sparse data */ SGSparseMatrix<float64_t> data=SGSparseMatrix<float64_t>(num_dimensions, num_vectors); for (index_t i=0; i<num_vectors; ++i) { /* put elements only at even indices */ data.sparse_matrix[i]=SGSparseVector<float64_t>(num_features); /* fill */ for (index_t j=0; j<num_features; ++j) { data.sparse_matrix[i].features[j].entry=i+j; data.sparse_matrix[i].features[j].feat_index=3*j; } } CSparseFeatures<float64_t>* f=new CSparseFeatures<float64_t>(data); /* display sparse matrix */ SG_SPRINT("original data\n"); for (index_t i=0; i<num_vectors; ++i) { SG_SPRINT("sparse vector at %i: [", i); for (index_t j=0; j<num_features; ++j) SG_SPRINT("%f, ", data.sparse_matrix[i].features[j].entry); SG_SPRINT("]\n"); } /* indices for a subset */ index_t offset_subset=1; SGVector<index_t> feature_subset(8); SGVector<index_t>::range_fill_vector(feature_subset.vector, feature_subset.vlen, offset_subset); SGVector<index_t>::display_vector(feature_subset.vector, feature_subset.vlen, "feature subset"); /* set subset and print data */ f->add_subset(feature_subset); SG_SPRINT("feature vectors after setting subset on original data:\n"); for (index_t i=0; i<f->get_num_vectors(); ++i) { SGSparseVector<float64_t> vec=f->get_sparse_feature_vector(i); SG_SPRINT("sparse vector at %i: ", i); for (index_t j=0; j<num_features; ++j) SG_SPRINT("%f, ", vec.features[j].entry); SG_SPRINT("]\n"); f->free_sparse_feature_vector(i); } /* indices that are to copy */ index_t offset_copy=2; SGVector<index_t> feature_copy_subset(4); SGVector<index_t>::range_fill_vector(feature_copy_subset.vector, feature_copy_subset.vlen, offset_copy); SGVector<index_t>::display_vector(feature_copy_subset.vector, feature_copy_subset.vlen, "indices that are to be copied"); /* copy a subset of features */ CSparseFeatures<float64_t>* subset_copy= (CSparseFeatures<float64_t>*)f->copy_subset(feature_copy_subset); /* print copied subset */ SG_SPRINT("copied features:\n"); for (index_t i=0; i<subset_copy->get_num_vectors(); ++i) { SGSparseVector<float64_t> vec=subset_copy->get_sparse_feature_vector(i); SG_SPRINT("sparse vector at %i: ", i); for (index_t j=0; j<num_features; ++j) SG_SPRINT("%f, ", vec.features[j].entry); SG_SPRINT("]\n"); subset_copy->free_sparse_feature_vector(i); } /* test if all elements are copied correctly */ for (index_t i=0; i<subset_copy->get_num_vectors(); ++i) { SGSparseVector<float64_t> vec=subset_copy->get_sparse_feature_vector(i); index_t ind=i+offset_copy+offset_subset+1; for (index_t j=0; j<vec.num_feat_entries; ++j) { float64_t a_entry=vec.features[j].entry; float64_t b_entry=data.sparse_matrix[ind].features[j].entry; index_t a_idx=vec.features[j].feat_index; index_t b_idx=data.sparse_matrix[ind].features[j].feat_index; ASSERT(a_entry==b_entry); ASSERT(a_idx==b_idx); } subset_copy->free_sparse_feature_vector(i); } SG_UNREF(f); SG_UNREF(subset_copy); } int main(int argc, char **argv) { init_shogun_with_defaults(); test(); exit_shogun(); return 0; }
#include <shogun/features/DenseFeatures.h> #include <shogun/base/init.h> #include <shogun/lib/common.h> #include <shogun/io/SGIO.h> using namespace shogun; int main(int argc, char** argv) { init_shogun_with_defaults(); //sg_io->set_loglevel(MSG_DEBUG); //sg_io->enable_file_and_line(); // create three 2-dimensional vectors SGMatrix<float64_t> matrix(2,3); for (int32_t i=0; i<6; i++) matrix.matrix[i]=i; // shogun will now own the matrix created CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix); ASSERT(features->parameter_hash_changed()); SG_UNREF(features); exit_shogun(); return 0; }
/* * 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) 2011-2012 Heiko Strathmann * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/labels/MulticlassLabels.h> #include <shogun/mathematics/Math.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } const int32_t num_labels=10; const int32_t num_classes=3; void test() { const int32_t num_subset_idx=CMath::random(1, num_labels); /* create labels */ CMulticlassLabels* labels=new CMulticlassLabels(num_labels); for (index_t i=0; i<num_labels; ++i) labels->set_label(i, i%num_classes); SG_REF(labels); /* print labels */ SGVector<float64_t> labels_data=labels->get_labels(); SGVector<float64_t>::display_vector(labels_data.vector, labels_data.vlen, "labels"); /* create subset indices */ SGVector<index_t> subset_idx(SGVector<index_t>::randperm(num_subset_idx), num_subset_idx); /* print subset indices */ SGVector<index_t>::display_vector(subset_idx.vector, subset_idx.vlen, "subset indices"); /* apply subset to features */ SG_SPRINT("\n\n-------------------\n" "applying subset to features\n" "-------------------\n"); labels->add_subset(subset_idx); /* do some stuff do check and output */ ASSERT(labels->get_num_labels()==num_subset_idx); SG_SPRINT("labels->get_num_labels(): %d\n", labels->get_num_labels()); for (index_t i=0; i<labels->get_num_labels(); ++i) { float64_t label=labels->get_label(i); SG_SPRINT("label %f:\n", label); ASSERT(label==labels_data.vector[subset_idx.vector[i]]); } /* remove features subset */SG_SPRINT("\n\n-------------------\n" "removing subset from features\n" "-------------------\n"); labels->remove_all_subsets(); ASSERT(labels->get_num_labels()==num_labels); SG_SPRINT("labels->get_num_labels(): %d\n", labels->get_num_labels()); for (index_t i=0; i<labels->get_num_labels(); ++i) { float64_t label=labels->get_label(i); SG_SPRINT("label %f:\n", label); ASSERT(label==labels_data.vector[i]); } SG_UNREF(labels); } int main(int argc, char **argv) { init_shogun(&print_message, &print_message, &print_message); test(); exit_shogun(); return 0; }
/* * 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) 2011-2012 Heiko Strathmann * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/features/Subset.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } void check_transposed(CDenseFeatures<int32_t>* features) { CDenseFeatures<int32_t>* transposed=features->get_transposed(); CDenseFeatures<int32_t>* double_transposed=transposed->get_transposed(); for (index_t i=0; i<features->get_num_vectors(); ++i) { SGVector<int32_t> orig_vec=features->get_feature_vector(i); SGVector<int32_t> new_vec=double_transposed->get_feature_vector(i); ASSERT(orig_vec.vlen==new_vec.vlen); for (index_t j=0; j<orig_vec.vlen; j++) ASSERT(orig_vec.vector[j]==new_vec.vector[j]); /* not necessary since feature matrix is in memory. for documentation */ features->free_feature_vector(orig_vec,i); double_transposed->free_feature_vector(new_vec, i); } SG_UNREF(transposed); SG_UNREF(double_transposed); } const int32_t num_vectors=6; const int32_t dim_features=6; void test() { const int32_t num_subset_idx=CMath::random(1, num_vectors); /* create feature data matrix */ SGMatrix<int32_t> data(dim_features, num_vectors); /* fill matrix with random data */ for (index_t i=0; i<num_vectors; ++i) { for (index_t j=0; j<dim_features; ++j) data.matrix[i*dim_features+j]=CMath::random(-5, 5); } /* create simple features */ CDenseFeatures<int32_t>* features=new CDenseFeatures<int32_t> (data); SG_REF(features); /* print feature matrix */ SGMatrix<int32_t>::display_matrix(data.matrix, data.num_rows, data.num_cols, "feature matrix"); /* create subset indices */ SGVector<index_t> subset_idx(SGVector<index_t>::randperm(num_subset_idx), num_subset_idx); /* print subset indices */ SGVector<index_t>::display_vector(subset_idx.vector, subset_idx.vlen, "subset indices"); /* apply subset to features */ SG_SPRINT("\n\n-------------------\n" "applying subset to features\n" "-------------------\n"); features->add_subset(subset_idx); /* do some stuff do check and output */ ASSERT(features->get_num_vectors()==num_subset_idx); /* check get_Transposed method */ SG_SPRINT("checking transpose..."); check_transposed(features); SG_SPRINT("does work\n"); SG_SPRINT("features->get_num_vectors(): %d\n", features->get_num_vectors()); for (index_t i=0; i<features->get_num_vectors(); ++i) { SGVector<int32_t> vec=features->get_feature_vector(i); SG_SPRINT("vector %d: ", i); SGVector<int32_t>::display_vector(vec.vector, vec.vlen); for (index_t j=0; j<dim_features; ++j) ASSERT(vec.vector[j]==data.matrix[subset_idx.vector[i]*num_vectors+j]); /* not necessary since feature matrix is in memory. for documentation */ features->free_feature_vector(vec, i); } /* remove features subset */ SG_SPRINT("\n\n-------------------\n" "removing subset from features\n" "-------------------\n"); features->remove_all_subsets(); /* do some stuff do check and output */ ASSERT(features->get_num_vectors()==num_vectors); SG_SPRINT("features->get_num_vectors(): %d\n", features->get_num_vectors()); /* check get_Transposed method */ SG_SPRINT("checking transpose..."); check_transposed(features); SG_SPRINT("does work\n"); for (index_t i=0; i<features->get_num_vectors(); ++i) { SGVector<int32_t> vec=features->get_feature_vector(i); SG_SPRINT("vector %d: ", i); SGVector<int32_t>::display_vector(vec.vector, vec.vlen); for (index_t j=0; j<dim_features; ++j) ASSERT(vec.vector[j]==data.matrix[i*num_vectors+j]); /* not necessary since feature matrix is in memory. for documentation */ features->free_feature_vector(vec, i); } SG_UNREF(features); } int main(int argc, char **argv) { init_shogun_with_defaults(); test(); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann */ #include <shogun/base/init.h> #include <shogun/features/SubsetStack.h> using namespace shogun; void test() { CSubsetStack* stack=new CSubsetStack(); SG_REF(stack); /* subset indices, each set is shifted by one */ SGVector<index_t> subset_a(10); SGVector<index_t> subset_b(4); subset_a.range_fill(1); subset_b.range_fill(1); /* add and remove subsets a couple of times */ stack->add_subset(subset_a); stack->remove_subset(); stack->add_subset(subset_b); stack->remove_subset(); /* add and remove subsets a couple of times, different order */ stack->add_subset(subset_a); stack->add_subset(subset_b); stack->remove_subset(); stack->remove_subset(); /** add two subsets and check if index mapping works */ stack->add_subset(subset_a); stack->add_subset(subset_b); /* remember, offset of one for each index set */ for (index_t i=0; i<subset_b.vlen; ++i) ASSERT(stack->subset_idx_conversion(i)==i+2); stack->remove_subset(); stack->remove_subset(); /* clean up */ SG_UNREF(stack); } int main(int argc, char **argv) { init_shogun_with_defaults(); test(); exit_shogun(); return 0; }
#include <shogun/base/init.h> #include <shogun/features/HashedDenseFeatures.h> #include <shogun/features/HashedSparseFeatures.h> #include <shogun/mathematics/Math.h> #include <shogun/kernel/PolyKernel.h> using namespace shogun; int main() { init_shogun_with_defaults(); int32_t num_vectors = 5; int32_t dim = 20; SGMatrix<int32_t> mat(dim, num_vectors); for (index_t v=0; v<num_vectors; v++) { for (index_t d=0; d<dim; d++) mat(d,v) = CMath::random(-dim, dim); } int32_t hashing_dim = 12; CHashedDenseFeatures<int32_t>* h_dense_feats = new CHashedDenseFeatures<int32_t>(mat, hashing_dim); CSparseFeatures<int32_t>* sparse_feats = new CSparseFeatures<int32_t>(mat); CHashedSparseFeatures<int32_t>* h_sparse_feats = new CHashedSparseFeatures<int32_t>(sparse_feats, hashing_dim); SG_REF(h_dense_feats); CPolyKernel* kernel = new CPolyKernel(h_dense_feats, h_dense_feats, 1, false); SGMatrix<float64_t> dense_mt = kernel->get_kernel_matrix(); SG_UNREF(kernel); SG_REF(h_sparse_feats); kernel = new CPolyKernel(h_sparse_feats, h_sparse_feats, 1, false); SGMatrix<float64_t> sparse_mt = kernel->get_kernel_matrix(); SG_UNREF(kernel); for (index_t i=0; i<dense_mt.num_rows; i++) { for (index_t j=0; j<dense_mt.num_cols; j++) ASSERT(dense_mt(i,j)==sparse_mt(i,j)) } dense_mt.display_matrix("Dense matrix"); sparse_mt.display_matrix("Sparse matrix"); SG_UNREF(h_dense_feats); SG_UNREF(h_sparse_feats); exit_shogun(); }
/* * 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) 2014 Jiaolong Xu * Copyright (C) 2014 Jiaolong Xu */ #include <shogun/io/LibSVMFile.h> #include <shogun/lib/SGVector.h> #include <shogun/lib/SGSparseVector.h> #include <shogun/base/DynArray.h> #include <shogun/base/init.h> using namespace shogun; #define SHOW_DATA /* file data */ const char fname_svm_multilabel[] = "../../../../data/multilabel/yeast_test.svm"; void test_libsvmfile_multilabel(const char* fname) { FILE* pfile = fopen(fname, "r"); if (pfile == NULL) { SG_SPRINT("Unable to open file: %s\n", fname); return; } fclose(pfile); /* sparse data from matrix*/ CLibSVMFile* svmfile = new CLibSVMFile(fname); SGSparseVector<float64_t>* feats; SGVector<float64_t>* labels; int32_t dim_feat; int32_t num_samples; int32_t num_classes; svmfile->get_sparse_matrix(feats, dim_feat, num_samples, labels, num_classes); #ifdef SHOW_DATA // Display the labels for (int32_t i = 0; i < num_samples; i++) { labels[i].display_vector(); } #endif SG_SPRINT("Number of the samples: %d\n", num_samples); SG_SPRINT("Dimention of the feature: %d\n", dim_feat); SG_SPRINT("Number of classes: %d\n", num_classes); SG_UNREF(svmfile); SG_FREE(feats); SG_FREE(labels); } int main(int argc, char ** argv) { init_shogun_with_defaults(); sg_io->set_loglevel(MSG_DEBUG); test_libsvmfile_multilabel(fname_svm_multilabel); exit_shogun(); return 0; }
#include <shogun/base/init.h> #include <shogun/io/LineReader.h> #include <shogun/lib/DelimiterTokenizer.h> #include <shogun/lib/SGVector.h> #include <shogun/io/SGIO.h> #include <cstdio> using namespace shogun; int main(int argc, char** argv) { init_shogun_with_defaults(); FILE* fin=fopen("io_linereader.cpp", "r"); CDelimiterTokenizer* tokenizer=new CDelimiterTokenizer(); tokenizer->delimiters['\n']=1; SG_REF(tokenizer); CLineReader* reader=new CLineReader(fin, tokenizer); int lines_count=0; SGVector<char> tmp_string; while (reader->has_next()) { tmp_string=reader->read_line(); SG_SPRINT("%d %d ", lines_count, tmp_string.vlen); for (int i=0; i<tmp_string.vlen; i++) SG_SPRINT("%c", tmp_string.vector[i]); SG_SPRINT("\n"); lines_count++; } SG_SPRINT("total lines: %d\n", lines_count); tmp_string=SGVector<char>(); SG_UNREF(reader); SG_UNREF(tokenizer); fclose(fin); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann */ #include <shogun/base/init.h> #include <shogun/kernel/GaussianKernel.h> #include <shogun/kernel/CustomKernel.h> #include <shogun/features/DenseFeatures.h> #include <shogun/features/DataGenerator.h> #include <shogun/features/IndexFeatures.h> using namespace shogun; void test_custom_kernel_subsets() { /* create some data */ index_t m=10; CFeatures* features= new CDenseFeatures<float64_t>(CDataGenerator::generate_mean_data( m, 2, 1)); SG_REF(features); /* create a custom kernel */ CKernel* k=new CGaussianKernel(); k->init(features, features); CCustomKernel* l=new CCustomKernel(k); /* create a random permutation */ SGVector<index_t> subset(m); for (index_t run=0; run<100; ++run) { subset.range_fill(); subset.permute(); // subset.display_vector("permutation"); features->add_subset(subset); k->init(features, features); l->add_row_subset(subset); l->add_col_subset(subset); // k->get_kernel_matrix().display_matrix("K"); // l->get_kernel_matrix().display_matrix("L"); for (index_t i=0; i<m; ++i) { for (index_t j=0; j<m; ++j) { SG_SDEBUG("K(%d,%d)=%f, L(%d,%d)=%f\n", i, j, k->kernel(i, j), i, j, l->kernel(i, j)); ASSERT(CMath::abs(k->kernel(i, j)-l->kernel(i, j))<10E-8); } } features->remove_subset(); l->remove_row_subset(); l->remove_col_subset(); } SG_UNREF(k); SG_UNREF(l); SG_UNREF(features); } int main(int argc, char** argv) { init_shogun_with_defaults(); //sg_io->set_loglevel(MSG_DEBUG); test_custom_kernel_subsets(); exit_shogun(); return 0; }
/* * Copyright (c) The Shogun Machine Learning Toolbox * Written (w) 2014 pl8787 * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are those * of the authors and should not be interpreted as representing official policies, * either expressed or implied, of the Shogun Development Team. */ #include <shogun/base/init.h> #include <shogun/kernel/GaussianKernel.h> #include <shogun/kernel/CustomKernel.h> #include <shogun/features/DenseFeatures.h> #include <shogun/features/DataGenerator.h> #include <shogun/features/IndexFeatures.h> using namespace shogun; void test_custom_kernel_index_subsets() { /* create some data */ index_t m=10; index_t num_sub_row=3; index_t num_sub_col=2; CFeatures* features= new CDenseFeatures<float64_t>(CDataGenerator::generate_mean_data( m, 2, 1)); SG_REF(features); /* create a custom kernel */ CGaussianKernel* gaussian_kernel=new CGaussianKernel(2,10); gaussian_kernel->init(features, features); CCustomKernel* custom_kernel=new CCustomKernel(gaussian_kernel); /* create random permutations */ SGVector<index_t> row_subset(num_sub_row); SGVector<index_t> col_subset(num_sub_col); row_subset.range_fill(); row_subset.permute(); col_subset.range_fill(); col_subset.permute(); /* create index features */ CIndexFeatures* row_idx_feat=new CIndexFeatures(row_subset); CIndexFeatures* col_idx_feat=new CIndexFeatures(col_subset); SG_REF(row_idx_feat); SG_REF(col_idx_feat); custom_kernel->init(row_idx_feat, col_idx_feat); SGMatrix<float64_t> gaussian_kernel_matrix= gaussian_kernel->get_kernel_matrix(); SGMatrix<float64_t> custom_kernel_matrix= custom_kernel->get_kernel_matrix(); custom_kernel_matrix.display_matrix("subset"); SG_UNREF(gaussian_kernel); SG_UNREF(custom_kernel); SG_UNREF(row_idx_feat); SG_UNREF(col_idx_feat); SG_UNREF(features); } int main(int argc, char** argv) { init_shogun_with_defaults(); test_custom_kernel_index_subsets(); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann */ #include <shogun/base/init.h> #include <shogun/kernel/GaussianKernel.h> #include <shogun/kernel/CustomKernel.h> #include <shogun/features/DenseFeatures.h> #include <shogun/features/DataGenerator.h> using namespace shogun; void test_custom_kernel_subsets() { /* create some data */ index_t m=10; CFeatures* features= new CDenseFeatures<float64_t>(CDataGenerator::generate_mean_data( m, 2, 1)); SG_REF(features); /* create a custom kernel */ CKernel* k=new CGaussianKernel(); k->init(features, features); CCustomKernel* l=new CCustomKernel(k); /* create a random permutation */ SGVector<index_t> subset(m); for (index_t run=0; run<100; ++run) { subset.range_fill(); subset.permute(); // subset.display_vector("permutation"); features->add_subset(subset); k->init(features, features); l->add_row_subset(subset); l->add_col_subset(subset); // k->get_kernel_matrix().display_matrix("K"); // l->get_kernel_matrix().display_matrix("L"); for (index_t i=0; i<m; ++i) { for (index_t j=0; j<m; ++j) { SG_SDEBUG("K(%d,%d)=%f, L(%d,%d)=%f\n", i, j, k->kernel(i, j), i, j, l->kernel(i, j)); ASSERT(CMath::abs(k->kernel(i, j)-l->kernel(i, j))<10E-8); } } features->remove_subset(); l->remove_row_subset(); l->remove_col_subset(); } SG_UNREF(k); SG_UNREF(l); SG_UNREF(features); } int main(int argc, char** argv) { init_shogun_with_defaults(); // sg_io->set_loglevel(MSG_DEBUG); test_custom_kernel_subsets(); exit_shogun(); return 0; }
#include <shogun/features/DenseFeatures.h> #include <shogun/kernel/GaussianKernel.h> #include <shogun/base/init.h> #include <shogun/lib/common.h> #include <shogun/io/SGIO.h> #include <stdio.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); // create some data SGMatrix<float64_t> matrix(2,3); for (int32_t i=0; i<6; i++) matrix.matrix[i]=i; // create three 2-dimensional vectors // shogun will now own the matrix created CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix); // create gaussian kernel with cache 10MB, width 0.5 CGaussianKernel* kernel = new CGaussianKernel(features, features, 0.5, 10); // print kernel matrix for (int32_t i=0; i<3; i++) { for (int32_t j=0; j<3; j++) { SG_SPRINT("%f ", kernel->kernel(i,j)); } SG_SPRINT("\n"); } // free up memory SG_UNREF(kernel); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann * Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/features/DenseFeatures.h> #include <shogun/labels/BinaryLabels.h> #include <shogun/kernel/LinearKernel.h> #include <shogun/classifier/svm/LibSVM.h> #include <shogun/evaluation/ContingencyTableEvaluation.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } void test() { /* data matrix dimensions */ index_t num_vectors=6; index_t num_features=2; /* data means -1, 1 in all components, small std deviation */ SGVector<float64_t> mean_1(num_features); SGVector<float64_t> mean_2(num_features); SGVector<float64_t>::fill_vector(mean_1.vector, mean_1.vlen, -10.0); SGVector<float64_t>::fill_vector(mean_2.vector, mean_2.vlen, 10.0); float64_t sigma=0.5; SGVector<float64_t>::display_vector(mean_1.vector, mean_1.vlen, "mean 1"); SGVector<float64_t>::display_vector(mean_2.vector, mean_2.vlen, "mean 2"); /* fill data matrix around mean */ SGMatrix<float64_t> train_dat(num_features, num_vectors); for (index_t i=0; i<num_vectors; ++i) { for (index_t j=0; j<num_features; ++j) { float64_t mean=i<num_vectors/2 ? mean_1.vector[0] : mean_2.vector[0]; train_dat.matrix[i*num_features+j]=CMath::normal_random(mean, sigma); } } SGMatrix<float64_t>::display_matrix(train_dat.matrix, train_dat.num_rows, train_dat.num_cols, "training data"); /* training features */ CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(train_dat); SG_REF(features); /* training labels +/- 1 for each cluster */ SGVector<float64_t> lab(num_vectors); for (index_t i=0; i<num_vectors; ++i) lab.vector[i]=i<num_vectors/2 ? -1.0 : 1.0; SGVector<float64_t>::display_vector(lab.vector, lab.vlen, "training labels"); CBinaryLabels* labels=new CBinaryLabels(lab); SG_REF(labels); /* evaluation instance */ CContingencyTableEvaluation* eval=new CContingencyTableEvaluation(ACCURACY); /* kernel */ CKernel* kernel=new CLinearKernel(); kernel->init(features, features); /* create svm via libsvm */ float64_t svm_C=10; float64_t svm_eps=0.0001; CLibSVM* svm=new CLibSVM(svm_C, kernel, labels); svm->set_epsilon(svm_eps); /* now train a few times on different subsets on data and assert that * results are correct (data linear separable) */ svm->data_lock(labels, features); SGVector<index_t> indices(5); indices.vector[0]=1; indices.vector[1]=2; indices.vector[2]=3; indices.vector[3]=4; indices.vector[4]=5; SGVector<index_t>::display_vector(indices.vector, indices.vlen, "training indices"); svm->train_locked(indices); CBinaryLabels* output=CLabelsFactory::to_binary(svm->apply()); SGVector<float64_t>::display_vector(output->get_labels().vector, output->get_num_labels(), "apply() output"); SGVector<float64_t>::display_vector(labels->get_labels().vector, labels->get_labels().vlen, "training labels"); SG_SPRINT("accuracy: %f\n", eval->evaluate(output, labels)); ASSERT(eval->evaluate(output, labels)==1); SG_UNREF(output); SG_SPRINT("\n\n"); indices=SGVector<index_t>(3); indices.vector[0]=1; indices.vector[1]=2; indices.vector[2]=3; SGVector<index_t>::display_vector(indices.vector, indices.vlen, "training indices"); output=CLabelsFactory::to_binary(svm->apply()); SGVector<float64_t>::display_vector(output->get_labels().vector, output->get_num_labels(), "apply() output"); SGVector<float64_t>::display_vector(labels->get_labels().vector, labels->get_labels().vlen, "training labels"); SG_SPRINT("accuracy: %f\n", eval->evaluate(output, labels)); ASSERT(eval->evaluate(output, labels)==1); SG_UNREF(output); SG_SPRINT("\n\n"); indices=SGVector<index_t>(4); indices.range_fill(); SGVector<index_t>::display_vector(indices.vector, indices.vlen, "training indices"); svm->train_locked(indices); output=CLabelsFactory::to_binary(svm->apply()); SGVector<float64_t>::display_vector(output->get_labels().vector, output->get_num_labels(), "apply() output"); SGVector<float64_t>::display_vector(labels->get_labels().vector, labels->get_labels().vlen, "training labels"); SG_SPRINT("accuracy: %f\n", eval->evaluate(output, labels)); ASSERT(eval->evaluate(output, labels)==1); SG_UNREF(output); SG_SPRINT("normal train\n"); svm->data_unlock(); svm->train(); output=CLabelsFactory::to_binary(svm->apply()); ASSERT(eval->evaluate(output, labels)==1); SGVector<float64_t>::display_vector(output->get_labels().vector, output->get_num_labels(), "output"); SGVector<float64_t>::display_vector(labels->get_labels().vector, labels->get_labels().vlen, "training labels"); SG_UNREF(output); /* clean up */ SG_UNREF(svm); SG_UNREF(features); SG_UNREF(eval); SG_UNREF(labels); } int main(int argc, char **argv) { init_shogun(&print_message, &print_message, &print_message); test(); exit_shogun(); return 0; }
#include <shogun/features/DenseFeatures.h> #include <shogun/kernel/DotKernel.h> #include <shogun/base/init.h> #include <shogun/lib/common.h> #include <shogun/io/SGIO.h> #include <stdio.h> using namespace shogun; class CReverseLinearKernel : public CDotKernel { public: /** default constructor */ CReverseLinearKernel() : CDotKernel(0) { } /** destructor */ virtual ~CReverseLinearKernel() { } /** initialize kernel * * @param l features of left-hand side * @param r features of right-hand side * @return if initializing was successful */ virtual bool init(CFeatures* l, CFeatures* r) { CDotKernel::init(l, r); return init_normalizer(); } /** load kernel init_data * * @param src file to load from * @return if loading was successful */ virtual bool load_init(FILE* src) { return false; } /** save kernel init_data * * @param dest file to save to * @return if saving was successful */ virtual bool save_init(FILE* dest) { return false; } /** return what type of kernel we are * * @return kernel type UNKNOWN (as it is not part * officially part of shogun) */ virtual EKernelType get_kernel_type() { return K_UNKNOWN; } /** return the kernel's name * * @return name "Reverse Linear" */ inline virtual const char* get_name() const { return "ReverseLinear"; } protected: /** compute kernel function for features a and b * idx_{a,b} denote the index of the feature vectors * in the corresponding feature object * * @param idx_a index a * @param idx_b index b * @return computed kernel function at indices a,b */ virtual float64_t compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree); float64_t* bvec= ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen); float64_t result=0; for (int32_t i=0; i<alen; i++) result+=avec[i]*bvec[alen-i-1]; ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree); ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree); return result; } }; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } int main(int argc, char** argv) { init_shogun(&print_message); // create some data SGMatrix<float64_t> matrix(2,3); for (int32_t i=0; i<6; i++) matrix.matrix[i]=i; // create three 2-dimensional vectors // shogun will now own the matrix created CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(); features->set_feature_matrix(matrix); // create reverse linear kernel CReverseLinearKernel* kernel = new CReverseLinearKernel(); kernel->init(features,features); // print kernel matrix for (int32_t i=0; i<3; i++) { for (int32_t j=0; j<3; j++) SG_SPRINT("%f ", kernel->kernel(i,j)); SG_SPRINT("\n"); } // free up memory SG_UNREF(kernel); exit_shogun(); return 0; }
/* * 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 Heiko Strathmann */ #include <shogun/base/init.h> #include <shogun/labels/BinaryLabels.h> using namespace shogun; void test_sigmoid_fitting() { CBinaryLabels* labels=new CBinaryLabels(10); labels->set_values(SGVector<float64_t>(labels->get_num_labels())); for (index_t i=0; i<labels->get_num_labels(); ++i) labels->set_value(i%2==0 ? 1 : -1, i); labels->get_values().display_vector("scores"); labels->scores_to_probabilities(); labels->get_values().display_vector("probabilities"); SG_UNREF(labels); } int main() { init_shogun_with_defaults(); // sg_io->set_loglevel(MSG_DEBUG); test_sigmoid_fitting(); exit_shogun(); return 0; }
#include <shogun/base/init.h> #include <shogun/lib/CircularBuffer.h> #include <shogun/lib/DelimiterTokenizer.h> #include <shogun/lib/SGVector.h> #include <shogun/io/SGIO.h> #include <cstdio> #include <cstring> using namespace shogun; const int max_line_length = 256; int main(int argc, char** argv) { init_shogun_with_defaults(); SGVector<char> test_string(const_cast<char* >("all your bayes are belong to us! "), 33, false); CCircularBuffer* buffer=new CCircularBuffer(max_line_length); CDelimiterTokenizer* tokenizer=new CDelimiterTokenizer(); tokenizer->delimiters[' ']=1; SG_REF(tokenizer); buffer->set_tokenizer(tokenizer); SGVector<char> tmp_string; buffer->push(test_string); int num_read; index_t start; while ((num_read=buffer->next_token_idx(start))>0) { buffer->skip_characters(start); tmp_string=buffer->pop(num_read); buffer->skip_characters(1); for (int i=0; i<tmp_string.vlen; i++) SG_SPRINT("%c", tmp_string.vector[i]); SG_SPRINT("\n"); } SG_UNREF(buffer); SG_UNREF(tokenizer); exit_shogun(); return 0; }
/* * 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) 2009 Soeren Sonnenburg * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/io/SGIO.h> #include <shogun/lib/Time.h> #include <shogun/lib/ShogunException.h> #include <shogun/mathematics/Math.h> #include <shogun/lib/DynInt.h> using namespace shogun; void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } void print_warning(FILE* target, const char* str) { fprintf(target, "%s", str); } void print_error(FILE* target, const char* str) { fprintf(target, "%s", str); } void gen_ints(uint256_t* &a, uint32_t* &b, uint32_t len) { a=SG_MALLOC(uint256_t, len); b=SG_MALLOC(uint32_t, len); CMath::init_random(17); for (uint32_t i=0; i<len; i++) { uint64_t r[4]={(uint64_t) CMath::random() << 32 | CMath::random(), (uint64_t) CMath::random() << 32 | CMath::random(), (uint64_t) CMath::random() << 32 | CMath::random(), (uint64_t) CMath::random() << 32 | CMath::random()}; a[len-i-1]=r; b[len-i-1]=i; } } const int LEN = 5*1024; int main() { init_shogun(&print_message, &print_warning, &print_error); try { uint256_t* a; uint32_t* b; CTime t; t.io->set_loglevel(MSG_DEBUG); SG_SPRINT("gen data.."); t.start(); gen_ints(a,b, LEN); t.cur_time_diff(true); SG_SPRINT("qsort.."); t.start(); CMath::qsort_index(a, b, LEN); t.cur_time_diff(true); SG_SPRINT("\n\n"); for (uint32_t i=0; i<10; i++) { SG_SPRINT("a[%d]=", i); a[i].print_hex(); SG_SPRINT("\n"); } SG_SPRINT("\n\n"); uint64_t val1[4]={1,2,3,4}; uint64_t val2[4]={5,6,7,8}; a[0]=val1; a[1]=val2; a[2]=a[0]; CMath::swap(a[0],a[1]); printf("a[0]==a[1] %d\n", (int) (a[0] == a[1])); printf("a[0]<a[1] %d\n", (int) (a[0] < a[1])); printf("a[0]<=a[1] %d\n", (int) (a[0] <= a[1])); printf("a[0]>a[1] %d\n", (int) (a[0] > a[1])); printf("a[0]>=a[1] %d\n", (int) (a[0] >= a[1])); printf("a[0]==a[0] %d\n", (int) (a[0] == a[0])); printf("a[0]<a[0] %d\n", (int) (a[0] < a[0])); printf("a[0]<=a[0] %d\n", (int) (a[0] <= a[0])); printf("a[0]>a[0] %d\n", (int) (a[0] > a[0])); printf("a[0]>=a[0] %d\n", (int) (a[0] >= a[0])); SG_SPRINT("\n\n"); for (uint32_t i=0; i<10 ; i++) { SG_SPRINT("a[%d]=", i); a[i].print_hex(); printf("\n"); } SG_FREE(a); SG_FREE(b); } catch(ShogunException & sh) { SG_SPRINT("%s",sh.get_exception_string()); } exit_shogun(); return 0; }
/* * 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) 2009 Soeren Sonnenburg * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society */ #include <shogun/io/SGIO.h> #include <shogun/lib/common.h> #include <shogun/lib/SGVector.h> #include <shogun/base/DynArray.h> using namespace shogun; int main() { init_shogun_with_defaults(); DynArray<int32_t> values; for (int32_t i=0; i<1000; i++) { values.set_element(i,i); } for (int32_t i=0; i<1000; i++) { SG_SPRINT("values[%i]=%i\n", i, values[i]); } DynArray<SGVector<float64_t> > vectors(5); for (int32_t i=0; i<20; i++) { SG_SPRINT("%i\n", i); SGVector<float64_t> vec(i); for (int32_t j=0; j<i; j++) vec.vector[j]=j; vectors.set_element(vec,i); } for (int32_t i=0; i<20; i++) { SG_SPRINT("%i\n", i); vectors[i].display_vector(); } exit_shogun(); return 0; }
#include <shogun/base/init.h> #include <shogun/lib/common.h> #include <shogun/lib/GCArray.h> #include <shogun/kernel/Kernel.h> #include <shogun/kernel/GaussianKernel.h> #include <stdio.h> using namespace shogun; const int l=10; int main(int argc, char** argv) { init_shogun(); // we need this scope, because exit_shogun() must not be called // before the destructor of CGCArray<CKernel*> kernels! { // create array of kernels CGCArray<CKernel*> kernels(l); // fill array with kernels for (int i=0; i<l; i++) kernels.set(new CGaussianKernel(10, 1.0), i); // print kernels for (int i=0; i<l; i++) { CKernel* kernel = kernels.get(i); printf("kernels[%d]=%p\n", i, kernel); SG_UNREF(kernel); } } exit_shogun(); return 0; }
#include <shogun/base/init.h> #include <shogun/lib/Hash.h> #include <stdio.h> using namespace shogun; int main(int argc, char** argv) { init_shogun(); uint8_t array[4]={0,1,2,3}; printf("hash(0)=%0x\n", CHash::MurmurHash3(&array[0], 1, 0xDEADBEAF)); printf("hash(1)=%0x\n", CHash::MurmurHash3(&array[1], 1, 0xDEADBEAF)); printf("hash(2)=%0x\n", CHash::MurmurHash3(&array[0], 2, 0xDEADBEAF)); printf("hash(3)=%0x\n", CHash::MurmurHash3(&array[0], 4, 0xDEADBEAF)); uint32_t h = 0xDEADBEAF; uint32_t carry = 0; CHash::IncrementalMurmurHash3(&h, &carry, &array[0], 1); printf("inc_hash(0)=%0x\n", h); CHash::IncrementalMurmurHash3(&h, &carry, &array[1], 1); printf("inc_hash(1)=%0x\n", h); CHash::IncrementalMurmurHash3(&h, &carry, &array[2], 1); printf("inc_hash(2)=%0x\n", h); CHash::IncrementalMurmurHash3(&h, &carry, &array[3], 1); printf("inc_hash(3)=%0x\n", h); h = CHash::FinalizeIncrementalMurmurHash3(h, carry, 4); printf("Final inc_hash(3)=%0x\n", h); exit_shogun(); return 0; }
#include <shogun/lib/config.h> #include <shogun/base/init.h> #include <shogun/lib/common.h> #include <shogun/lib/SGMatrix.h> #include <shogun/io/HDF5File.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun_with_defaults(); #ifdef HAVE_HDF5 CHDF5File* hdf = new CHDF5File((char*) "../data/australian.libsvm.h5",'r', "/data/data"); float64_t* mat; int32_t num_feat; int32_t num_vec; hdf->get_matrix(mat, num_feat, num_vec); SGMatrix<float64_t>::display_matrix(mat, num_feat, num_vec); SG_FREE(mat); SG_UNREF(hdf); #endif exit_shogun(); return 0; }
#include <shogun/base/init.h> #include <shogun/lib/common.h> #include <shogun/lib/memory.h> #include <shogun/lib/IndirectObject.h> #include <shogun/mathematics/Math.h> #include <shogun/base/SGObject.h> #include <stdio.h> using namespace shogun; const int l=10; int main(int argc, char** argv) { init_shogun(); // create array a int32_t* a=SG_MALLOC(int32_t, l); for (int i=0; i<l; i++) a[i]=l-i; typedef CIndirectObject<int32_t, int32_t**> INDIRECT; // create array of indirect objects pointing to array a INDIRECT::set_array(&a); INDIRECT* x = SG_MALLOC(INDIRECT, l); INDIRECT::init_slice(x, l); printf("created array a and indirect object array x pointing to a.\n\n"); for (int i=0; i<l; i++) printf("a[%d]=%d x[%d]=%d\n", i, a[i], i, int32_t(x[i])); //sort the array CMath::qsort(x, l); printf("\n\nvoila! sorted indirect object array x, keeping a const.\n\n"); for (int i=0; i<l; i++) printf("a[%d]=%d x[%d]=%d\n", i, a[i], i, int32_t(x[i])); SG_FREE(x); SG_FREE(a); exit_shogun(); return 0; }
#include <shogun/lib/Map.h> #include <shogun/io/SGIO.h> #include <shogun/base/init.h> #include <shogun/lib/common.h> using namespace shogun; #define SIZE 6 void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } int main(int argc, char** argv) { init_shogun(&print_message, &print_message, &print_message); const char* v[SIZE] = {"Russia", "England", "Germany", "USA", "France", "Spain"}; CMap<int32_t, const char*>* map = new CMap<int32_t, const char*>(SIZE/2, SIZE/2); for (int i=0; i<SIZE; i++) map->add(i, v[i]); map->remove(0); //SG_SPRINT("Num of elements: %d\n", map->get_num_elements()); for (int i=0; i<SIZE; i++) { if (map->contains(i)) ; //SG_SPRINT("key %d contains in map with index %d and data=%s\n", // i, map->index_of(i), map->get_element(i)); } SG_UNREF(map); exit_shogun(); return 0; }
#include <shogun/lib/config.h> #include <shogun/base/init.h> #include <shogun/lib/common.h> #include <shogun/lib/SGMatrix.h> #include <shogun/io/MLDataHDF5File.h> #include <shogun/io/SGIO.h> #include <shogun/mathematics/Math.h> using namespace shogun; int main(int argc, char** argv) { init_shogun_with_defaults(); #if defined(HAVE_HDF5) && defined( HAVE_CURL) CMLDataHDF5File* hdf = NULL; try { hdf = new CMLDataHDF5File((char *)"australian", "/data/data"); } catch (ShogunException& e) { SG_UNREF(hdf); exit_shogun(); return 0; } float64_t* mat=NULL; int32_t num_feat; int32_t num_vec; try { hdf->get_matrix(mat, num_feat, num_vec); SGMatrix<float64_t>::display_matrix(mat, num_feat, num_vec); } catch (ShogunException& e) { SG_SWARNING("%s", e.get_exception_string()); } SG_FREE(mat); SG_UNREF(hdf); #endif // HAVE_CURL && HAVE_HDF5 exit_shogun(); return 0; }
#include <shogun/base/init.h> #include <shogun/io/SGIO.h> #include <shogun/lib/SGMatrix.h> #include <shogun/io/SerializableAsciiFile.h> #include <shogun/features/SparseFeatures.h> using namespace shogun; int main(int argc, char** argv) { init_shogun_with_defaults(); sg_io->set_loglevel(MSG_DEBUG); /* create feature data matrix */ SGMatrix<int32_t> data(3, 20); /* fill matrix with random data */ for (index_t i=0; i<20*3; ++i) { if (i%2==0) data.matrix[i]=0; else data.matrix[i]=CMath::random(1, 9); } /* create sparse features */ CSparseFeatures<int32_t>* sparse_features=new CSparseFeatures<int32_t>(data); CSerializableAsciiFile* file; file=new CSerializableAsciiFile("sparseFeatures.txt", 'w'); sparse_features->save_serializable(file); file->close(); SG_UNREF(file); /* this will fail with a warning, same with CSerializableHdf5File and xml serialization*/ CSparseFeatures<int32_t>* sparse_features_loaded = new CSparseFeatures<int32_t>(); file = new CSerializableAsciiFile("sparseFeatures.txt", 'r'); sparse_features_loaded->load_serializable(file); SG_UNREF(file); SG_UNREF(sparse_features_loaded); SG_UNREF(sparse_features); exit_shogun(); }
#include <shogun/lib/Set.h> #include <shogun/io/SGIO.h> #include <shogun/base/init.h> #include <shogun/lib/common.h> using namespace shogun; #define SIZE 8 void print_message(FILE* target, const char* str) { fprintf(target, "%s", str); } int main(int argc, char** argv) { init_shogun(&print_message, &print_message, &print_message); double v[SIZE] = {0.0,0.1,0.2,0.2,0.3,0.4,0.5,0.5}; CSet<double>* set = new CSet<double>(SIZE/2, SIZE/2); for (int i=0; i<SIZE; i++) set->add(v[i]); set->remove(0.2); //SG_SPRINT("Num of elements: %d\n", set->get_num_elements()); for (int i=0; i<SIZE; i++) { if (set->contains(v[i])) ; //SG_SPRINT("%lg contains in set with index %d\n", v[i], set->index_of(v[i])); } SG_UNREF(set); exit_shogun(); return 0; }
/* * 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) 2011 Heiko Strathmann * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/mathematics/Statistics.h> #include <shogun/mathematics/Math.h> #include <shogun/lib/SGVector.h> using namespace shogun; void test() { SGVector<float64_t> data(10); SGVector<float64_t>::range_fill_vector(data.vector, data.vlen, 1.0); float64_t low, up, mean; float64_t error_prob=0.05; mean=CStatistics::confidence_intervals_mean(data, error_prob, low, up); SG_SPRINT("sample mean: %f. True mean lies in [%f,%f] with %f%%\n", mean, low, up, 100*(1-error_prob)); } int main(int argc, char **argv) { init_shogun_with_defaults(); test(); exit_shogun(); return 0; }
/* * 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) 2011 Sergey Lisitsyn * Written (W) 2012 Heiko Strathmann * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society */ #include <shogun/base/init.h> #include <shogun/lib/config.h> #include <shogun/lib/SGVector.h> #include <shogun/lib/SGMatrix.h> #include <shogun/mathematics/Math.h> #include <shogun/mathematics/lapack.h> using namespace shogun; #ifdef HAVE_LAPACK bool is_equal(float64_t a, float64_t b, float64_t eps) { return CMath::abs(a-b)<=eps; } void test_ev() { SGMatrix<float64_t> A(3,3); A(0,0)=0; A(0,1)=1; A(0,2)=0; A(1,0)=1; A(1,1)=0; A(1,2)=1; A(1,0)=0; A(2,1)=1; A(2,2)=0; SGVector<float64_t> ev=SGMatrix<float64_t>::compute_eigenvectors(A); SGMatrix<float64_t>::display_matrix(A.matrix, A.num_rows, A.num_cols, "A"); SGVector<float64_t>::display_vector(ev.vector, ev.vlen, "eigenvalues"); float64_t sqrt22=CMath::sqrt(2.0)/2.0; float64_t eps=10E-16; /* check for correct eigenvectors */ ASSERT(is_equal(A(0,0), 0.5, eps)); ASSERT(is_equal(A(0,1), -sqrt22, eps)); ASSERT(is_equal(A(0,2), 0.5, eps)); ASSERT(is_equal(A(1,0), -sqrt22, eps)); ASSERT(is_equal(A(1,1), 0, eps)); ASSERT(is_equal(A(1,2), sqrt22, eps)); ASSERT(is_equal(A(2,0), 0.5, eps)); ASSERT(is_equal(A(2,1), sqrt22, eps)); ASSERT(is_equal(A(2,2), 0.5, eps)); /* check for correct eigenvalues */ ASSERT(is_equal(ev[0], -sqrt22*2, eps)); ASSERT(is_equal(ev[1], 0, eps)); ASSERT(is_equal(ev[2], sqrt22*2, eps)); } void test_matrix_multiply() { index_t n=10; SGMatrix<float64_t> I=SGMatrix<float64_t>::create_identity_matrix(n,1.0); index_t m=4; SGMatrix<float64_t> A(n, m); SGVector<float64_t>::range_fill_vector(A.matrix, m*n); SGMatrix<float64_t>::display_matrix(I, "I"); SGMatrix<float64_t>::transpose_matrix(A.matrix, A.num_rows, A.num_cols); SGMatrix<float64_t>::display_matrix(A, "A transposed"); SGMatrix<float64_t>::transpose_matrix(A.matrix, A.num_rows, A.num_cols); SGMatrix<float64_t>::display_matrix(A, "A"); SG_SPRINT("multiply A by I and check result\n"); SGMatrix<float64_t> A2=SGMatrix<float64_t>::matrix_multiply(I, A); ASSERT(A2.num_rows==A.num_rows); ASSERT(A2.num_cols==A.num_cols); SGMatrix<float64_t>::display_matrix(A2); for (index_t i=0; i<A2.num_rows; ++i) { for (index_t j=0; j<A2.num_cols; ++j) ASSERT(A(i,j)==A2(i,j)); } SG_SPRINT("multiply A by transposed I and check result\n"); SGMatrix<float64_t> A3=SGMatrix<float64_t>::matrix_multiply(I, A, true); ASSERT(A3.num_rows==I.num_rows); ASSERT(A3.num_cols==A.num_cols); SGMatrix<float64_t>::display_matrix(A3); for (index_t i=0; i<A2.num_rows; ++i) { for (index_t j=0; j<A2.num_cols; ++j) ASSERT(A(i,j)==A3(i,j)); } SG_SPRINT("multiply transposed A by I and check result\n"); SGMatrix<float64_t> A4=SGMatrix<float64_t>::matrix_multiply(A, I, true, false); ASSERT(A4.num_rows==A.num_cols); ASSERT(A4.num_cols==I.num_cols); SGMatrix<float64_t>::display_matrix(A4); for (index_t i=0; i<A.num_rows; ++i) { for (index_t j=0; j<A.num_cols; ++j) ASSERT(A(i,j)==A4(j,i)); } SG_SPRINT("multiply A by scaled I and check result\n"); SGMatrix<float64_t> A5=SGMatrix<float64_t>::matrix_multiply(I, A, false, false, n); ASSERT(A5.num_rows==I.num_rows); ASSERT(A5.num_cols==A.num_cols); SGMatrix<float64_t>::display_matrix(A5); for (index_t i=0; i<A2.num_rows; ++i) { for (index_t j=0; j<A2.num_cols; ++j) ASSERT(n*A(i,j)==A5(i,j)); } } void test_lapack() { // size of square matrix int N = 100; // square matrix double* double_matrix = new double[N*N]; // for storing eigenpairs double* double_eigenvalues = new double[N]; double* double_eigenvectors = new double[N*N]; // for SVD double* double_U = new double[N*N]; double* double_s = new double[N]; double* double_Vt = new double[N*N]; // status (should be zero) int status; // DSYGVX for (int i=0; i<N; i++) { for (int j=0; j<N; j++) double_matrix[i*N+j] = ((double)(i-j))/(i+j+1); double_matrix[i*N+i] += 100; } status = 0; wrap_dsygvx(1,'V','U',N,double_matrix,N,double_matrix,N,1,3,double_eigenvalues,double_eigenvectors,&status); if (status!=0) SG_SERROR("DSYGVX/SSYGVX failed with code %d\n",status); delete[] double_eigenvectors; // DGEQRF+DORGQR status = 0; double* double_tau = new double[N]; wrap_dgeqrf(N,N,double_matrix,N,double_tau,&status); wrap_dorgqr(N,N,N,double_matrix,N,double_tau,&status); if (status!=0) SG_SERROR("DGEQRF/DORGQR failed with code %d\n",status); delete[] double_tau; // DGESVD for (int i=0; i<N; i++) { for (int j=0; j<N; j++) double_matrix[i*N+j] = i*i+j*j; } status = 0; wrap_dgesvd('A','A',N,N,double_matrix,N,double_s,double_U,N,double_Vt,N,&status); if (status!=0) SG_SERROR("DGESVD failed with code %d\n",status); delete[] double_s; delete[] double_U; delete[] double_Vt; // DSYEV status = 0; wrap_dsyev('V','U',N,double_matrix,N,double_eigenvalues,&status); if (status!=0) SG_SERROR("DSYEV failed with code %d\n",status); delete[] double_eigenvalues; delete[] double_matrix; } #endif // HAVE_LAPACK int main(int argc, char** argv) { init_shogun_with_defaults(); #ifdef HAVE_LAPACK SG_SPRINT("checking lapack\n"); test_lapack(); SG_SPRINT("compute_eigenvectors\n"); test_ev(); SG_SPRINT("matrix_multiply\n"); test_matrix_multiply(); #endif // HAVE_LAPACK exit_shogun(); return 0; }
#include <shogun/metric/LMNN.h> #include <shogun/features/DenseFeatures.h> #include <shogun/labels/MulticlassLabels.h> using namespace shogun; int main() { init_shogun_with_defaults(); // create features, each column is a feature vector SGMatrix<float64_t> feat_mat(2,4); // 1st feature vector feat_mat(0,0)=0; feat_mat(1,0)=0; // 2nd feature vector feat_mat(0,1)=0; feat_mat(1,1)=-1; // 3rd feature vector feat_mat(0,2)=1; feat_mat(1,2)=1; // 4th feature vector feat_mat(0,3)=-1; feat_mat(1,3)=1; // wrap feat_mat into Shogun features CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(feat_mat); // create labels SGVector<float64_t> lab_vec(4); lab_vec[0]=0; lab_vec[1]=0; lab_vec[2]=1; lab_vec[3]=1; // two-class data, use MulticlassLabels because LMNN works in general for more than two classes CMulticlassLabels* labels=new CMulticlassLabels(lab_vec); // create LMNN metric machine int32_t k=1; // number of target neighbors per example CLMNN* lmnn=new CLMNN(features,labels,k); // use the identity matrix as initial transform for LMNN SGMatrix<float64_t> init_transform=SGMatrix<float64_t>::create_identity_matrix(2,1); // set number of maximum iterations and train lmnn->set_maxiter(1500); // lmnn->io->set_loglevel(MSG_DEBUG); lmnn->train(init_transform); // lmnn->get_linear_transform().display_matrix("linear_transform"); CLMNNStatistics* statistics=lmnn->get_statistics(); /* statistics->obj.display_vector("objective"); statistics->stepsize.display_vector("stepsize"); statistics->num_impostors.display_vector("num_impostors"); */ SG_UNREF(statistics); SG_UNREF(lmnn); exit_shogun(); }