net_test_1.cpp 2.06 KB
Newer Older
Martin Beseda's avatar
Martin Beseda committed
1 2 3 4 5
/**
 * Basic example using particle swarm method to train the network
 * (result 0, -1/4)
 */

6 7 8 9
//
// Created by martin on 7/16/18.
//

Martin Beseda's avatar
Martin Beseda committed
10 11
#include <vector>

12
#include "../include/4neuro.h"
Martin Beseda's avatar
Martin Beseda committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

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;

    /* Input neurons */
    NeuronLinear *i1 = new NeuronLinear(0.0, 1.0);  //f(x) = x
    NeuronLinear *i2 = new NeuronLinear(0.0, 1.0);  //f(x) = x

    /* Output neuron */
    NeuronLinear *o1 = new NeuronLinear(1.0, 2.0);  //f(x) = 2x + 1



    /* Adding neurons to the net */
    int idx1 = net.add_neuron(i1);
    int idx2 = net.add_neuron(i2);
    int idx3 = net.add_neuron(o1);

    /* 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);
    net.add_connection_simple(idx2, idx3);

    //net.randomize_weights();

Michal Kravcenko's avatar
Michal Kravcenko committed
55 56 57 58 59 60 61 62 63 64
    /* specification of the input/output neurons */
    std::vector<size_t> net_input_neurons_indices(2);
    std::vector<size_t> net_output_neurons_indices(1);
    net_input_neurons_indices[0] = idx1;
    net_input_neurons_indices[1] = idx2;

    net_output_neurons_indices[0] = idx3;

    net.specify_input_neurons(net_input_neurons_indices);
    net.specify_output_neurons(net_output_neurons_indices);
Martin Beseda's avatar
Martin Beseda committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    /* 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;
}