commit 25cefcf286553ea61ace0a4feb92d56bde93ecc5
parent ed1425560d7afef27815068a304756f2b5af32bd
Author: Étienne Simon <etienne.jl.simon@gmail.com>
Date:   Mon,  6 May 2013 04:13:36 +0200
Add a test of the learning algorithm.
Diffstat:
3 files changed, 104 insertions(+), 0 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -43,4 +43,5 @@ add_subdirectory(doc)
 if(${COMPILE_PROGRAMS})
 	enable_testing()
 	add_test(Serialization ${CMAKE_CURRENT_BINARY_DIR}/src/test_serialization)
+	add_test(Learning ${CMAKE_CURRENT_BINARY_DIR}/src/test_learning)
 endif(${COMPILE_PROGRAMS})
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
@@ -1,5 +1,6 @@
 set(binaries
 	"supervisor"
+	"test_learning"
 	"test_serialization"
 	"view_ctl"
 )
@@ -7,6 +8,9 @@ set(binaries
 set(supervisor_LINK ${SQLITE3_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_SERIALIZATION_LIBRARY})
 set(supervisor_SRC "supervisor.cpp" "data_set.cpp" "factory.cpp" "model.cpp")
 
+set(test_learning_LINK ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY})
+set(test_learning_SRC "test/learning.cpp" "data_set.cpp" "factory.cpp" "model.cpp")
+
 set(test_serialization_LINK ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${Boost_SERIALIZATION_LIBRARY})
 set(test_serialization_SRC "test/serialization.cpp")
 
diff --git a/src/test/learning.cpp b/src/test/learning.cpp
@@ -0,0 +1,99 @@
+#include <iostream>
+#include <string>
+#include <vector>
+
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_MODULE learning
+#include <boost/make_shared.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/test/unit_test.hpp>
+#include <nmlp/Tensor.h>
+#include <nmlp/CPUMatrix.h>
+#include <nmlp/modules/CPULinear.h>
+#include <nmlp/modules/CPULogistic.h>
+#include <nmlp/modules/SequentialModule.h>
+
+#include "../data_set.hpp"
+#include "../model.hpp"
+#include "../view.hpp"
+
+BOOST_AUTO_TEST_CASE( Learning_add_test ){
+	boost::shared_ptr<CPUMatrix> inmat=boost::make_shared<CPUMatrix>(4, 2);
+	boost::shared_ptr<CPUMatrix> outmat=boost::make_shared<CPUMatrix>(4, 1);
+	for(unsigned char i=0; i<4; ++i){
+		bool l=i&1;
+		bool r=i&2;
+		inmat->setValue(i, 0, l);
+		inmat->setValue(i, 1, r);
+		outmat->setValue(i, 0, l && r);
+	}
+
+	boost::shared_ptr<Tensor> raw_data=boost::make_shared<Tensor>(2);
+	raw_data->setMatrix(0, inmat);
+	raw_data->setMatrix(1, outmat);
+
+	std::vector<std::string> kinds;
+	kinds.push_back("and test input");
+	kinds.push_back("and test output");
+
+	Data_set data_set(raw_data, kinds);
+
+	boost::shared_ptr<View> inview=boost::make_shared<View>(), outview=boost::make_shared<View>();
+	inview->kind="and test input";
+	outview->kind="and test output";
+	inview->view_dimension=2;
+	outview->view_dimension=1;
+	inview->concept_dimension=2;
+	outview->concept_dimension=2;
+
+	inview->encoder=boost::make_shared<SequentialModule>();
+	outview->encoder=boost::make_shared<SequentialModule>();
+	inview->decoder=boost::make_shared<SequentialModule>();
+	outview->decoder=boost::make_shared<SequentialModule>();
+
+	boost::shared_ptr<CPULinear> p;
+	p=boost::make_shared<CPULinear>(2, 2);
+	p->randomize(1);
+	inview->encoder->addModule(p);
+	p=boost::make_shared<CPULinear>(1, 2);
+	p->randomize(1);
+	outview->encoder->addModule(p);
+	p=boost::make_shared<CPULinear>(2, 2);
+	p->randomize(1);
+	inview->decoder->addModule(p);
+	p=boost::make_shared<CPULinear>(2, 1);
+	p->randomize(1);
+	outview->decoder->addModule(p);
+
+	inview->encoder->addModule(boost::make_shared<CPULogistic>(2));
+	outview->encoder->addModule(boost::make_shared<CPULogistic>(2));
+	inview->decoder->addModule(boost::make_shared<CPULogistic>(2));
+	outview->decoder->addModule(boost::make_shared<CPULogistic>(1));
+
+	Model and_model;
+	and_model.add_view(inview);
+	and_model.add_view(outview);
+
+	double pre_err_00=and_model.error(data_set, "CPUSquareLoss", 0, 0);
+	double pre_err_01=and_model.error(data_set, "CPUSquareLoss", 0, 1);
+	double pre_err_10=and_model.error(data_set, "CPUSquareLoss", 1, 0);
+	double pre_err_11=and_model.error(data_set, "CPUSquareLoss", 1, 1);
+
+	and_model.stochastic_learn(data_set, 100000, 0.005, "CPUSquareLoss");
+
+	double post_err_00=and_model.error(data_set, "CPUSquareLoss", 0, 0);
+	double post_err_01=and_model.error(data_set, "CPUSquareLoss", 0, 1);
+	double post_err_10=and_model.error(data_set, "CPUSquareLoss", 1, 0);
+	double post_err_11=and_model.error(data_set, "CPUSquareLoss", 1, 1);
+
+	std::cout << "0->0 : " << pre_err_00 << " vs " << post_err_00 << "\n";
+	std::cout << "0->1 : " << pre_err_01 << " vs " << post_err_01 << "\n";
+	std::cout << "1->0 : " << pre_err_10 << " vs " << post_err_10 << "\n";
+	std::cout << "1->1 : " << pre_err_11 << " vs " << post_err_11 << "\n";
+
+	BOOST_CHECK_GT(pre_err_00, post_err_00);
+	BOOST_CHECK_GT(pre_err_01, post_err_01);
+	BOOST_CHECK_GT(pre_err_10, post_err_10);
+	BOOST_CHECK_GT(pre_err_11, post_err_11);
+}
+