Skip to content
Snippets Groups Projects
SmoothingLearning.cpp 1.63 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * DESCRIPTION OF THE FILE
     *
     * @author Michal Kravčenko
     * @date 13.10.19 -
     */
    
    #include <random.hpp>
    #include <limits>
    
    #include "message.h"
    
    #include "SmoothingLearning.h"
    
    namespace lib4neuro {
    	SmoothingLearning::SmoothingLearning(
    		LearningMethod &inner_trainer,
    		double tol,
    		double grate
    	){
    		this->inner_method = &inner_trainer;
    		this->learning_tolerance = tol;
    		this->growth_rate = grate;
    	}
    
    	SmoothingLearning::~SmoothingLearning( ) {}
    
    	void SmoothingLearning::optimize(
    		lib4neuro::ErrorFunction& ef,
    		std::ofstream* ofs
    	) {
    
    	    DataSet *ds_orig = ef.get_dataset();
            double res = this->growth_rate;
    
            size_t niters = 1.0 / this->growth_rate;
            double the_best_error, current_error;
    
            current_error = ef.eval();
            the_best_error = current_error;
            for( int i = 0; i < niters; ++i ){
                DataSet *new_data_set = ds_orig->get_smoothed_set( i * this->growth_rate );
    			ef.set_dataset( new_data_set );
    
                this->inner_method->optimize( ef, ofs );
                current_error = ef.eval();
                if(current_error < the_best_error){
                    the_best_error = current_error;
                }
    
                while(current_error > the_best_error){
                    this->inner_method->optimize( ef, ofs );
    
                    current_error = ef.eval();
    
                    if(current_error < the_best_error){
                        the_best_error = current_error;
                    }
                }
    
    			delete new_data_set;
    			COUT_INFO("--------------------------------------------");
            }
    
    		ef.set_dataset( ds_orig );
    		this->inner_method->optimize( ef, ofs );
    	}
    
    }//end of namespace lib4neuro