Skip to content
Snippets Groups Projects
net_test_2.cpp 1.98 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * Example of a set neural networks dependent on each other
     */
    
    //
    // Created by martin on 7/16/18.
    //
    
    #include <vector>
    #include <utility>
    
    #include "../include/4neuro.h"
    
    int main() {
    
        /* TRAIN DATA DEFINITION */
        std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
        std::vector<double> inp, out;
    
        inp = {0, 1};
        out = {0.5};
        data_vec.emplace_back(std::make_pair(inp, out));
    
        inp = {1, 0.5};
        out = {0.75};
        data_vec.emplace_back(std::make_pair(inp, out));
    
        DataSet ds(&data_vec);
    
        /* NETWORK DEFINITION */
        NeuralNetwork net, net2;
    
        /* Input neurons */
        NeuronLinear *i1 = new NeuronLinear(0.0, 1.0);  //f(x) = x
        NeuronLinear *i2 = new NeuronLinear(0.0, 1.0);  //f(x) = x
    
        NeuronLinear *i3 = new NeuronLinear(1, 1); //f(x) = x + 1
    
        /* Output neurons */
        NeuronLinear *o1 = new NeuronLinear(1.0, 2.0);  //f(x) = 2x + 1
        NeuronLinear *o2 = new NeuronLinear(1, 2);  //f(x) = 2x + 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 = net2.add_neuron(i3);
        int idx5 = net2.add_neuron(i4);
    
        /* 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();
    
        /* ERROR FUNCTION SPECIFICATION */
        MSE mse(&net, &ds);
    
        /* TRAINING METHOD SETUP */
        unsigned int n_edges = 2;
        unsigned int dim = n_edges, max_iters = 2000;
    
    
        double domain_bounds[4] = {-800.0, 800.0, -800.0, 800.0};
    
        double c1 = 0.5, c2 = 1.5, w = 0.8;
    
        unsigned int n_particles = 10;
    
        ParticleSwarm swarm_01(&mse, domain_bounds, c1, c2, w, n_particles, max_iters);
    
        swarm_01.optimize(0.5, 0.02);
    
        return 0;
    }