Skip to content
Snippets Groups Projects
MultiGridLearning.cpp 2.57 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * DESCRIPTION OF THE FILE
     *
     * @author Michal Kravčenko
     * @date 29.09.19 -
     */
    
    #include <random.hpp>
    #include <limits>
    
    #include <mpi.h>
    
    
    #include "message.h"
    
    #include "MultiGridLearning.h"
    
    namespace lib4neuro {
    	MultiGridLearning::MultiGridLearning(
    		LearningMethod &inner_trainer,
    		double tol,
    		double res
    	){
    		this->inner_method = &inner_trainer;
    		this->learning_tolerance = tol;
    		this->resolution_coeff = res;
    	}
    
    	MultiGridLearning::~MultiGridLearning( ) {}
    
    	void MultiGridLearning::optimize(
    		lib4neuro::ErrorFunction& ef,
    		std::ofstream* ofs
    	) {
    
    	    DataSet *ds_orig = ef.get_dataset();
            double res = this->resolution_coeff;
    
    		double multi_grid_coeff = this->resolution_coeff;
    
    		std::vector<DataSet*> data_set_hierarchies;
    		data_set_hierarchies.push_back( ds_orig );
    		DataSet *new_data_set = ds_orig->get_approximated_set( 1e-12, 0 );
    		data_set_hierarchies.push_back( new_data_set );
    
    		size_t nref = new_data_set->get_n_elements();
    		while(new_data_set->get_n_elements() >= nref){
    			res += multi_grid_coeff;
    			multi_grid_coeff *= 0.95;
    			new_data_set = new_data_set->get_approximated_set( res, 0 );
    			data_set_hierarchies.push_back( new_data_set );
    		}
    
    		for( size_t i = data_set_hierarchies.size() - 1; i > 0; --i ){
    			ef.set_dataset( data_set_hierarchies[ i ] );
    
    //			while( true ){
                    this->inner_method->optimize( ef, ofs );
    //                if( ef.eval() < this->learning_tolerance ){
    //                    break;
    //                }
    //			}
    			delete data_set_hierarchies[ i ];
    			COUT_INFO("--------------------------------------------");
    		}
    
    //		std::vector<double> output(1);
    //        for(auto e : *ds_orig->get_data()) {
    //            std::cout << "{";
    //            for( size_t j = 0; j < e.first.size(); ++j ){
    //                std::cout << e.first[ j ] << " ";
    //            }
    //            std::cout << "} -> {";
    //
    //            std::cout << e.second.at(0) << " ";
    //            ef.get_nets()[0]->eval_single(e.first, output);
    //            std::cout << output.at(0) << "}" << std::endl;
    //        }
    //
    //        for(auto e : *ef.get_dataset()->get_data()) {
    //            std::cout << "{";
    //            for( size_t j = 0; j < e.first.size(); ++j ){
    //                std::cout << e.first[ j ] << " ";
    //            }
    //            std::cout << "} -> {";
    //
    //            std::cout << e.second.at(0) << " ";
    //            ef.get_nets()[0]->eval_single(e.first, output);
    //            std::cout << output.at(0) << "}" << std::endl;
    //        }
    		ef.set_dataset( ds_orig );
    		this->inner_method->optimize( ef, ofs );
    	}
    
    }//end of namespace lib4neuro