mi024

College project "Projet IAD" master 1
git clone https://esimon.eu/repos/mi024.git
Log | Files | Refs | README

learning.cpp (3429B)


      1 #include <iostream>
      2 #include <string>
      3 #include <vector>
      4 
      5 #define BOOST_TEST_DYN_LINK
      6 #define BOOST_TEST_MODULE learning
      7 #include <boost/make_shared.hpp>
      8 #include <boost/shared_ptr.hpp>
      9 #include <boost/test/unit_test.hpp>
     10 #include <nmlp/Tensor.h>
     11 #include <nmlp/CPUMatrix.h>
     12 #include <nmlp/modules/CPULinear.h>
     13 #include <nmlp/modules/CPULogistic.h>
     14 #include <nmlp/modules/SequentialModule.h>
     15 
     16 #include "../data_set.hpp"
     17 #include "../model.hpp"
     18 #include "../view.hpp"
     19 
     20 BOOST_AUTO_TEST_CASE( Learning_add_test ){
     21 	boost::shared_ptr<CPUMatrix> inmat=boost::make_shared<CPUMatrix>(4, 2);
     22 	boost::shared_ptr<CPUMatrix> outmat=boost::make_shared<CPUMatrix>(4, 1);
     23 	for(unsigned char i=0; i<4; ++i){
     24 		bool l=i&1;
     25 		bool r=i&2;
     26 		inmat->setValue(i, 0, l);
     27 		inmat->setValue(i, 1, r);
     28 		outmat->setValue(i, 0, l && r);
     29 	}
     30 
     31 	boost::shared_ptr<Tensor> raw_data=boost::make_shared<Tensor>(2);
     32 	raw_data->setMatrix(0, inmat);
     33 	raw_data->setMatrix(1, outmat);
     34 
     35 	std::vector<std::string> kinds;
     36 	kinds.push_back("and test input");
     37 	kinds.push_back("and test output");
     38 
     39 	Data_set data_set(raw_data, kinds);
     40 
     41 	boost::shared_ptr<View> inview=boost::make_shared<View>(), outview=boost::make_shared<View>();
     42 	inview->kind="and test input";
     43 	outview->kind="and test output";
     44 	inview->view_dimension=2;
     45 	outview->view_dimension=1;
     46 	inview->concept_dimension=2;
     47 	outview->concept_dimension=2;
     48 
     49 	inview->encoder=boost::make_shared<SequentialModule>();
     50 	outview->encoder=boost::make_shared<SequentialModule>();
     51 	inview->decoder=boost::make_shared<SequentialModule>();
     52 	outview->decoder=boost::make_shared<SequentialModule>();
     53 
     54 	boost::shared_ptr<CPULinear> p;
     55 	p=boost::make_shared<CPULinear>(2, 2);
     56 	p->randomize(1);
     57 	inview->encoder->addModule(p);
     58 	p=boost::make_shared<CPULinear>(1, 2);
     59 	p->randomize(1);
     60 	outview->encoder->addModule(p);
     61 	p=boost::make_shared<CPULinear>(2, 2);
     62 	p->randomize(1);
     63 	inview->decoder->addModule(p);
     64 	p=boost::make_shared<CPULinear>(2, 1);
     65 	p->randomize(1);
     66 	outview->decoder->addModule(p);
     67 
     68 	inview->encoder->addModule(boost::make_shared<CPULogistic>(2));
     69 	outview->encoder->addModule(boost::make_shared<CPULogistic>(2));
     70 	inview->decoder->addModule(boost::make_shared<CPULogistic>(2));
     71 	outview->decoder->addModule(boost::make_shared<CPULogistic>(1));
     72 
     73 	Model and_model;
     74 	and_model.add_view(inview);
     75 	and_model.add_view(outview);
     76 
     77 	double pre_err_00=and_model.error(data_set, "CPUSquareLoss", 0, 0);
     78 	double pre_err_01=and_model.error(data_set, "CPUSquareLoss", 0, 1);
     79 	double pre_err_10=and_model.error(data_set, "CPUSquareLoss", 1, 0);
     80 	double pre_err_11=and_model.error(data_set, "CPUSquareLoss", 1, 1);
     81 
     82 	and_model.stochastic_learn(data_set, 100000, 0.005, "CPUSquareLoss");
     83 
     84 	double post_err_00=and_model.error(data_set, "CPUSquareLoss", 0, 0);
     85 	double post_err_01=and_model.error(data_set, "CPUSquareLoss", 0, 1);
     86 	double post_err_10=and_model.error(data_set, "CPUSquareLoss", 1, 0);
     87 	double post_err_11=and_model.error(data_set, "CPUSquareLoss", 1, 1);
     88 
     89 	std::cout << "0->0 : " << pre_err_00 << " vs " << post_err_00 << "\n";
     90 	std::cout << "0->1 : " << pre_err_01 << " vs " << post_err_01 << "\n";
     91 	std::cout << "1->0 : " << pre_err_10 << " vs " << post_err_10 << "\n";
     92 	std::cout << "1->1 : " << pre_err_11 << " vs " << post_err_11 << "\n";
     93 
     94 	BOOST_CHECK_GT(pre_err_00, post_err_00);
     95 	BOOST_CHECK_GT(pre_err_01, post_err_01);
     96 	BOOST_CHECK_GT(pre_err_10, post_err_10);
     97 	BOOST_CHECK_GT(pre_err_11, post_err_11);
     98 }
     99