Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #934 from karlnapf/master
added a collection of tests/examüples for things that currently do not work
- Loading branch information
Showing
4 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
examples/undocumented/libshogun/base_migration_new_buggy.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 <shogun/base/init.h> | ||
#include <shogun/base/Parameter.h> | ||
#include <shogun/io/SerializableAsciiFile.h> | ||
#include <shogun/base/ParameterMap.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() | ||
{ | ||
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; | ||
} | ||
|
73 changes: 73 additions & 0 deletions
73
examples/undocumented/libshogun/serialization_multiclass_labels.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 <shogun/labels/MulticlassLabels.h> | ||
#include <shogun/io/SerializableAsciiFile.h> | ||
|
||
using namespace shogun; | ||
|
||
void test() | ||
{ | ||
index_t n=10; | ||
index_t n_class=3; | ||
|
||
CMulticlassLabels* labels=new CMulticlassLabels(); | ||
|
||
SGVector<float64_t> lab(n); | ||
for (index_t i=0; i<n; ++i) | ||
lab[i]=i%n_class; | ||
|
||
labels->set_labels(lab); | ||
|
||
labels->allocate_confidences_for(n_class); | ||
SGVector<float64_t> conf(n_class); | ||
for (index_t i=0; i<n_class; ++i) | ||
conf[i]=CMath::randn_double(); | ||
|
||
for (index_t i=0; i<n; ++i) | ||
labels->set_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; i<n_class; ++i) | ||
{ | ||
labels->get_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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 <shogun/base/init.h> | ||
#include <shogun/labels/MulticlassLabels.h> | ||
#include <shogun/io/SerializableAsciiFile.h> | ||
#include <gtest/gtest.h> | ||
|
||
using namespace shogun; | ||
|
||
TEST(Serialization,multiclass_labels) | ||
{ | ||
index_t n=10; | ||
index_t n_class=3; | ||
|
||
CMulticlassLabels* labels=new CMulticlassLabels(); | ||
|
||
SGVector<float64_t> lab(n); | ||
for (index_t i=0; i<n; ++i) | ||
lab[i]=i%n_class; | ||
|
||
labels->set_labels(lab); | ||
|
||
labels->allocate_confidences_for(n_class); | ||
SGVector<float64_t> conf(n_class); | ||
for (index_t i=0; i<n_class; ++i) | ||
conf[i]=CMath::randn_double(); | ||
|
||
for (index_t i=0; i<n; ++i) | ||
labels->set_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; i<n; ++i) | ||
ASSERT(labels_loaded->get_labels()[i]==labels->get_labels()[i]); | ||
|
||
for (index_t i=0; i<n; ++i) | ||
{ | ||
for (index_t j=0; j<n_class; ++j) | ||
{ | ||
//float64_t a=labels->get_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); | ||
} | ||
|
||
|