Skip to content
Snippets Groups Projects
x2_fitting.cpp 3.57 KiB
Newer Older
  • Learn to ignore specific revisions
  • kra568's avatar
    kra568 committed
    #include <4neuro_public.h>
    
    void optimize_via_particle_swarm(l4n::NeuralNetwork& net,
                                     l4n::ErrorFunction& ef) {
    
        /* TRAINING METHOD SETUP */
        std::vector<double> domain_bounds(2 * (net.get_n_weights() + net.get_n_biases()));
    
        for (size_t i = 0; i < domain_bounds.size() / 2; ++i) {
            domain_bounds[2 * i]     = -10;
            domain_bounds[2 * i + 1] = 10;
        }
    
        double c1          = 1.7;
        double c2          = 1.7;
        double w           = 0.7;
        size_t n_particles = 100;
        size_t iter_max    = 150;
    
        /* if the maximal velocity from the previous step is less than 'gamma' times the current maximal velocity, then one
         * terminating criterion is met */
        double gamma = 0.5;
    
        /* if 'delta' times 'n' particles are in the centroid neighborhood given by the radius 'epsilon', then the second
         * terminating criterion is met ('n' is the total number of particles) */
        double epsilon = 0.02;
        double delta   = 0.7;
    
        l4n::ParticleSwarm swarm_01(
            &domain_bounds,
            c1,
            c2,
            w,
            gamma,
            epsilon,
            delta,
            n_particles,
            iter_max
        );
        swarm_01.optimize(ef);
    
        net.copy_parameter_space(swarm_01.get_parameters());
    
        /* ERROR CALCULATION */
        std::cout << "Run finished! Error of the network[Particle swarm]: " << ef.eval(nullptr) << std::endl;
        std::cout
            << "***********************************************************************************************************************"
            << std::endl;
    }
    
    void optimize_via_gradient_descent(l4n::NeuralNetwork& net,
                                       l4n::ErrorFunction& ef) {
    
        std::cout
            << "***********************************************************************************************************************"
            << std::endl;
        l4n::GradientDescentBB gd(1e-4,
                                  100,
                                  15000);
    
        gd.optimize(ef);
    
        net.copy_parameter_space(gd.get_parameters());
    
        /* ERROR CALCULATION */
        double err = ef.eval(nullptr);
        std::cout << "Run finished! Error of the network[Gradient descent]: " << err << std::endl;
    
        /* Just for validation test purposes - NOT necessary for the example to work! */
        if(err > 0.002) {
            throw std::runtime_error("Training was incorrect!");
        }
    }
    
    
        l4n::CSVReader reader("../../data/x2_data.txt",
                              " ",
    
    Martin Beseda's avatar
    Martin Beseda committed
                              true);
    
        std::vector<unsigned int>     input_ind  = {0};
        std::vector<unsigned int>     output_ind = {1};
        std::shared_ptr<l4n::DataSet> ds         = reader.get_data_set(&input_ind,
                                                                       &output_ind);
    
        std::vector<unsigned int>     neuron_numbers_in_layers = {1, 15, 1};
        std::vector<l4n::NEURON_TYPE> hidden_type_v            = {l4n::NEURON_TYPE::LOGISTIC};
        l4n::FullyConnectedFFN        net(&neuron_numbers_in_layers,
                                          &hidden_type_v);
    
    Martin Beseda's avatar
    Martin Beseda committed
        l4n::MSE mse(&net,
                     ds.get());
    
        optimize_via_particle_swarm(net, mse);
    
        optimize_via_gradient_descent(net, mse);
    
        /* Print fit comparison with real data */
        std::vector<double> output;
        output.resize(1);
    
        for(auto e : *ds->get_data()) {
            for(auto inp_e : e.first) {
                std::cout << inp_e << " ";
            }
            std::cout << e.second.at(0) << " ";
            net.eval_single(e.first, output);
            std::cout << output.at(0) << std::endl;
        }