From 4da2a23b474958427855af7a978f332d3649cc78 Mon Sep 17 00:00:00 2001 From: Heiko Strathmann Date: Thu, 14 Mar 2013 17:04:39 +0000 Subject: [PATCH 1/5] added an example for multiclass serialization that doesnt work (but runs fine) Once muticlass label serialization work this should be turned into a test --- examples/undocumented/libshogun/Makefile | 1 + .../labels_multiclass_serialization.cpp | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 examples/undocumented/libshogun/labels_multiclass_serialization.cpp diff --git a/examples/undocumented/libshogun/Makefile b/examples/undocumented/libshogun/Makefile index 88473e56b9c..8fc20af4e51 100644 --- a/examples/undocumented/libshogun/Makefile +++ b/examples/undocumented/libshogun/Makefile @@ -84,6 +84,7 @@ TARGETS = basic_minimal \ converter_stochasticproximityembedding \ converter_factoranalysis \ serialization_basic_tests \ + serialization_multiclass_labels \ kernel_machine_train_locked \ statistics \ transfer_multitaskleastsquaresregression \ diff --git a/examples/undocumented/libshogun/labels_multiclass_serialization.cpp b/examples/undocumented/libshogun/labels_multiclass_serialization.cpp new file mode 100644 index 00000000000..9017c2b9e73 --- /dev/null +++ b/examples/undocumented/libshogun/labels_multiclass_serialization.cpp @@ -0,0 +1,43 @@ +/* + * 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 + */ +#include + +using namespace shogun; + +void test_sigmoid_fitting() +{ + CBinaryLabels* labels=new CBinaryLabels(10); + labels->set_values(SGVector(labels->get_num_labels())); + + for (index_t i=0; iget_num_labels(); ++i) + labels->set_value(i%2==0 ? 1 : -1, i); + + labels->get_values().display_vector("scores"); + labels->scores_to_probabilities(); + + /* only two probabilities will be the result, repeatedly, + * assert against reference implementation */ + ASSERT(CMath::abs(labels->get_value(0)-0.8571428439385661)<10E-15); + ASSERT(CMath::abs(labels->get_value(1)-0.14285715606143384)<10E-15); + + SG_UNREF(labels); +} + +int main() +{ + init_shogun_with_defaults(); + +// sg_io->set_loglevel(MSG_DEBUG); + + test_sigmoid_fitting(); + + exit_shogun(); + return 0; +} + From b51ee305b33fd53544f4ed02e3e38b906f1d21b8 Mon Sep 17 00:00:00 2001 From: Heiko Strathmann Date: Thu, 14 Mar 2013 17:11:23 +0000 Subject: [PATCH 2/5] added a test for multiclass serialization --- .../serialization_multiclass_labels.cpp | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 examples/undocumented/libshogun/serialization_multiclass_labels.cpp diff --git a/examples/undocumented/libshogun/serialization_multiclass_labels.cpp b/examples/undocumented/libshogun/serialization_multiclass_labels.cpp new file mode 100644 index 00000000000..d6a5fe5f2b1 --- /dev/null +++ b/examples/undocumented/libshogun/serialization_multiclass_labels.cpp @@ -0,0 +1,73 @@ +/* + * 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 + */ +#include +#include + +using namespace shogun; + +void test() +{ + index_t n=10; + index_t n_class=3; + + CMulticlassLabels* labels=new CMulticlassLabels(); + + SGVector lab(n); + for (index_t i=0; iset_labels(lab); + + labels->allocate_confidences_for(n_class); + SGVector conf(n_class); + for (index_t i=0; iset_multiclass_confidences(i, conf); + + /* create serialized copy */ + const char* filename="multiclass_labels.txt"; + CSerializableAsciiFile* file=new CSerializableAsciiFile(filename, 'w'); + labels->save_serializable(file); + file->close(); + SG_UNREF(file); + + file=new CSerializableAsciiFile(filename, 'r'); + CMulticlassLabels* labels_loaded=new CMulticlassLabels(); + labels_loaded->load_serializable(file); + file->close(); + SG_UNREF(file); + + /* compare */ + labels->get_labels().display_vector("labels"); + labels_loaded->get_labels().display_vector("labels_loaded"); + + for (index_t i=0; iget_multiclass_confidences(i).display_vector("confidences"); + labels_loaded->get_multiclass_confidences(i).display_vector("confidences_loaded"); + } + + SG_UNREF(labels_loaded); + SG_UNREF(labels); +} + +int main() +{ + init_shogun_with_defaults(); + +// sg_io->set_loglevel(MSG_DEBUG); + + test(); + + exit_shogun(); + return 0; +} + From 0aa25e8b24d303519c7692a95ed3d6e68ca4f7c3 Mon Sep 17 00:00:00 2001 From: Heiko Strathmann Date: Thu, 14 Mar 2013 17:11:31 +0000 Subject: [PATCH 3/5] removed wrongly named file --- .../labels_multiclass_serialization.cpp | 43 ------------------- 1 file changed, 43 deletions(-) delete mode 100644 examples/undocumented/libshogun/labels_multiclass_serialization.cpp diff --git a/examples/undocumented/libshogun/labels_multiclass_serialization.cpp b/examples/undocumented/libshogun/labels_multiclass_serialization.cpp deleted file mode 100644 index 9017c2b9e73..00000000000 --- a/examples/undocumented/libshogun/labels_multiclass_serialization.cpp +++ /dev/null @@ -1,43 +0,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 - */ -#include - -using namespace shogun; - -void test_sigmoid_fitting() -{ - CBinaryLabels* labels=new CBinaryLabels(10); - labels->set_values(SGVector(labels->get_num_labels())); - - for (index_t i=0; iget_num_labels(); ++i) - labels->set_value(i%2==0 ? 1 : -1, i); - - labels->get_values().display_vector("scores"); - labels->scores_to_probabilities(); - - /* only two probabilities will be the result, repeatedly, - * assert against reference implementation */ - ASSERT(CMath::abs(labels->get_value(0)-0.8571428439385661)<10E-15); - ASSERT(CMath::abs(labels->get_value(1)-0.14285715606143384)<10E-15); - - SG_UNREF(labels); -} - -int main() -{ - init_shogun_with_defaults(); - -// sg_io->set_loglevel(MSG_DEBUG); - - test_sigmoid_fitting(); - - exit_shogun(); - return 0; -} - From b2313d0d1b38b94686621e5a749b9a1f3c956740 Mon Sep 17 00:00:00 2001 From: Heiko Strathmann Date: Thu, 14 Mar 2013 17:12:19 +0000 Subject: [PATCH 4/5] added a provisory test for multiclass label serialization which has to be activated once this works --- tests/unit/base/Serialization_unittest.cc | 71 +++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/unit/base/Serialization_unittest.cc diff --git a/tests/unit/base/Serialization_unittest.cc b/tests/unit/base/Serialization_unittest.cc new file mode 100644 index 00000000000..ee428c95fdd --- /dev/null +++ b/tests/unit/base/Serialization_unittest.cc @@ -0,0 +1,71 @@ +/* + * 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 + */ + +#include +#include +#include +#include + +using namespace shogun; + +TEST(Serialization,multiclass_labels) +{ + index_t n=10; + index_t n_class=3; + + CMulticlassLabels* labels=new CMulticlassLabels(); + + SGVector lab(n); + for (index_t i=0; iset_labels(lab); + + labels->allocate_confidences_for(n_class); + SGVector conf(n_class); + for (index_t i=0; iset_multiclass_confidences(i, conf); + + /* create serialized copy */ + const char* filename="multiclass_labels.txt"; + CSerializableAsciiFile* file=new CSerializableAsciiFile(filename, 'w'); + labels->save_serializable(file); + file->close(); + SG_UNREF(file); + + file=new CSerializableAsciiFile(filename, 'r'); + CMulticlassLabels* labels_loaded=new CMulticlassLabels(); + labels_loaded->load_serializable(file); + file->close(); + SG_UNREF(file); + + /* compare */ + for (index_t i=0; iget_labels()[i]==labels->get_labels()[i]); + + for (index_t i=0; iget_multiclass_confidences(i)[j]; + //float64_t b=labels_loaded->get_multiclass_confidences(i)[j]; + // Add one multiclass serialization works + //float64_t diff=CMath::abs(a-b); + //EXPECT_LE(diff, 10E-15); + } + } + + SG_UNREF(labels_loaded); + SG_UNREF(labels); +} + + From 4110afcb7cda05f29564691076ae20cdd4c172f3 Mon Sep 17 00:00:00 2001 From: Heiko Strathmann Date: Thu, 14 Mar 2013 17:14:46 +0000 Subject: [PATCH 5/5] added an example for adding new parameters to classes. However this one does not work, must be activated once it works --- examples/undocumented/libshogun/Makefile | 1 + .../libshogun/base_migration_new_buggy.cpp | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 examples/undocumented/libshogun/base_migration_new_buggy.cpp diff --git a/examples/undocumented/libshogun/Makefile b/examples/undocumented/libshogun/Makefile index 8fc20af4e51..e4363301410 100644 --- a/examples/undocumented/libshogun/Makefile +++ b/examples/undocumented/libshogun/Makefile @@ -104,6 +104,7 @@ TARGETS = basic_minimal \ library_serialization \ classifier_svmlight_string_features_precomputed_kernel \ classifier_mkl_svmlight_modelselection_bug \ + base_migration_new_buggy \ all: $(TARGETS) diff --git a/examples/undocumented/libshogun/base_migration_new_buggy.cpp b/examples/undocumented/libshogun/base_migration_new_buggy.cpp new file mode 100644 index 00000000000..dc24e08b5b2 --- /dev/null +++ b/examples/undocumented/libshogun/base_migration_new_buggy.cpp @@ -0,0 +1,95 @@ +/* + * 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 +#include +#include +#include + +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() +{ + const char* filename="test.txt"; + + /* 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); +} + +int main(int argc, char **argv) +{ + init_shogun_with_defaults(); + + test(); + + exit_shogun(); + + return 0; +} +