Newer
Older
* 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)
//
#include <vector>
#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, 0};
out = {0.5, 0};
data_vec.emplace_back(std::make_pair(inp, out));
inp = {1, 0.5, 0};
out = {0.75, 0};
data_vec.emplace_back(std::make_pair(inp, out));
inp = {0, 0, 1.25};
out = {0, 0.63};
data_vec.emplace_back(std::make_pair(inp, out));
DataSet ds(&data_vec);
/* NETWORK DEFINITION */
NeuralNetwork net;
/* Input neurons */
NeuronLinear *i1 = new NeuronLinear( ); //f(x) = x
NeuronLinear *i2 = new NeuronLinear( ); //f(x) = x

Michal Kravcenko
committed
NeuronLinear *i3 = new NeuronLinear( ); //f(x) = x + 1
/* Output neurons */

Michal Kravcenko
committed
NeuronLinear *o1 = new NeuronLinear( ); //f(x) = x + 1
NeuronLinear *o2 = new NeuronLinear( ); //f(x) = x + 1
/* Adding neurons to the nets */

Michal Kravcenko
committed
size_t idx1 = net.add_neuron(i1, BIAS_TYPE::NO_BIAS);
size_t idx2 = net.add_neuron(i2, BIAS_TYPE::NO_BIAS);
size_t idx3 = net.add_neuron(o1, BIAS_TYPE::NEXT_BIAS);
size_t idx4 = net.add_neuron(i3, BIAS_TYPE::NEXT_BIAS);
size_t idx5 = net.add_neuron(o2, BIAS_TYPE::NEXT_BIAS);
std::vector<double> *bv = net.get_parameter_ptr_biases();
for(size_t i = 0; i < 3; ++i){
bv->at(i) = 1.0;
}
/* Adding connections */
//net.add_connection_simple(idx1, idx3, -1, 1.0);
//net.add_connection_simple(idx2, idx3, -1, 1.0);

Michal Kravcenko
committed
net.add_connection_simple(idx1, idx3, SIMPLE_CONNECTION_TYPE::NEXT_WEIGHT); // weight index 0
net.add_connection_simple(idx2, idx3, SIMPLE_CONNECTION_TYPE::NEXT_WEIGHT); // weight index 1
net.add_connection_simple(idx4, idx5, SIMPLE_CONNECTION_TYPE::EXISTING_WEIGHT, 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 */
MSE mse(&net, &ds);
// double weights[2] = {-0.18012411, -0.17793740};
// double weights[2] = {1, 1};
// printf("evaluation of error at point (%f, %f) => %f\n", weights[0], weights[1], mse.eval(weights));
/* TRAINING METHOD SETUP */
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;
ParticleSwarm swarm_01(&mse, domain_bounds, c1, c2, w, n_particles, max_iters);
return 0;
}