Newer
Older

Michal Kravcenko
committed
/**
* DESCRIPTION OF THE FILE
*
* @author Michal Kravčenko
* @date 19.2.19 -
*/
#include "LearningSequence.h"

Michal Kravcenko
committed
namespace lib4neuro {
LearningSequence::LearningSequence( double tolerance, int max_n_cycles ){
this->tol = tolerance;
this->max_number_of_cycles = max_n_cycles;
Martin Beseda
committed
// this->optimal_parameters = new std::vector<double>();

Michal Kravcenko
committed
}
LearningSequence::~LearningSequence() = default;

Michal Kravcenko
committed
void LearningSequence::add_learning_method(std::shared_ptr<LearningMethod> method) {

Michal Kravcenko
committed
this->learning_sequence.push_back( method );
}
void LearningSequence::optimize(lib4neuro::ErrorFunction &ef, std::ofstream *ofs) {
puts("*********************** 11");

Michal Kravcenko
committed
double error = ef.eval();
puts("*********************** 12");

Michal Kravcenko
committed
double the_best_error = error;
int mcycles = this->max_number_of_cycles, cycle_idx = 0;
Martin Beseda
committed
// std::shared_ptr<std::vector<double>> best_params = std::make_shared<std::vector<double>>(this->optimal_parameters);

Michal Kravcenko
committed
while( error > this->tol && mcycles != 0){
mcycles--;
cycle_idx++;
for(auto m: this->learning_sequence ){
puts("*********************** 8");

Michal Kravcenko
committed
m->optimize( ef, ofs );
Martin Beseda
committed
//TODO do NOT copy vectors if not needed
params = *m->get_parameters();
puts("*********************** 10");
// std::shared_ptr<std::vector<double>> params;
// params.reset(m->get_parameters());
ef.get_network_instance()->copy_parameter_space(¶ms);

Michal Kravcenko
committed
if( error < the_best_error ){
the_best_error = error;
Martin Beseda
committed
this->optimal_parameters = ef.get_parameters();
// best_params = ef.get_parameters();
// best_params.reset(ef.get_parameters().get());

Michal Kravcenko
committed
}
if( error <= this->tol ){
Martin Beseda
committed
ef.get_network_instance()->copy_parameter_space( &this->optimal_parameters );

Michal Kravcenko
committed
return;
}
}
COUT_DEBUG("Cycle: " << cycle_idx << ", the lowest error: " << the_best_error << std::endl );

Michal Kravcenko
committed
}
Martin Beseda
committed
ef.get_network_instance()->copy_parameter_space( &this->optimal_parameters );