Skip to content
Snippets Groups Projects
net_test_2.cpp 3.08 KiB
Newer Older
  • Learn to ignore specific revisions
  • Michal Kravcenko's avatar
    Michal Kravcenko committed
     * Example of a neural network with reused edge weights
     * The system of equations associated with the net in this example is not regular
     * minimizes the function: ((2y+0.5)^2 + (2x+1)^2 + (2x + y + 0.25)^2 + (2x+1)^2 + 1 + (4.5x + 0.37)^2 ) /3
     * minimum [0.705493164] at (x, y) = (-1133/6290, -11193/62900) = (-0.180127186, -0.177949126)
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
    // Created by Michal on 7/17/18.
    
    //
    
    #include <vector>
    
    #include "../include/4neuro.h"
    
    int main() {
    
        /* TRAIN DATA DEFINITION */
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        inp = {0, 1, 0};
        out = {0.5, 0};
        data_vec.emplace_back(std::make_pair(inp, out));
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        inp = {1, 0.5, 0};
        out = {0.75, 0};
        data_vec.emplace_back(std::make_pair(inp, out));
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        inp = {0, 0, 1.25};
        out = {0, 0.63};
        data_vec.emplace_back(std::make_pair(inp, out));
        DataSet ds(&data_vec);
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        NeuronLinear *i1 = new NeuronLinear( );  //f(x) = x
        NeuronLinear *i2 = new NeuronLinear( );  //f(x) = x
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        double b = 1;//bias
        NeuronLinear *i3 = new NeuronLinear(&b); //f(x) = x + 1
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        NeuronLinear *o1 = new NeuronLinear(&b);  //f(x) = x + 1
        NeuronLinear *o2 = new NeuronLinear(&b);  //f(x) = x + 1
    
    
    
    
        /* Adding neurons to the nets */
        int idx1 = net.add_neuron(i1);
        int idx2 = net.add_neuron(i2);
        int idx3 = net.add_neuron(o1);
    
        int idx4 = net.add_neuron(i3);
        int idx5 = net.add_neuron(o2);
    
    
        /* Adding connections */
        //net.add_connection_simple(idx1, idx3, -1, 1.0);
        //net.add_connection_simple(idx2, idx3, -1, 1.0);
        net.add_connection_simple(idx1, idx3); // weight index 0
        net.add_connection_simple(idx2, idx3); // weight index 1
        net.add_connection_simple(idx4, idx5, 0); // AGAIN weight index 0 - same weight!
    
    
        net.randomize_weights();
    
        /* specification of the input/output neurons */
        std::vector<size_t> net_input_neurons_indices(3);
        std::vector<size_t> net_output_neurons_indices(2);
        net_input_neurons_indices[0] = idx1;
        net_input_neurons_indices[1] = idx2;
        net_input_neurons_indices[2] = idx4;
    
        net_output_neurons_indices[0] = idx3;
        net_output_neurons_indices[1] = idx5;
    
        net.specify_input_neurons(net_input_neurons_indices);
        net.specify_output_neurons(net_output_neurons_indices);
    
    
    
    
    
        /* COMPLEX ERROR FUNCTION SPECIFICATION */
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        MSE mse(&net, &ds);
    
    //    double weights[2] = {-0.18012411, -0.17793740};
    //    double weights[2] = {1, 1};
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
    //    printf("evaluation of error at point (%f, %f) => %f\n", weights[0], weights[1], mse.eval(weights));
    
        unsigned int max_iters = 5000;
    
        //must encapsulate each of the partial error functions
    
        double domain_bounds[4] = {-800.0, 800.0, -800.0, 800.0};
    
        double c1 = 0.5, c2 = 1.5, w = 0.8;
    
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        unsigned int n_particles = 100;
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        ParticleSwarm swarm_01(&mse, domain_bounds, c1, c2, w, n_particles, max_iters);
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        swarm_01.optimize(0.5, 0.02, 0.9);
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        printf("evaluation of error: %f\n", mse.eval());